Yilmaz
Yilmaz

Reputation: 49681

Parse subscript in markdown with react-markdown

I wrote this syntax to get subscript in an .md file:

   x_i
   x~i~

react-markdown did not parse this as a subscript. I found the package remark-sub-super and this plugin as follows:

       <ReactMarkdown
          renderers={customRenderers }
          plugins={[remarkSubSuper]}
        >
          {blog.content}
        </ReactMarkdown>

This gives me an error: TypeError: Cannot set property 'sub_super' of undefined. I also added skipHtml=true to the component and wrote this in the .md file:

  b<sub>i</sub>

This did not work either. I am using next.js.

Upvotes: 1

Views: 1387

Answers (1)

Mayank Kumar Chaudhari
Mayank Kumar Chaudhari

Reputation: 18696

Use the below code

<ReactMarkdown children={props.content} 
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} /> 
            },
 }}

Basically we are creating a new custom plugin.

Upvotes: 3

Related Questions