2.5 KiB
2.5 KiB
react-markdown
React component to render markdown.
Contents
What is this?
This package is a React component that can be given a string of markdown that it'll safely render to React elements. You can pass plugins to change how markdown is transformed and pass components that will be used instead of normal HTML elements.
Install
npm install react-markdown
Use
Basic usage:
import Markdown from "react-markdown";
const markdown = "# Hi, *Pluto*!";
<Markdown>{markdown}</Markdown>
With plugins:
import Markdown from "react-markdown";
import remarkGfm from "remark-gfm";
const markdown = `Just a link: www.nasa.gov.`;
<Markdown remarkPlugins={[remarkGfm]}>{markdown}</Markdown>
API
Key props:
children— markdown string to renderremarkPlugins— array of remark pluginsrehypePlugins— array of rehype pluginscomponents— object mapping HTML tags to React componentsallowedElements— array of allowed HTML tagsdisallowedElements— array of disallowed HTML tags
Examples
Using GitHub Flavored Markdown
import Markdown from "react-markdown";
import remarkGfm from "remark-gfm";
const markdown = `
* [x] todo
* [ ] done
| Column 1 | Column 2 |
|----------|----------|
| Cell 1 | Cell 2 |
`;
<Markdown remarkPlugins={[remarkGfm]}>{markdown}</Markdown>
Custom Components (Syntax Highlighting)
import Markdown from "react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { dark } from "react-syntax-highlighter/dist/esm/styles/prism";
const markdown = `
\`\`\`js
console.log('Hello, world!');
\`\`\`
`;
<Markdown
components={{
code(props) {
const { children, className, ...rest } = props;
const match = /language-(\w+)/.exec(className || "");
return match ? (
<SyntaxHighlighter
{...rest}
PreTag="div"
children={String(children).replace(/\n$/, "")}
language={match[1]}
style={dark}
/>
) : (
<code {...rest} className={className}>
{children}
</code>
);
},
}}
>
{markdown}
</Markdown>
Plugins
Common plugins:
remark-gfm— GitHub Flavored Markdown (tables, task lists, strikethrough)remark-math— Math notation supportrehype-katex— Render math with KaTeXrehype-highlight— Syntax highlightingrehype-raw— Allow raw HTML (use carefully for security)