unified

Project: remarkjs/react-markdown

Package: react-markdown@5.0.3

  1. Dependencies: 0·Dependents: 677
  2. Render Markdown as React components
  1. remark 189
  2. unified 166
  3. markdown 132
  4. ast 30
  5. gfm 16
  6. react 15
  7. commonmark 4

react-markdown

Build Coverage Downloads Size Sponsors Backers Chat

Markdown component for React using remark.

Learn markdown here and check out the demo here.

Install

npm:

npm install react-markdown

Why this one?

There are other ways for markdown in React out there so why use this one? The two main reasons are that they often rely on dangerouslySetInnerHTML or have bugs with how they handle markdown. react-markdown uses a syntax tree to build the virtual dom which allows for updating only the changing DOM instead of completely overwriting. react-markdown is 100% CommonMark (optionally GFM) compliant and has extensions to support custom syntax.

Use

A basic hello world:

import React from 'react'
import ReactMarkdown from 'react-markdown'
import {render} from 'react-dom'

render(<ReactMarkdown># Hello, *world*!</ReactMarkdown>, document.body)
Show equivalent JSX
<h1>
  Hello, <em>world</em>!
</h1>

Here is an example using requires, passing the markdown as a string, and how to use a plugin (remark-gfm, which adds support for strikethrough, tables, tasklists and URLs directly):

const React = require('react')
const ReactMarkdown = require('react-markdown')
const render = require('react-dom').render
const gfm = require('remark-gfm')

const markdown = `Just a link: https://reactjs.com.`

render(<ReactMarkdown plugins={[gfm]} children={markdown} />, document.body)
Show equivalent JSX
<p>
  Just a link: <a href="https://reactjs.com">https://reactjs.com</a>.
</p>

API

props

Examples

Use a plugin

This example shows how to use a plugin. In this case, remark-gfm, which adds support for strikethrough, tables, tasklists and URLs directly:

import React from 'react'
import ReactMarkdown from 'react-markdown'
import {render} from 'react-dom'
import gfm from 'remark-gfm'

const markdown = `A paragraph with *emphasis* and **strong importance**.

> A block quote with ~strikethrough~ and a URL: https://reactjs.org.

* Lists
* [ ] todo
* [x] done

A table:

| a | b |
| - | - |
`

render(<ReactMarkdown plugins={[gfm]} children={markdown} />, document.body)
Show equivalent JSX
<>
  <p>
    A paragraph with <em>emphasis</em> and <strong>strong importance</strong>.
  </p>
  <blockquote>
    <p>
      A block quote with <del>strikethrough</del> and a URL:{' '}
      <a href="https://reactjs.org">https://reactjs.org</a>.
    </p>
  </blockquote>
  <ul>
    <li>Lists</li>
    <li>
      <input checked={false} readOnly={true} type="checkbox" /> todo
    </li>
    <li>
      <input checked={true} readOnly={true} type="checkbox" /> done
    </li>
  </ul>
  <p>A table:</p>
  <table>
    <thead>
      <tr>
        <td>a</td>
        <td>b</td>
      </tr>
    </thead>
  </table>
</>

Use a plugin with options

This example shows how to use a plugin and give it options. To do that, use an array with the plugin at the first place, and the options second. remark-gfm has an option to allow only double tildes for strikethrough:

import React from 'react'
import ReactMarkdown from 'react-markdown'
import {render} from 'react-dom'
import gfm from 'remark-gfm'

render(
  <ReactMarkdown plugins={[[gfm, {singleTilde: false}]]}>
    This ~is not~ strikethrough, but ~~this is~~!
  </ReactMarkdown>,
  document.body
)
Show equivalent JSX
<p>
  This ~is not~ strikethrough, but <del>this is</del>!
</p>

Use custom renderers (syntax highlight)

This example shows how you can overwrite the normal handling of a node by passing a renderer. In this case, we apply syntax highlighting with the seriously super amazing react-syntax-highlighter by @conorhastings:

import React from 'react'
import ReactMarkdown from 'react-markdown'
import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter'
import {dark} from 'react-syntax-highlighter/dist/esm/styles/prism'
import {render} from 'react-dom'

const renderers = {
  code: ({language, value}) => {
    return <SyntaxHighlighter style={dark} language={language} children={value} />
  }
}

// Did you know you can use tildes instead of backticks for code in markdown? ✨
const markdown = `Here is some JavaScript code:

~~~js
console.log('It works!')
~~~
`

render(<ReactMarkdown renderers={renderers} children={markdown} />, document.body)
Show equivalent JSX
<>
  <p>Here is some JavaScript code:</p>
  <SyntaxHighlighter language="js" style={dark} children="console.log('It works!')" />
</>

Use a plugin and custom renderers (math)

This example shows how a syntax extension is used to support math in markdown that adds new node types (remark-math), which are then handled by renderers to use @matejmazur/react-katex:

import React from 'react'
import ReactMarkdown from 'react-markdown'
import Tex from '@matejmazur/react-katex'
import {render} from 'react-dom'
import math from 'remark-math'
import 'katex/dist/katex.min.css' // `react-katex` does not import the CSS for you

const renderers = {
  inlineMath: ({value}) => <Tex math={value} />,
  math: ({value}) => <Tex block math={value} />
}

render(
  <ReactMarkdown
    plugins={[math]}
    renderers={renderers}
    children={`The lift coefficient ($C_L$) is a dimensionless coefficient.`}
  />,
  document.body
)
Show equivalent JSX
<p>
  The lift coefficient (<InlineMath math="C_L" />) is a dimensionless coefficient.
</p>

Appendix A: HTML in markdown

react-markdown typically escapes HTML (or ignores it, with skipHtml), because it is dangerous and defeats the purpose of this library.

However, if you are in a trusted environment (you trust the markdown), you can react-markdown/with-html:

const React = require('react')
const ReactMarkdownWithHtml = require('react-markdown/with-html')
const render = require('react-dom').render

const markdown = `
This Markdown contains <a href="https://en.wikipedia.org/wiki/HTML">HTML</a>, and will require the <code>html-parser</code> AST plugin to be loaded, in addition to setting the <code class="prop">allowDangerousHtml</code> property to false.
`

render(<ReactMarkdownWithHtml children={markdown} allowDangerousHtml />, document.body)
Show equivalent JSX
<p>
  This Markdown contains <a href="https://en.wikipedia.org/wiki/HTML">HTML</a>, and will require
  the <code>html-parser</code> AST plugin to be loaded, in addition to setting the{' '}
  <code className="prop">allowDangerousHtml</code> property to false.
</p>

If you want to specify options for the HTML parsing step, you can instead import the extension directly:

const ReactMarkdown = require('react-markdown')
const htmlParser = require('react-markdown/plugins/html-parser')

// For more info on the processing instructions, see
// <https://github.com/aknuds1/html-to-react#with-custom-processing-instructions>
const parseHtml = htmlParser({
  isValidNode: (node) => node.type !== 'script',
  processingInstructions: [
    /* ... */
  ]
})

<ReactMarkdown astPlugins={[parseHtml]} allowDangerousHtml children={markdown} />

Appendix B: Node types

The node types available by default are:

With remark-gfm, the following are also available:

Security

Use of react-markdown is secure by default. Overwriting transformLinkUri or transformImageUri to something insecure or turning allowDangerousHtml on, will open you up to XSS vectors. Furthermore, the plugins you use and renderers you write may be insecure.

Contribute

See contributing.md in remarkjs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Espen Hovlandsdal