8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
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] [ ] .
12
54
13
55
## Install
14
56
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] [ ] :
19
59
20
60
``` sh
21
61
npm install rehype-react
22
62
```
23
63
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
+
24
78
## Use
25
79
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:
30
81
31
82
``` js
32
- import React from ' react'
33
- import ReactDOM from ' react-dom'
83
+ import {createElement , Fragment , useEffect , useState } from ' react'
34
84
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'
41
86
import rehypeReact from ' rehype-react'
42
87
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
71
105
}
72
106
73
- ReactDOM .render (< App / > , document .querySelector (' #root' ))
107
+ export default function App () {
108
+ return useProcessor (text)
109
+ }
74
110
```
75
111
76
- Yields ( in ` id="preview" ` , on first render) :
112
+ Assuming that runs in Next.js, Create React App (CRA), or similar, we’d get :
77
113
78
114
``` 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 >
85
117
```
86
118
87
119
## API
@@ -91,15 +123,18 @@ The default export is `rehypeReact`.
91
123
92
124
### ` unified().use(rehypeReact, options) `
93
125
94
- [ ** rehype ** ] [ rehype ] ( [ hast ] [ ] ) plugin to transform to [ ** React** ] [ react ] .
126
+ Compile HTML to React nodes .
95
127
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 ` .
100
133
101
134
##### ` options `
102
135
136
+ Configuration (optional).
137
+
103
138
###### ` options.createElement `
104
139
105
140
How to create elements or components (` Function ` , required).
@@ -112,15 +147,15 @@ You should typically pass `React.Fragment`.
112
147
113
148
###### ` options.components `
114
149
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
116
151
mapping tag names to components (` Record<string, Component> ` , default: ` {} ` ).
117
152
118
153
For example, to use ` <MyLink> ` components instead of ` <a> ` , and ` <MyParagraph> `
119
154
instead of ` <p> ` , so something like this:
120
155
121
156
``` js
122
157
// …
123
- .use (rehype2react , {
158
+ .use (rehypeReact , {
124
159
createElement: React .createElement ,
125
160
components: {
126
161
a: MyLink,
@@ -139,22 +174,38 @@ React key prefix (`string`, default: `'h-'`).
139
174
Pass the original hast node as ` props.node ` to custom React components
140
175
(` boolean ` , default: ` false ` ).
141
176
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
+
142
193
## Security
143
194
144
195
Use of ` rehype-react ` can open you up to a [ cross-site scripting (XSS)] [ xss ]
145
196
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.
147
198
148
199
## Related
149
200
150
201
* [ ` 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
154
203
* [ ` 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
158
209
159
210
## Contribute
160
211
@@ -201,6 +252,8 @@ abide by its terms.
201
252
202
253
[ npm ] : https://docs.npmjs.com/cli/install
203
254
255
+ [ skypack ] : https://www.skypack.dev
256
+
204
257
[ health ] : https://github.com/rehypejs/.github
205
258
206
259
[ contributing ] : https://github.com/rehypejs/.github/blob/HEAD/contributing.md
@@ -219,18 +272,18 @@ abide by its terms.
219
272
220
273
[ rhysd ] : https://rhysd.github.io
221
274
275
+ [ typescript ] : https://www.typescriptlang.org
276
+
222
277
[ unified ] : https://github.com/unifiedjs/unified
223
278
224
279
[ rehype ] : https://github.com/rehypejs/rehype
225
280
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
229
282
230
- [ nlcst ] : https://github.com/syntax-tree/nlcst
283
+ [ rehype-sanitize ] : https://github.com/rehypejs/rehype-sanitize
231
284
232
- [ react ] : https://github.com/facebook /react
285
+ [ react-markdown ] : https://github.com/remarkjs /react-markdown
233
286
234
- [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
287
+ [ react-remark ] : https://github.com/remarkjs/react-remark
235
288
236
- [ sanitize ] : https://github.com/rehypejs/rehype-sanitize
289
+ [ mdx ] : https://github.com/mdx-js/mdx/
0 commit comments