1

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.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
Yilmaz
  • 35,338
  • 10
  • 157
  • 202

1 Answers1

4

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.

You may also like to read this story.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122