GitbookIO / markup-it

JavaScript library to parse and serialize markup content (Markdown and HTML)
270 stars 49 forks source link

Allow recognizing React elements with complex props as HTML in markdown #146

Closed jpreynat closed 4 years ago

jpreynat commented 4 years ago

Currently, the markdown parser crashed if markdown includes a complex React Element such as:

<ComplexList icon={<Icon type="check" />} type="info">
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
  </ul>
</ComplexList>

This happens because the markdown regex for HTML blocks doesn't recognize the ComplexList element as HTML because of the nested element in the props. It basically stops recognizing the block at: <ComplexList icon={<Icon type="check" />, which is not recognized as a finished tag.

What's happening next is that the markdown regex for HTML inlines recognizes it as an inline tag and does the following:

To fix the issue, the markdown regex for HTML have been updated to recognize React Elements as HTML tags completely, so now the HTML blocks regex properly recognize the ComplexList block as HTML. It is then fully parsed as HTML using the HTMLParser, which ignores non-existing HTML tags and strips it.

You can see in the test file that the type="info"> text remains after stripping the element. This happens because we use htmlparser2 that also stops its parsing for the tag at the nested element closing tag.