8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** remark** ] [ remark ] plugin to serialize Markdown as HTML.
12
-
13
- > ⚠️ This package essentially packs [ ` remark-rehype ` ] [ remark-rehype ] and
14
- > [ ` rehype-stringify ` ] [ rehype-stringify ] , and although it does support some
15
- > customisation, it isn’t very pluggable.
16
- > It’s probably smarter to use ` remark-rehype ` directly and benefit from the
17
- > [ ** rehype** ] [ rehype ] ecosystem.
11
+ ** [ remark] [ ] ** plugin to add support for serializing HTML.
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(remarkHtml[, options]) ` ] ( #unifieduseremarkhtml-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] [ ] ([ remark] [ ] ) plugin that compiles markdown to
31
+ HTML.
32
+
33
+ ** unified** is a project that transforms content with abstract syntax trees
34
+ (ASTs).
35
+ ** remark** adds support for markdown to unified.
36
+ ** rehype** adds support for HTML to unified.
37
+ ** mdast** is the markdown AST that remark uses.
38
+ ** hast** is the HTML AST that rehype uses.
39
+ This is a remark plugin that adds a compiler to compile mdast to hast and then
40
+ to a string.
41
+
42
+ ## When should I use this?
43
+
44
+ This plugin is useful when you want to turn markdown into HTML.
45
+ It’s a shortcut for ` .use(remarkRehype).use(rehypeStringify) ` .
46
+
47
+ The reason that there are different ecosystems for markdown and HTML is that
48
+ turning markdown into HTML is, while frequently needed, not the only purpose of
49
+ markdown.
50
+ Checking (linting) and formatting markdown are also common use cases for
51
+ remark and markdown.
52
+ There are several aspects of markdown that do not translate 1-to-1 to HTML.
53
+ In some cases markdown contains more information than HTML: for example, there
54
+ are several ways to add a link in markdown (as in, autolinks: ` <https://url> ` ,
55
+ resource links: ` [label](url) ` , and reference links with definitions:
56
+ ` [label][id] ` and ` [id]: url ` ).
57
+ In other cases HTML contains more information than markdown: there are many
58
+ tags, which add new meaning (semantics), available in HTML that aren’t available
59
+ in markdown.
60
+ If there was just one AST, it would be quite hard to perform the tasks that
61
+ several remark and rehype plugins currently do.
62
+
63
+ This plugin is useful when you want to quickly turn markdown into HTML.
64
+ In most cases though, it’s recommended to use [ ` remark-rehype ` ] [ remark-rehype ]
65
+ instead and finally use [ ` rehype-stringify ` ] [ rehype-stringify ] to serialize
66
+ HTML.
67
+ The reason using both ecosystems instead of this plugin is recommended, is that
68
+ there are many useful rehype plugins that you can then use.
69
+ For example, you can [ minify HTML] [ rehype-minify ] , [ format HTML] [ rehype-format ] ,
70
+ [ highlight code] [ rehype-highlight ] , [ add metadata] [ rehype-meta ] , and a lot more.
18
71
19
72
## Install
20
73
21
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
22
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
23
-
24
- [ npm] [ ] :
74
+ This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) .
75
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
25
76
26
77
``` sh
27
78
npm install remark-html
28
79
```
29
80
81
+ In Deno with [ Skypack] [ ] :
82
+
83
+ ``` js
84
+ import remarkHtml from ' https://cdn.skypack.dev/remark-html@15?dts'
85
+ ```
86
+
87
+ In browsers with [ Skypack] [ ] :
88
+
89
+ ``` html
90
+ <script type =" module" >
91
+ import remarkHtml from ' https://cdn.skypack.dev/remark-html@15?min'
92
+ </script >
93
+ ```
94
+
30
95
## Use
31
96
32
- Say we have the following file, ` example.md ` :
97
+ Say we have the following file ` example.md ` :
33
98
34
99
``` markdown
35
100
# Hello & World
@@ -39,26 +104,27 @@ Say we have the following file, `example.md`:
39
104
* Some _emphasis_, **importance**, and `code`.
40
105
```
41
106
42
- And our module, ` example.js ` , looks as follows:
107
+ And our module ` example.js ` looks as follows:
43
108
44
109
``` js
45
- import fs from ' node:fs '
110
+ import { read } from ' to-vfile '
46
111
import {unified } from ' unified'
47
112
import remarkParse from ' remark-parse'
48
113
import remarkHtml from ' remark-html'
49
114
50
- const buf = fs . readFileSync ( ' example.md ' )
115
+ main ( )
51
116
52
- unified ()
53
- .use (remarkParse)
54
- .use (remarkHtml)
55
- .process (buf)
56
- .then ((file ) => {
57
- console .log (String (file))
58
- })
117
+ async function main () {
118
+ const file = await unified ()
119
+ .use (remarkParse)
120
+ .use (remarkHtml)
121
+ .process (await read (' example.md' ))
122
+
123
+ console .log (String (file))
124
+ }
59
125
```
60
126
61
- Now, running ` node example ` yields:
127
+ Now running ` node example.js ` yields:
62
128
63
129
``` html
64
130
<h1 >Hello & ; World</h1 >
@@ -77,61 +143,61 @@ The default export is `remarkHtml`.
77
143
78
144
### ` unified().use(remarkHtml[, options]) `
79
145
80
- Serialize Markdown as HTML.
146
+ Add support for serializing HTML.
81
147
82
148
##### ` options `
83
149
84
- All options except for ` sanitize ` and ` handlers ` are passed to
85
- [ ` hast-util-to-html ` ] [ to-html ] .
86
-
87
- The underlying tools allow much more customisation.
88
- It is recommended to replace this project with [ ` remark-rehype ` ] [ remark-rehype ]
89
- and [ ` rehype-stringify ` ] [ rehype-stringify ] ;
150
+ Configuration (optional).
151
+ All options other than ` sanitize ` and ` handlers ` are passed to
152
+ [ ` hast-util-to-html ` ] [ hast-util-to-html ] .
90
153
91
154
###### ` options.handlers `
92
155
93
- Object mapping [ mdast] [ ] [ nodes] [ mdast-node ] to functions handling them.
94
- This option is passed to [ ` mdast-util-to-hast ` ] [ to-hast-handlers ] .
156
+ This option is a bit advanced as it requires knowledge of ASTs, so we defer
157
+ to the documentation available in
158
+ [ ` mdast-util-to-hast ` ] [ mdast-util-to-hast ] .
95
159
96
160
###### ` options.sanitize `
97
161
98
162
How to sanitize the output (` Object ` or ` boolean ` , default: ` true ` ):
99
163
100
164
* ` false `
101
- — HTML is not sanitized, dangerous HTML persists
165
+ — output is not sanitized, dangerous raw HTML persists
102
166
* ` true `
103
- — HTML is sanitized according to [ GitHub’s sanitation rules] [ github ] ,
104
- dangerous HTML is dropped
167
+ — output is sanitized according to [ GitHub’s sanitation rules] [ github ] ,
168
+ dangerous raw HTML is dropped
105
169
* ` Object `
106
- — the object is treated as a ` schema ` for how to sanitize with
107
- [ ` hast-util-sanitize ` ] [ sanitize ] , dangerous HTML is dropped
170
+ — ` schema ` that defines how to sanitize output with
171
+ [ ` hast-util-sanitize ` ] [ sanitize ] , dangerous raw HTML is dropped
108
172
109
- > Note that raw HTML in Markdown cannot be sanitized, so it’s removed.
110
- > A schema can still be used to allow certain values from other plugins
111
- > though.
112
- > To support HTML in Markdown, use [ ` rehype-raw ` ] [ raw ] .
173
+ ## Types
113
174
114
- For example, to add strict sanitation but allowing ` className ` s, use something
115
- like:
175
+ This package is fully typed with [ TypeScript] [ ] .
176
+ It exports an ` Options ` type, which specifies the interface of the accepted
177
+ options.
116
178
117
- ``` js
118
- // ...
119
- var merge = require (' deepmerge' )
120
- var github = require (' hast-util-sanitize/lib/github' )
179
+ ## Compatibility
121
180
122
- var schema = merge (github, {attributes: {' *' : [' className' ]}})
181
+ Projects maintained by the unified collective are compatible with all maintained
182
+ versions of Node.js.
183
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
184
+ Our projects sometimes work with older versions, but this is not guaranteed.
123
185
124
- remark ()
125
- .use (html, {sanitize: schema})
126
- .processSync (/* … */ )
127
- ```
186
+ This plugin works with ` unified ` version 6+ and ` remark ` version 7+.
128
187
129
188
## Security
130
189
131
- Use of ` remark-html ` is * unsafe* by default and opens you up to a
132
- [ cross-site scripting (XSS)] [ xss ] attack .
190
+ Use of ` remark-html ` is ** unsafe** by default and opens you up to
191
+ [ cross-site scripting (XSS)] [ xss ] attacks .
133
192
Pass ` sanitize: true ` to prevent attacks.
134
- Settings ` sanitize ` to anything else may be unsafe.
193
+ Setting ` sanitize ` to anything else can be unsafe.
194
+
195
+ ## Related
196
+
197
+ * [ ` remark-rehype ` ] ( https://github.com/remarkjs/remark-rehype )
198
+ — turn markdown into HTML to support rehype
199
+ * [ ` rehype-sanitize ` ] ( https://github.com/rehypejs/rehype-sanitize )
200
+ — sanitize HTML
135
201
136
202
## Contribute
137
203
@@ -177,38 +243,44 @@ abide by its terms.
177
243
178
244
[ npm ] : https://docs.npmjs.com/cli/install
179
245
246
+ [ skypack ] : https://www.skypack.dev
247
+
180
248
[ health ] : https://github.com/remarkjs/.github
181
249
182
- [ contributing ] : https://github.com/remarkjs/.github/blob/HEAD /contributing.md
250
+ [ contributing ] : https://github.com/remarkjs/.github/blob/main /contributing.md
183
251
184
- [ support ] : https://github.com/remarkjs/.github/blob/HEAD /support.md
252
+ [ support ] : https://github.com/remarkjs/.github/blob/main /support.md
185
253
186
- [ coc ] : https://github.com/remarkjs/.github/blob/HEAD /code-of-conduct.md
254
+ [ coc ] : https://github.com/remarkjs/.github/blob/main /code-of-conduct.md
187
255
188
256
[ license ] : license
189
257
190
258
[ author ] : https://wooorm.com
191
259
260
+ [ unified ] : https://github.com/unifiedjs/unified
261
+
192
262
[ remark ] : https://github.com/remarkjs/remark
193
263
194
- [ remark-rehype ] : https://github.com/remarkjs/remark-rehype
264
+ [ github ] : https://github.com/syntax-tree/hast-util-sanitize#schema
265
+
266
+ [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
195
267
196
- [ rehype ] : https://github.com/rehypejs/rehype
268
+ [ typescript ] : https://www.typescriptlang.org
197
269
198
- [ rehype-stringify ] : https://github.com/rehypejs/rehype/tree/HEAD/packages/rehype-stringify
270
+ [ remark-rehype ] : https://github.com/remarkjs/remark-rehype
199
271
200
- [ raw ] : https://github.com/rehypejs/rehype-raw
272
+ [ rehype-minify ] : https://github.com/rehypejs/rehype-minify
201
273
202
- [ mdast ] : https://github.com/syntax-tree/mdast
274
+ [ rehype-format ] : https://github.com/rehypejs/rehype-format
203
275
204
- [ mdast-node ] : https://github.com/syntax-tree/mdast#nodes
276
+ [ rehype-highlight ] : https://github.com/rehypejs/rehype-highlight
205
277
206
- [ to-html ] : https://github.com/syntax-tree/hast-util-to-html
278
+ [ rehype-meta ] : https://github.com/rehypejs/rehype-meta
207
279
208
- [ to-hast-handlers ] : https://github.com/syntax- tree/mdast-util-to-hast#optionshandlers
280
+ [ rehype-stringify ] : https://github.com/rehypejs/rehype/ tree/main/packages/rehype-stringify
209
281
210
282
[ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
211
283
212
- [ github ] : https://github.com/syntax-tree/hast-util-sanitize#schema
284
+ [ hast-util-to-html ] : https://github.com/syntax-tree/hast-util-to-html
213
285
214
- [ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
286
+ [ mdast-util-to-hast ] : https://github.com/syntax-tree/mdast-util-to-hast
0 commit comments