I'm building a block that shows a bunch of posts of a custom post type. This is the loop that prints them:
{ customposts.map( ( custompost, index ) => {
return <div key={ index } className="custom-post">
<h2 id={ custompost.id } className="custom-post-title">{ custompost.title.raw }</h2>
<div className="custom-post-content">{ custompost.content.rendered }</div>
</div>;
} ) }
But, using either custompost.content.raw
or custompost.content.rendered
shows uninterpreted HTML code. In the first case, it prints the blocks markup (with HTML comments), and in the second case, the HTML markup, but in both cases, the code is shown verbatim.
If I wrap this with a RawHTML
component, the content is shown correctly, but according to the docs, this is discouraged as this is prone to cross-site scripting.
// This is discouraged
<div className="custom-post-content"><RawHTML>{ custompost.content.rendered }</RawHTML></div>
What is the best practice for displaying post content in the block editor?