<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Miro Haapamäki</title>
    <description>The latest articles on DEV Community by Miro Haapamäki (@mirohaapa).</description>
    <link>https://dev.to/mirohaapa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1113020%2F4ef2c449-cc36-4080-a089-ccb6d3fa5a49.png</url>
      <title>DEV Community: Miro Haapamäki</title>
      <link>https://dev.to/mirohaapa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mirohaapa"/>
    <language>en</language>
    <item>
      <title>React Content Rendering</title>
      <dc:creator>Miro Haapamäki</dc:creator>
      <pubDate>Sat, 02 Sep 2023 08:15:10 +0000</pubDate>
      <link>https://dev.to/mirohaapa/react-content-rendering-pde</link>
      <guid>https://dev.to/mirohaapa/react-content-rendering-pde</guid>
      <description>&lt;p&gt;Hello guys!&lt;/p&gt;

&lt;p&gt;I am building my own combined CMS and ecommerce platform in React to meet my personal requirements. I needed an ability to create, edit, remove and view page content in the application - just to be easier to edit content, when project is in production. &lt;/p&gt;

&lt;p&gt;I want to share you my CMS approach and hear your opinions on this. In this posting I will show you, how the content is rendered - because I think that part is the more relevant.&lt;/p&gt;

&lt;p&gt;There is two packages required in this approach and they are '&lt;a href="https://www.npmjs.com/package/html-react-parser"&gt;html-react-parser&lt;/a&gt;' and '&lt;a href="https://www.npmjs.com/package/dompurify"&gt;dompurify&lt;/a&gt;'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's begin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First we have the component called Content. In my personal use case, Content component will get content ID as prop, so it can handle also logic to fetch content string from API.&lt;/p&gt;

&lt;p&gt;In this example I am using HTML string, which we are rendering in the page. You may notice, that the htmlString has also some strange looking syntax, which starts with &lt;code&gt;{{&lt;/code&gt; and ends with &lt;code&gt;}}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It is syntax for components, because we want to have an ability to render also components with this solution.&lt;/p&gt;

&lt;p&gt;For example, in my use case I might want to rended component that displays newest products, or top-selling items, etc.&lt;/p&gt;

&lt;p&gt;We will also sanitize the htmlString by using 'dompurify' package to prevent XSS attacks and other bad things.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import DOMPurify from "dompurify"
import parse from "html-react-parser"

const Content = () =&amp;gt; {

   const htmlString = `
       &amp;lt;h1&amp;gt;Lorem ipsum&amp;lt;/h1&amp;gt;
       {{
       ExampleComponent(
           text: 'Lorem ipsum',
           color: 'red'
       )
       }}
       &amp;lt;p&amp;gt;Lorem ipsum dolor sit amet.&amp;lt;/p&amp;gt;
       &amp;lt;p&amp;gt;And the string continues ...&amp;lt;/p&amp;gt;
   `

   const sanitizedHtml = DOMPurify.sanitize(htmlString)
   ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before moving on to parsing and rendering, we have to define what kind of component's are accepted to render. And don't forget to import components, if they are in other file!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const acceptedComponents = {
    ExampleComponent,
    OtherComponent
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, now we have sanitized our html string, defined accepted components and it is time move on to parsing and rendering the content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

function parseHtml(html) {

    // We will split our HTML in to parts
    const parts = html.split(/(\{{[^\}]+\}})/)

    // Then we will map every part and return them correctly
    return parts.map((part, index) =&amp;gt; {

        // In cases the part is surrounded with {{ }}, we will try to find a component, handle props, which are surrounded in ( )
        if (part.startsWith('{{') &amp;amp;&amp;amp; part.endsWith('}}')) {

            const content = part.substring(2, part.length - 2).trim()
            const [componentName, propString] = content.split(/\(|\)/)

            // Tarkistetaan löytyykö komponentin nimi sallittujen listalta
            const Component = acceptedComponents[componentName]

            if (Component) {
                const props = {}

                // If we find some props
                if (propString) {
                    const propPairs = propString.split(',')

                    // Let's map also props
                    propPairs.forEach(pair =&amp;gt; {
                        const [propName, propValue] = pair.split(':').map(item =&amp;gt; item.trim())

                        if (propName &amp;amp;&amp;amp; propName.match(/^[A-Za-z]\w*$/) &amp;amp;&amp;amp; propValue &amp;amp;&amp;amp; propValue.match(/^".*"$/)) {
                            props[propName] = propValue.replace(/"/g, '')
                        }
                    })
                }

                // Return component with or without props and continue to next part
                return &amp;lt;Component key={index} {...props} /&amp;gt;
                }
                // To prevent errors, we want to also return message if component didn't found.
                else {
                    return &amp;lt;p key={index}&amp;gt;Component {componentName} not found&amp;lt;/p&amp;gt;
                }
            }
            // If it is not component, we will parse HTML by using 'html-react-parser' package.
            return parse(part)
        })
    }

    // Component will return HTML parsed, showing HTML as HTMl and components as components, if found any.
    return parseHtml(sanitizedHtml)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you guys think of this approach? 😊&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
