rehype-react
rehype plugin to compile HTML to React nodes.
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package is a unified (rehype) plugin that compiles HTML (hast) to React nodes (the virtual DOM that React uses).
unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that adds a compiler to compile hast to React nodes.
When should I use this?
This plugin adds a compiler for rehype, which means that it turns the final HTML (hast) syntax tree into something else (in this case, a React node). Itβs useful when youβre already using unified (whether remark or rehype) or are open to learning about ASTs (theyβre powerful!) and want to render content in your React app.
If youβre not familiar with unified, then react-markdown
might be a better fit.
You can also use react-remark
instead, which is somewhere
between rehype-react
and react-markdown
, as it does more that the former and
is more modern (such as supporting hooks) than the latter, and also a good
alternative.
If you want to use JavaScript and JSX inside markdown files, use MDX.
Install
This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:
npm install rehype-react
In Deno with esm.sh
:
import rehypeReact from 'https://esm.sh/rehype-react@7'
In browsers with esm.sh
:
<script type="module">
import rehypeReact from 'https://esm.sh/rehype-react@7?bundle'
</script>
Use
Say our React app module example.js
looks as follows:
import {createElement, Fragment, useEffect, useState} from 'react'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeReact from 'rehype-react'
const text = `<h2>Hello, world!</h2>
<p>Welcome to my page π</p>`
function useProcessor(text) {
const [Content, setContent] = useState(Fragment)
useEffect(() => {
unified()
.use(rehypeParse, {fragment: true})
.use(rehypeReact, {createElement, Fragment})
.process(text)
.then((file) => {
setContent(file.result)
})
}, [text])
return Content
}
export default function App() {
return useProcessor(text)
}
Assuming that runs in Next.js, Create React App (CRA), or similar, weβd get:
<h2>Hello, world!</h2>
<p>Welcome to my page π</p>
API
This package exports no identifiers.
The default export is rehypeReact
.
unified().use(rehypeReact, options)
Compile HTML to React nodes.
π Note: this compiler returns a React node where compilers typically returnstring
. When using.stringify
, the result is such a React node. When using.process
(or.processSync
), the result is available atfile.result
.
options
Configuration (optional).
options.createElement
How to create elements or components (Function
, required).
You should typically pass React.createElement
.
options.Fragment
Create fragments instead of an outer <div>
if available (symbol
).
You should typically pass React.Fragment
.
options.components
Override default elements (such as <a>
, <p>
, etc.) by passing an object
mapping tag names to components (Record<string, Component>
, default: {}
).
For example, to use <MyLink>
components instead of <a>
, and <MyParagraph>
instead of <p>
, so something like this:
// β¦
.use(rehypeReact, {
createElement: React.createElement,
components: {
a: MyLink,
p: MyParagraph
}
})
// β¦
options.prefix
React key prefix (string
, default: 'h-'
).
options.passNode
Pass the original hast node as props.node
to custom React components
(boolean
, default: false
).
options.fixTableCellAlign
Fix obsolete align attributes on table cells by turning them
into inline styles (boolean
, default: true
).
Keep it on when working with markdown, turn it off when working
with markup for emails.
Types
This package is fully typed with TypeScript.
It exports an Options
type, which specifies the interface of the accepted
options.
Compatibility
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.
This plugin works with rehype-parse
version 3+, rehype
version 4+, and
unified
version 9+, and React 16+.
Security
Use of rehype-react
can open you up to a cross-site scripting (XSS)
attack if the tree is unsafe.
Use rehype-sanitize
to make the tree safe.
Related
remark-rehype
β turn markdown into HTML to support rehyperehype-remark
β turn HTML into markdown to support remarkrehype-retext
β rehype plugin to support retextrehype-sanitize
β sanitize HTML
Contribute
See contributing.md
in rehypejs/.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 Β© Titus Wormer, modified by Tom MacWright, Mapbox, and rhysd.