
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
rehype-react
Advanced tools
rehype plugin to transform to React.
npm:
npm install rehype-react
The following example shows how to create a Markdown input textarea, and corresponding rendered HTML output. The Markdown is processed to add a Table of Contents, highlight code blocks, and to render GitHub mentions (and other cool GH features).
import React from 'react'
import ReactDOM from 'react-dom'
import unified from 'unified'
import markdown from 'remark-parse'
import slug from 'remark-slug'
import toc from 'remark-toc'
import github from 'remark-github'
import remark2rehype from 'remark-rehype'
import highlight from 'rehype-highlight'
import rehype2react from 'rehype-react'
var processor = unified()
.use(markdown)
.use(slug)
.use(toc)
.use(github, {repository: 'rehypejs/rehype-react'})
.use(remark2rehype)
.use(highlight)
.use(rehype2react, {createElement: React.createElement})
class App extends React.Component {
constructor() {
super()
this.state = {text: '# Hello\n\n## Table of Contents\n\n## @rhysd'}
this.onChange = this.onChange.bind(this)
}
onChange(ev) {
this.setState({text: ev.target.value})
}
render() {
return (
<div>
<textarea value={this.state.text} onChange={this.onChange} />
<div id="preview">
{processor.processSync(this.state.text).result}
</div>
</div>
)
}
}
ReactDOM.render(<App />, document.querySelector('#root'))
Yields (in id="preview", on first render):
<div><h1 id="hello">Hello</h1>
<h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li><a href="#rhysd">@rhysd</a></li>
</ul>
<h2 id="rhysd"><a href="https://github.com/rhysd"><strong>@rhysd</strong></a></h2></div>
origin.use(rehype2react[, options])rehype (hast) plugin to transform to React.
Typically, unified compilers return string.
This compiler returns a ReactElement.
When using .process or .processSync, the value at file.result (or when
using .stringify, the return value), is a ReactElement.
When using TypeScript, cast the type on your side.
ℹ️ In
unified@9.0.0, the result of.processchanged fromtofile.contentsfile.result.
optionsoptions.createElementHow to create elements or components (Function).
You should typically pass React.createElement.
options.FragmentCreate fragments instead of an outer <div> if available (symbol).
You should typically pass React.Fragment.
options.componentsOverride default elements (such as <a>, <p>, etcetera) by passing an object
mapping tag names to components (Object.<Component>, default: {}).
For example, to use <MyLink> components instead of <a>, and <MyParagraph>
instead of <p>, so something like this:
// …
.use(rehype2react, {
createElement: React.createElement,
components: {
a: MyLink,
p: MyParagraph
}
})
// …
options.prefixReact key prefix (string, default: 'h-').
options.passNodePass the original hast node as props.node to custom React components
(boolean, default: false).
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.
remark-rehype
— Transform Markdown (mdast) to HTML (hast)rehype-retext
— Transform HTML (hast) to natural language (nlcst)rehype-remark
— Transform HTML (hast) to Markdown (mdast)rehype-sanitize
— Sanitize HTMLSee 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.
MIT © Titus Wormer, modified by Tom MacWright, Mapbox, and rhysd.
html-react-parser is a library that converts HTML strings into React components. It is similar to rehype-react in that it allows you to render HTML content within React applications. However, html-react-parser is more straightforward and does not require the use of the unified processor.
react-html-parser is another library for converting HTML strings into React components. It is similar to rehype-react but offers a simpler API. It does not provide the same level of customization and extensibility as rehype-react, but it is easier to use for basic HTML to React transformations.
FAQs
rehype plugin to transform to React
The npm package rehype-react receives a total of 198,505 weekly downloads. As such, rehype-react popularity was classified as popular.
We found that rehype-react demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.