I am unable to render subscript the markdown notation (~this is subscript~but not this) using react-markdown.
There are three ways I could easily find to render a do this: remark-supersub, remark-sub-super and something like this:
components={{
em: ({ node, ...props }) => {
if ( props.children[0] && typeof props.children[0] === 'string' && props.children[0].startsWith('^')) {
return <sup>{props.children[0].substring(1)}</sup>
}
if ( props.children[0] && typeof props.children[0] === 'string' && props.children[0].startsWith('~')) {
return <sub>{props.children[0].substring(1)}</sub>
}
return <i {...props} />
},
}}
... none of them works for me (they either do nothing or throw an error while rendering). Any other ideas? Thanks in advance.
One of the packages you mentioned,
remark-supersubworks pretty well without much configuration.You should pass this plugin as a
remarkPluginsprop in theMarkdowncomponent:This will render your markdown like this:
Update: As seen in your comment, the issue lies with
remark-gfmbecause this plugin will interpret by default~text~as strikethrough. The solution would be to pass{ singleTilde: false }toremarkGfm, that way you can keep a single~text~as subtext instead and~~text~~as strikethrough.