Skip to content

Commit 21753da

Browse files
authoredJan 6, 2022
Add improved docs
Closes GH-37. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Merlijn Vos <merlijn@soverin.net>
1 parent 276884f commit 21753da

File tree

1 file changed

+127
-74
lines changed

1 file changed

+127
-74
lines changed
 

‎readme.md

+127-74
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,112 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**rehype**][rehype] plugin to transform to [**React**][react].
11+
**[rehype][]** plugin to compile HTML to React nodes.
12+
13+
## Contents
14+
15+
* [What is this?](#what-is-this)
16+
* [When should I use this?](#when-should-i-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`unified().use(rehypeReact, options)`](#unifieduserehypereact-options)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Security](#security)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
27+
28+
## What is this?
29+
30+
This package is a [unified][] ([rehype][]) plugin that compiles HTML (hast) to
31+
React nodes (the virtual DOM that React uses).
32+
33+
**unified** is a project that transforms content with abstract syntax trees
34+
(ASTs).
35+
**rehype** adds support for HTML to unified.
36+
**hast** is the HTML AST that rehype uses.
37+
This is a rehype plugin that adds a compiler to compile hast to React nodes.
38+
39+
## When should I use this?
40+
41+
This plugin adds a compiler for rehype, which means that it turns the final
42+
HTML (hast) syntax tree into something else (in this case, a React node).
43+
It’s useful when you’re already using unified (whether remark or rehype) or are
44+
open to learning about ASTs (they’re powerful!) and want to render content in
45+
your React app.
46+
47+
If you’re not familiar with unified, then [`react-markdown`][react-markdown]
48+
might be a better fit.
49+
You can also use [`react-remark`][react-remark] instead, which is somewhere
50+
between `rehype-react` and `react-markdown`, as it does more that the former and
51+
is more modern (such as supporting hooks) than the latter, and also a good
52+
alternative.
53+
If you want to use JavaScript and JSX *inside* markdown files, use [MDX][].
1254

1355
## Install
1456

15-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17-
18-
[npm][]:
57+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
58+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
1959

2060
```sh
2161
npm install rehype-react
2262
```
2363

64+
In Deno with [Skypack][]:
65+
66+
```js
67+
import rehypeReact from 'https://cdn.skypack.dev/rehype-react@7?dts'
68+
```
69+
70+
In browsers with [Skypack][]:
71+
72+
```html
73+
<script type="module">
74+
import rehypeReact from 'https://cdn.skypack.dev/rehype-react@7?min'
75+
</script>
76+
```
77+
2478
## Use
2579

26-
The following example shows how to create a markdown input textarea, and
27-
corresponding rendered HTML output.
28-
The Markdown is processed to add a Table of Contents, highlight code blocks, and
29-
to render GitHub mentions (and other cool GH features).
80+
Say our React app module `example.js` looks as follows:
3081

3182
```js
32-
import React from 'react'
33-
import ReactDOM from 'react-dom'
83+
import {createElement, Fragment, useEffect, useState} from 'react'
3484
import {unified} from 'unified'
35-
import remarkParse from 'remark-parse'
36-
import remarkSlug from 'remark-slug'
37-
import remarkToc from 'remark-toc'
38-
import remarkGithub from 'remark-github'
39-
import remarkRehype from 'remark-rehype'
40-
import rehypeHighlight from 'rehype-highlight'
85+
import rehypeParse from 'rehype-parse'
4186
import rehypeReact from 'rehype-react'
4287

43-
const processor = unified()
44-
.use(remarkParse)
45-
.use(remarkSlug)
46-
.use(remarkToc)
47-
.use(remarkGithub, {repository: 'rehypejs/rehype-react'})
48-
.use(remarkRehype)
49-
.use(rehypeHighlight)
50-
.use(rehypeReact, {createElement: React.createElement})
51-
52-
class App extends React.Component {
53-
constructor() {
54-
super()
55-
this.state = {text: '# Hello\n\n## Table of Contents\n\n## @rhysd'}
56-
this.onChange = this.onChange.bind(this)
57-
}
58-
59-
onChange(ev) {
60-
this.setState({text: ev.target.value})
61-
}
62-
63-
render() {
64-
return (
65-
<div>
66-
<textarea value={this.state.text} onChange={this.onChange} />
67-
<div id="preview">{processor.processSync(this.state.text).result}</div>
68-
</div>
69-
)
70-
}
88+
const text = `<h2>Hello, world!</h2>
89+
<p>Welcome to my page 👀</p>`
90+
91+
function useProcessor(text) {
92+
const [Content, setContent] = useState(Fragment)
93+
94+
useEffect(() => {
95+
unified()
96+
.use(rehypeParse, {fragment: true})
97+
.use(rehypeReact, {createElement, Fragment})
98+
.process(text)
99+
.then((file) => {
100+
setContent(file.result)
101+
})
102+
}, [text])
103+
104+
return Content
71105
}
72106

73-
ReactDOM.render(<App />, document.querySelector('#root'))
107+
export default function App() {
108+
return useProcessor(text)
109+
}
74110
```
75111

76-
Yields (in `id="preview"`, on first render):
112+
Assuming that runs in Next.js, Create React App (CRA), or similar, we’d get:
77113

78114
```html
79-
<div><h1 id="hello">Hello</h1>
80-
<h2 id="table-of-contents">Table of Contents</h2>
81-
<ul>
82-
<li><a href="#rhysd">@rhysd</a></li>
83-
</ul>
84-
<h2 id="rhysd"><a href="https://github.com/rhysd"><strong>@rhysd</strong></a></h2></div>
115+
<h2>Hello, world!</h2>
116+
<p>Welcome to my page 👀</p>
85117
```
86118

87119
## API
@@ -91,15 +123,18 @@ The default export is `rehypeReact`.
91123

92124
### `unified().use(rehypeReact, options)`
93125

94-
[**rehype**][rehype] ([hast][]) plugin to transform to [**React**][react].
126+
Compile HTML to React nodes.
95127

96-
Typically, [**unified**][unified] compilers return `string`.
97-
This compiler returns a `ReactElement` (or similar).
98-
When using `.process` or `.processSync`, the value at `file.result` (or when
99-
using `.stringify`, the return value), is such a custom node.
128+
> 👉 **Note**: this compiler returns a React node where compilers typically
129+
> return `string`.
130+
> When using `.stringify`, the result is such a React node.
131+
> When using `.process` (or `.processSync`), the result is available at
132+
> `file.result`.
100133
101134
##### `options`
102135

136+
Configuration (optional).
137+
103138
###### `options.createElement`
104139

105140
How to create elements or components (`Function`, required).
@@ -112,15 +147,15 @@ You should typically pass `React.Fragment`.
112147

113148
###### `options.components`
114149

115-
Override default elements (such as `<a>`, `<p>`, etcetera) by passing an object
150+
Override default elements (such as `<a>`, `<p>`, etc.) by passing an object
116151
mapping tag names to components (`Record<string, Component>`, default: `{}`).
117152

118153
For example, to use `<MyLink>` components instead of `<a>`, and `<MyParagraph>`
119154
instead of `<p>`, so something like this:
120155

121156
```js
122157
//
123-
.use(rehype2react, {
158+
.use(rehypeReact, {
124159
createElement: React.createElement,
125160
components: {
126161
a: MyLink,
@@ -139,22 +174,38 @@ React key prefix (`string`, default: `'h-'`).
139174
Pass the original hast node as `props.node` to custom React components
140175
(`boolean`, default: `false`).
141176

177+
## Types
178+
179+
This package is fully typed with [TypeScript][].
180+
It exports an `Options` type, which specifies the interface of the accepted
181+
options.
182+
183+
## Compatibility
184+
185+
Projects maintained by the unified collective are compatible with all maintained
186+
versions of Node.js.
187+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
188+
Our projects sometimes work with older versions, but this is not guaranteed.
189+
190+
This plugin works with `rehype-parse` version 3+, `rehype` version 4+, and
191+
`unified` version 9+, and React 16+.
192+
142193
## Security
143194

144195
Use of `rehype-react` can open you up to a [cross-site scripting (XSS)][xss]
145196
attack if the tree is unsafe.
146-
Use [`rehype-sanitize`][sanitize] to make the tree safe.
197+
Use [`rehype-sanitize`][rehype-sanitize] to make the tree safe.
147198

148199
## Related
149200

150201
* [`remark-rehype`](https://github.com/remarkjs/remark-rehype)
151-
— Transform Markdown ([**mdast**][mdast]) to HTML ([**hast**][hast])
152-
* [`rehype-retext`](https://github.com/rehypejs/rehype-retext)
153-
— Transform HTML ([**hast**][hast]) to natural language ([**nlcst**][nlcst])
202+
— turn markdown into HTML to support rehype
154203
* [`rehype-remark`](https://github.com/rehypejs/rehype-remark)
155-
— Transform HTML ([**hast**][hast]) to Markdown ([**mdast**][mdast])
156-
* [`rehype-sanitize`][sanitize]
157-
— Sanitize HTML
204+
— turn HTML into markdown to support remark
205+
* [`rehype-retext`](https://github.com/rehypejs/rehype-retext)
206+
— rehype plugin to support retext
207+
* [`rehype-sanitize`][rehype-sanitize]
208+
— sanitize HTML
158209

159210
## Contribute
160211

@@ -201,6 +252,8 @@ abide by its terms.
201252

202253
[npm]: https://docs.npmjs.com/cli/install
203254

255+
[skypack]: https://www.skypack.dev
256+
204257
[health]: https://github.com/rehypejs/.github
205258

206259
[contributing]: https://github.com/rehypejs/.github/blob/HEAD/contributing.md
@@ -219,18 +272,18 @@ abide by its terms.
219272

220273
[rhysd]: https://rhysd.github.io
221274

275+
[typescript]: https://www.typescriptlang.org
276+
222277
[unified]: https://github.com/unifiedjs/unified
223278

224279
[rehype]: https://github.com/rehypejs/rehype
225280

226-
[mdast]: https://github.com/syntax-tree/mdast
227-
228-
[hast]: https://github.com/syntax-tree/hast
281+
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
229282

230-
[nlcst]: https://github.com/syntax-tree/nlcst
283+
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize
231284

232-
[react]: https://github.com/facebook/react
285+
[react-markdown]: https://github.com/remarkjs/react-markdown
233286

234-
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
287+
[react-remark]: https://github.com/remarkjs/react-remark
235288

236-
[sanitize]: https://github.com/rehypejs/rehype-sanitize
289+
[mdx]: https://github.com/mdx-js/mdx/

0 commit comments

Comments
 (0)
Please sign in to comment.