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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.