26

I am using tinymce to accept markdown data from user. The output data is html. I want to display that data on a page. I am using react-markdown for it. I can see data on page but it's HTML tags. Is there any way to show HTML page not tags?

export default function ThePage() {
  const markdown = {
    description: "<p>Hello from the other </p>\n<p><strong>side</strong></p>",
  }

  return (
    <>
      <ReactMarkdown children={markdown.description} />
    </>
  );
}

3 Answers 3

61

The ReactMarkdown component is for rendering mark down, not HTML mark up 😏. Given HTML input it just escapes it, and that's why you see it as "source", not formatted text.

If you need to use it with HTML you need to apply a plugin, such as rehypeRaw :

import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";

//...

export default function ThePage() {
  const markdown = {
    description: "<p>Hello from the other </p>\n<p><strong>side</strong></p>"
  }

  return (
    <>
      <ReactMarkdown children={markdown.description} rehypePlugins={[rehypeRaw]} />
    </>
  );
}
Sign up to request clarification or add additional context in comments.

5 Comments

You legend!! Thanks for sharing this, that rehypeRaw plugin is pretty sweet :)
Also take a look at github.com/remarkjs/remark-gfm plugin for react-markdown to allow redendering of strikethrough, checkboxes and tables in markdown.
I tried this and it is not working it crashes forme
2023 update; to prevent Typescript complaining use rehypePlugins={[rehypeRaw] as any}
Just to clarify, could this cause a cross-site scripting vulnerability when rendering un-trusted content?
4

Yes you can use react-render-markup see example:

import { Markup } from "react-render-markup";
 export default function App(){
 return(<Markup markup={markdown.description} />)
 }

2 Comments

This is the best solution if you only have HTML, and not markdown + HTML
Then it's not an answer, the question is about combining the two!
1

The approach would be

Step #1 Convert markdown to HTML string

Step #2 Render HTML string in Next.js JSX

For Step #1, I went with "npm i markdown-to-jsx"

For Step #2, I followed the tailwindcss typography guide from https://github.com/tailwindlabs/tailwindcss-typography

The steps are as follows:

  1. Install the plugin from npm:

    npm install -D @tailwindcss/typography

  2. Add the plugin to your tailwind.config.js file:

    module.exports = {
      theme: {
        // ...
      },
      plugins: [
        require('@tailwindcss/typography'), // add this line
        // ...
      ],
    }

  1. Add the "prose" tailwind class to the div which is going to contain the html string

<div className="prose">{markdownToHTML}</div>

  1. Follow the tailwindcss typography guide for advanced styling

2 Comments

This turned out to be a bit confusing.
Don't post the same answer multiple times

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.