Parse strings with tags into react components
npm install react-tiny-markupThis component aims to parse a simple markup language nontechnical users may input - typically , and tags. It will never be a complete HTML5 parser (tag nesting validation, autoclosing tags - none of that). I wanted a smaller (~2.4kB vs ~60+kB) alternative to sanitize-html.
It doesn't use dangerouslySetInnerHTML.
``JSX
// replace or remove tags
const str =
left in;`
switch (p.tag) {
case 'ooo':
return createElement('c', { key: p.key }, p.children);
case 'remove':
return null;
default:
return createElement(p.tag, { key: p.key }, p.children);
}
}}
>
{str}
//
`JSX
// simply parse tags
const str = 'abcabcde';
// abcabcde
// rewrite tag (strong in this case) and simply parse the rest`
p.tag === 'strong' ? (
{p.children}
) : (
defaultRenderer(p)
)
}
>
{str}
// abcabcde
`JSX``
// Parse tags with attributes
const str = 'Look at this dog
';
>
{str}
// renders
// <>Look at this dog
>