8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- ** [ mdast] [ ] ** utility to parse markdown.
11
+ ** [ mdast] [ ] ** utility that turns a syntax tree into markdown.
12
12
13
- ## When to use this
13
+ ## Contents
14
14
15
- Use this if you have direct access to an AST and need to serialize it.
16
- Use ** [ remark] [ ] ** instead, which includes this, but has a nice interface and
17
- hundreds of plugins.
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
+ * [ ` toMarkdown(tree[, options]) ` ] ( #tomarkdowntree-options )
21
+ * [ List of extensions] ( #list-of-extensions )
22
+ * [ Syntax] ( #syntax )
23
+ * [ Syntax tree] ( #syntax-tree )
24
+ * [ Types] ( #types )
25
+ * [ Security] ( #security )
26
+ * [ Related] ( #related )
27
+ * [ Contribute] ( #contribute )
28
+ * [ License] ( #license )
18
29
19
- ## Install
30
+ ## What is this?
31
+
32
+ This package is a utility that takes an [ mdast] [ ] syntax tree as input and turns
33
+ it into serialized markdown.
34
+
35
+ This utility is a low level project.
36
+ It’s used in [ ` remark-stringify ` ] [ remark-stringify ] , which focusses on making it
37
+ easier to transform content by abstracting these internals away.
38
+
39
+ ## When should I use this?
20
40
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 .
41
+ If you want to handle syntax trees manually, use this.
42
+ For an easier time processing content, use the ** [ remark ] [ ] ** ecosystem instead.
23
43
24
- [ npm] [ ] :
44
+ ## Install
45
+
46
+ This package is [ ESM only] [ esm ] .
47
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
25
48
26
49
``` sh
27
50
npm install mdast-util-to-markdown
28
51
```
29
52
53
+ In Deno with [ Skypack] [ ] :
54
+
55
+ ``` js
56
+ import {toMarkdown } from ' https://cdn.skypack.dev/mdast-util-to-markdown@1?dts'
57
+ ```
58
+
59
+ In browsers with [ Skypack] [ ] :
60
+
61
+ ``` html
62
+ <script type =" module" >
63
+ import {toMarkdown } from ' https://cdn.skypack.dev/mdast-util-to-markdown@1?min'
64
+ </script >
65
+ ```
66
+
30
67
## Use
31
68
32
- Say we have the following script, ` example.js ` :
69
+ Say our module ` example.js ` looks as follows :
33
70
34
71
``` js
35
72
import {toMarkdown } from ' mdast-util-to-markdown'
36
73
74
+ /** @type {import('mdast').Root} */
37
75
const tree = {
38
76
type: ' root' ,
39
77
children: [
@@ -60,8 +98,7 @@ const tree = {
60
98
console .log (toMarkdown (tree))
61
99
```
62
100
63
- Now, running ` node example ` yields (note the properly escaped characters which
64
- would otherwise turn into a list and image respectively):
101
+ …now running ` node example.js ` yields:
65
102
66
103
``` markdown
67
104
> ***
@@ -70,14 +107,17 @@ would otherwise turn into a list and image respectively):
70
107
> b \
71
108
```
72
109
110
+ > 👉 ** Note** : observe the properly escaped characters which would otherwise
111
+ > turn into a list and image respectively.
112
+
73
113
## API
74
114
75
115
This package exports the following identifier: ` toMarkdown ` .
76
116
There is no default export.
77
117
78
118
### ` toMarkdown(tree[, options]) `
79
119
80
- Serialize ** [ mdast] [ ] ** to markdown.
120
+ Turn an ** [ mdast] [ ] ** syntax tree into markdown.
81
121
82
122
##### Formatting options
83
123
@@ -138,48 +178,48 @@ heading as the opening sequence (`boolean`, default: `false`).
138
178
139
179
###### ` options.emphasis `
140
180
141
- Marker to use to serialize emphasis (` '*' ` or ` '_' ` , default: ` '*' ` ).
181
+ Marker to use for emphasis (` '*' ` or ` '_' ` , default: ` '*' ` ).
142
182
143
183
###### ` options.fence `
144
184
145
- Marker to use to serialize fenced code (`` '`' `` or ` '~' ` , default: `` '`' `` ).
185
+ Marker to use for fenced code (`` '`' `` or ` '~' ` , default: `` '`' `` ).
146
186
147
187
###### ` options.fences `
148
188
149
189
Whether to use fenced code always (` boolean ` , default: ` false ` ).
150
- The default is to fenced code if there is a language defined, if the code is
151
- empty, or if it starts or ends in empty lines.
190
+ The default is to use fenced code if there is a language defined, if the code is
191
+ empty, or if it starts or ends in blank lines.
152
192
153
193
###### ` options.incrementListMarker `
154
194
155
- Whether to increment the value of bullets of items in ordered lists (` boolean ` ,
156
- default: ` true ` ).
195
+ Whether to increment the counter of ordered lists items (` boolean ` , default:
196
+ ` true ` ).
157
197
158
198
###### ` options.listItemIndent `
159
199
160
- Whether to indent the content of list items with the size of the bullet plus one
161
- space (when ` 'one' ` ) or a tab stop (` 'tab' ` ), or depending on the item and its
162
- parent list (` 'mixed' ` , uses ` 'one' ` if the item and list are tight and ` 'tab' `
163
- otherwise) (` 'one' ` , ` 'tab' ` , or ` 'mixed' ` , default: ` 'tab' ` ).
200
+ How to indent the content of list items (` 'one' ` , ` 'tab' ` , or ` 'mixed' ` ,
201
+ default: ` 'tab' ` ).
202
+ Either with the size of the bullet plus one space (when ` 'one' ` ), a tab stop
203
+ (` 'tab' ` ), or depending on the item and its parent list (` 'mixed' ` , uses ` 'one' `
204
+ if the item and list are tight and ` 'tab' ` otherwise).
164
205
165
206
###### ` options.quote `
166
207
167
- Marker to use to serialize titles (` '"' ` or ` "'" ` , default: ` '"' ` ).
208
+ Marker to use for titles (` '"' ` or ` "'" ` , default: ` '"' ` ).
168
209
169
210
###### ` options.resourceLink `
170
211
171
- Whether to use resource links (` [text](url) ` ) always ( ` boolean ` , default:
172
- ` false ` ).
173
- The default is to use autolinks ( ` <https://example.com> ` ) when possible .
212
+ Whether to always use resource links (` boolean ` , default: ` false ` ).
213
+ The default is to use autolinks ( ` <https://example.com> ` ) when possible
214
+ and resource links ( ` [text](url) ` ) otherwise .
174
215
175
216
###### ` options.rule `
176
217
177
218
Marker to use for thematic breaks (` '*' ` , ` '-' ` , or ` '_' ` , default: ` '*' ` ).
178
219
179
220
###### ` options.ruleRepetition `
180
221
181
- Number of markers to use for thematic breaks (` number ` , default:
182
- ` 3 ` , min: ` 3 ` ).
222
+ Number of markers to use for thematic breaks (` number ` , default: ` 3 ` , min: ` 3 ` ).
183
223
184
224
###### ` options.ruleSpaces `
185
225
@@ -189,20 +229,23 @@ Whether to add spaces between markers in thematic breaks (`boolean`, default:
189
229
###### ` options.setext `
190
230
191
231
Whether to use setext headings when possible (` boolean ` , default: ` false ` ).
192
- Setext headings are not possible for headings with a rank more than 2 or when
193
- they’re empty.
232
+ The default is to always use ATX headings (` # heading ` ) instead of setext
233
+ headings (` heading\n======= ` ).
234
+ Setext headings can’t be used for empty headings or headings with a rank of
235
+ three or more.
194
236
195
237
###### ` options.strong `
196
238
197
- Marker to use to serialize strong (` '*' ` or ` '_' ` , default: ` '*' ` ).
239
+ Marker to use for strong (` '*' ` or ` '_' ` , default: ` '*' ` ).
198
240
199
241
###### ` options.tightDefinitions `
200
242
201
- Whether to join definitions w/o a blank line (` boolean ` , default: ` false ` ).
202
- Shortcut for a join function like so:
243
+ Whether to join definitions without a blank line (` boolean ` , default: ` false ` ).
244
+ The default is to add blank lines between any flow (“block”) construct.
245
+ Turning this option on is a shortcut for a join function like so:
203
246
204
247
``` js
205
- function (left , right ) {
248
+ function joinTightDefinitions (left , right ) {
206
249
if (left .type === ' definition' && right .type === ' definition' ) {
207
250
return 0
208
251
}
@@ -211,18 +254,23 @@ function (left, right) {
211
254
212
255
###### ` options.handlers `
213
256
214
- Object mapping node types to custom handlers.
257
+ Object mapping node types to custom handlers (` Record<string, Handle> ` , default:
258
+ ` {} ` ).
215
259
Useful for syntax extensions.
216
- Take a look at [ ` lib/handle ` ] [ handlers ] for examples.
260
+
261
+ This option is a bit advanced.
262
+ It’s recommended to look at the code in [ ` lib/handle/ ` ] [ handlers ] for examples.
217
263
218
264
###### ` options.join `
219
265
220
- List of functions used to determine what to place between two flow nodes.
221
- Often, they are joined by one blank line.
222
- In certain cases, it’s nicer to have them next to each other.
223
- Or, they can’t occur together.
224
- These functions receive two adjacent nodes and their parent and can return
225
- ` number ` or ` boolean ` , referring to how many blank lines to use between them.
266
+ List of functions used to determine what to place between two flow nodes
267
+ (` Array<Join> ` , default: ` [] ` ).
268
+
269
+ “Blocks” are typically joined by one blank line.
270
+ Sometimes it’s nicer to have them flush next to each other, yet other times
271
+ they can’t occur together at all.
272
+ Join functions receive two adjacent siblings and their parent and can return
273
+ ` number ` or ` boolean ` , to signal how many blank lines to use between them.
226
274
A return value of ` true ` is as passing ` 1 ` .
227
275
A return value of ` false ` means the nodes cannot be joined by a blank line, such
228
276
as two adjacent block quotes or indented code after a list, in which case a
@@ -238,50 +286,69 @@ comment will be injected to break them up:
238
286
239
287
###### ` options.unsafe `
240
288
241
- List of patterns to escape.
289
+ List of patterns to escape ( ` Array<Unsafe> ` ) .
242
290
Useful for syntax extensions.
243
- Take a look at [ ` lib/unsafe.js ` ] [ unsafe ] for examples.
291
+
292
+ This option is quite advanced.
293
+ It’s recommended to look at the code in [ ` lib/unsafe.js ` ] [ unsafe ] for examples.
244
294
245
295
##### Extension options
246
296
247
297
###### ` options.extensions `
248
298
249
- List of extensions (` Array<ToMarkdownExtension> ` ).
299
+ List of extensions (` Array<ToMarkdownExtension> ` , default: ` [] ` ).
250
300
Each ` ToMarkdownExtension ` is an object with the same interface as ` options `
251
301
here.
252
302
253
303
##### Returns
254
304
255
- ` string ` — Serialized markdown.
305
+ Serialized markdown ( ` string ` ) .
256
306
257
307
## List of extensions
258
308
259
309
* [ ` syntax-tree/mdast-util-directive ` ] ( https://github.com/syntax-tree/mdast-util-directive )
260
- — serialize directives
310
+ — directives
261
311
* [ ` syntax-tree/mdast-util-frontmatter ` ] ( https://github.com/syntax-tree/mdast-util-frontmatter )
262
- — serialize frontmatter (YAML, TOML, more)
312
+ — frontmatter (YAML, TOML, more)
263
313
* [ ` syntax-tree/mdast-util-gfm ` ] ( https://github.com/syntax-tree/mdast-util-gfm )
264
- — serialize GFM
314
+ — GFM
265
315
* [ ` syntax-tree/mdast-util-gfm-autolink-literal ` ] ( https://github.com/syntax-tree/mdast-util-gfm-autolink-literal )
266
- — serialize GFM autolink literals
316
+ — GFM autolink literals
267
317
* [ ` syntax-tree/mdast-util-gfm-footnote ` ] ( https://github.com/syntax-tree/mdast-util-gfm-footnote )
268
- — serialize GFM footnotes
318
+ — GFM footnotes
269
319
* [ ` syntax-tree/mdast-util-gfm-strikethrough ` ] ( https://github.com/syntax-tree/mdast-util-gfm-strikethrough )
270
- — serialize GFM strikethrough
320
+ — GFM strikethrough
271
321
* [ ` syntax-tree/mdast-util-gfm-table ` ] ( https://github.com/syntax-tree/mdast-util-gfm-table )
272
- — serialize GFM tables
322
+ — GFM tables
273
323
* [ ` syntax-tree/mdast-util-gfm-task-list-item ` ] ( https://github.com/syntax-tree/mdast-util-gfm-task-list-item )
274
- — serialize GFM task list items
324
+ — GFM task list items
275
325
* [ ` syntax-tree/mdast-util-math ` ] ( https://github.com/syntax-tree/mdast-util-math )
276
- — serialize math
326
+ — math
277
327
* [ ` syntax-tree/mdast-util-mdx ` ] ( https://github.com/syntax-tree/mdast-util-mdx )
278
- — serialize MDX or MDX.js
328
+ — MDX
279
329
* [ ` syntax-tree/mdast-util-mdx-expression ` ] ( https://github.com/syntax-tree/mdast-util-mdx-expression )
280
- — serialize MDX or MDX.js expressions
330
+ — MDX expressions
281
331
* [ ` syntax-tree/mdast-util-mdx-jsx ` ] ( https://github.com/syntax-tree/mdast-util-mdx-jsx )
282
- — serialize MDX or MDX.js JSX
332
+ — MDX JSX
283
333
* [ ` syntax-tree/mdast-util-mdxjs-esm ` ] ( https://github.com/syntax-tree/mdast-util-mdxjs-esm )
284
- — serialize MDX.js ESM
334
+ — MDX ESM
335
+
336
+ ## Syntax
337
+
338
+ Markdown is serialized according to CommonMark but care is taken to format in
339
+ such a way that the resulting markdown should work with most markdown parsers.
340
+ Extensions can add support for custom syntax.
341
+
342
+ ## Syntax tree
343
+
344
+ The syntax tree is [ mdast] [ ] .
345
+
346
+ ## Types
347
+
348
+ This package is fully typed with [ TypeScript] [ ] .
349
+ It exports the types ` Options ` , ` Map ` , ` Unsafe ` , ` Join ` , ` Handlers ` , ` Handle ` ,
350
+ ` Context ` , ` SafeOptions ` , which model the interfaces used by options and
351
+ extensions.
285
352
286
353
## Security
287
354
@@ -290,20 +357,20 @@ syntax tree, but there are several cases where that is impossible.
290
357
It’ll do its best, but complete roundtripping is impossible given that any value
291
358
could be injected into the tree.
292
359
293
- As Markdown is sometimes used for HTML, and improper use of HTML can open you up
360
+ As markdown is sometimes used for HTML, and improper use of HTML can open you up
294
361
to a [ cross-site scripting (XSS)] [ xss ] attack, use of ` mdast-util-to-markdown `
295
362
and parsing it again later could potentially be unsafe.
296
363
When parsing markdown afterwards and then going to HTML, use something like
297
- [ ` hast-util-sanitize ` ] [ sanitize ] to make the tree safe.
364
+ [ ` hast-util-sanitize ` ] [ hast-util- sanitize] to make the tree safe.
298
365
299
366
## Related
300
367
301
- * [ ` micromark/micromark ` ] ( https://github.com/micromark/micromark )
302
- — the smallest commonmark-compliant markdown parser that exists
303
- * [ ` remarkjs/remark ` ] ( https://github.com/remarkjs/remark )
304
- — markdown processor powered by plugins
305
368
* [ ` syntax-tree/mdast-util-from-markdown ` ] ( https://github.com/syntax-tree/mdast-util-from-markdown )
306
369
— parse markdown to mdast
370
+ * [ ` micromark/micromark ` ] ( https://github.com/micromark/micromark )
371
+ — parse markdown
372
+ * [ ` remarkjs/remark ` ] ( https://github.com/remarkjs/remark )
373
+ — process markdown
307
374
308
375
## Contribute
309
376
@@ -349,6 +416,8 @@ abide by its terms.
349
416
350
417
[ npm ] : https://docs.npmjs.com/cli/install
351
418
419
+ [ skypack ] : https://www.skypack.dev
420
+
352
421
[ license ] : license
353
422
354
423
[ author ] : https://wooorm.com
@@ -359,14 +428,20 @@ abide by its terms.
359
428
360
429
[ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
361
430
431
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
432
+
433
+ [ typescript ] : https://www.typescriptlang.org
434
+
362
435
[ mdast ] : https://github.com/syntax-tree/mdast
363
436
364
437
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
365
438
366
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
439
+ [ hast-util- sanitize] : https://github.com/syntax-tree/hast-util-sanitize
367
440
368
441
[ handlers ] : lib/handle
369
442
370
443
[ unsafe ] : lib/unsafe.js
371
444
372
445
[ remark ] : https://github.com/remarkjs/remark
446
+
447
+ [ remark-stringify ] : https://github.com/remarkjs/remark/tree/main/packages/remark-stringify
0 commit comments