1

I am using markdown-it to parse markup documents. My code is as follows:

import React from 'react';
import Markdown from 'markdown-it';

const md = new Markdown();

export default () => {
  return (
    <div>
    {
      md.render('# markdown-it rulezz!')
    }
    </div>
  );
}

The h1 being returned is correct, but the output is not registering as an h1; the h1 is considered part of the string - it's not being recognized as a tag. See attachment. Can someone please tell me what I'm doing wrong here? Thanks!

enter image description here

2
  • __dangerouslySetInnerHTML? Commented Oct 5, 2017 at 11:20
  • Thanks! That is what I was looking for. Commented Oct 11, 2017 at 2:42

2 Answers 2

2

You should use:

<div dangerouslySetInnerHTML={{__html: md.render('# markdown-it rulezz!')}}></div>

Here's reference: https://zhenyong.github.io/react/tips/dangerously-set-inner-html.html

Or you can you use react-markdown

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

1 Comment

Thanks! That is what I was looking for.
0

No problem when using it like this ;

<p dangerouslySetInnerHTML={ {__html: PostItem.Content} } />

But when he does this, he gives an error

<p dangerouslySetInnerHTML={{__html: md.render(PostItem.Content)}}></p>

The error message is as follows ;

Error: Input data should be a String

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.