8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- ** [ mdast] [ ] ** utility to parse markdown.
12
-
13
- ## When to use this
14
-
15
- Use this if you want to use ** [ micromark] [ ] ** but need an AST.
16
- Use ** [ remark] [ ] ** instead, which includes both to provide a nice interface and
17
- hundreds of plugins.
11
+ ** [ mdast] [ ] ** utility that turns markdown into a syntax tree.
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
+ * [ ` fromMarkdown(doc[, encoding][, options]) ` ] ( #frommarkdowndoc-encoding-options )
21
+ * [ List of extensions] ( #list-of-extensions )
22
+ * [ Syntax] ( #syntax )
23
+ * [ Syntax tree] ( #syntax-tree )
24
+ * [ Types] ( #types )
25
+ * [ Compatibility] ( #compatibility )
26
+ * [ Security] ( #security )
27
+ * [ Related] ( #related )
28
+ * [ Contribute] ( #contribute )
29
+ * [ License] ( #license )
30
+
31
+ ## What is this?
32
+
33
+ This package is a utility that takes markdown input and turns it into an
34
+ [ mdast] [ ] syntax tree.
35
+
36
+ This utility uses [ ` micromark ` ] [ micromark ] , which turns markdown into tokens,
37
+ while it turns those tokens into nodes.
38
+ It’s used in [ ` remark-parse ` ] [ remark-parse ] , which focusses on making it easier
39
+ to transform content by abstracting these internals away.
40
+
41
+ ## When should I use this?
42
+
43
+ If you want to handle syntax trees manually, use this.
44
+ Use [ ` micromark ` ] [ micromark ] instead when you * just* want to turn markdown into
45
+ HTML.
46
+ For an easier time processing content, use the ** [ remark] [ ] ** ecosystem instead.
18
47
19
48
## Install
20
49
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] [ ] :
50
+ This package is [ ESM only] [ esm ] .
51
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
25
52
26
53
``` sh
27
54
npm install mdast-util-from-markdown
28
55
```
29
56
57
+ In Deno with [ Skypack] [ ] :
58
+
59
+ ``` js
60
+ import {fromMarkdown } from ' https://cdn.skypack.dev/mdast-util-from-markdown@1?dts'
61
+ ```
62
+
63
+ In browsers with [ Skypack] [ ] :
64
+
65
+ ``` html
66
+ <script type =" module" >
67
+ import {fromMarkdown } from ' https://cdn.skypack.dev/mdast-util-from-markdown@1?min'
68
+ </script >
69
+ ```
70
+
30
71
## Use
31
72
32
- Say we have the following markdown file, ` example.md ` :
73
+ Say we have the following markdown file ` example.md ` :
33
74
34
75
``` markdown
35
76
## Hello, *World*!
36
77
```
37
78
38
- And our script, ` example.js ` , looks as follows:
79
+ …and our module ` example.js ` looks as follows:
39
80
40
81
``` js
41
- import fs from ' node:fs'
82
+ import { promises as fs } from ' node:fs'
42
83
import {fromMarkdown } from ' mdast-util-from-markdown'
43
84
44
- const doc = fs . readFileSync ( ' example.md ' )
85
+ main ( )
45
86
46
- const tree = fromMarkdown (doc)
87
+ async function main () {
88
+ const doc = await fs .readFile (' example.md' )
89
+ const tree = fromMarkdown (doc)
47
90
48
- console .log (tree)
91
+ console .log (tree)
92
+ }
49
93
```
50
94
51
- Now, running ` node example ` yields (positional info removed for brevity):
95
+ …now running ` node example.js ` yields (positional info removed for brevity):
52
96
53
97
``` js
54
98
{
@@ -59,10 +103,7 @@ Now, running `node example` yields (positional info removed for brevity):
59
103
depth: 2 ,
60
104
children: [
61
105
{type: ' text' , value: ' Hello, ' },
62
- {
63
- type: ' emphasis' ,
64
- children: [{type: ' text' , value: ' World' }]
65
- },
106
+ {type: ' emphasis' , children: [{type: ' text' , value: ' World' }]},
66
107
{type: ' text' , value: ' !' }
67
108
]
68
109
}
@@ -82,7 +123,7 @@ Without this condition, production code is loaded.
82
123
83
124
### ` fromMarkdown(doc[, encoding][, options]) `
84
125
85
- Parse markdown to a ** [ mdast ] [ ] ** tree.
126
+ Turn markdown into a syntax tree.
86
127
87
128
##### Parameters
88
129
@@ -97,12 +138,12 @@ Value to parse (`string` or [`Buffer`][buffer]).
97
138
98
139
###### ` options.extensions `
99
140
100
- Array of syntax extensions (` Array<MicromarkSyntaxExtension> ` , default: ` [] ` ).
101
- Passed to [ ` micromark ` as ` extensions ` ] [ micromark-extensions ] .
141
+ List of syntax extensions (` Array<MicromarkSyntaxExtension> ` , default: ` [] ` ).
142
+ Passed to [ ` micromark ` as ` options. extensions` ] [ micromark-extensions ] .
102
143
103
144
###### ` options.mdastExtensions `
104
145
105
- Array of mdast extensions (` Array<MdastExtension> ` , default: ` [] ` ).
146
+ List of mdast extensions (` Array<MdastExtension> ` , default: ` [] ` ).
106
147
107
148
##### Returns
108
149
@@ -111,48 +152,73 @@ Array of mdast extensions (`Array<MdastExtension>`, default: `[]`).
111
152
## List of extensions
112
153
113
154
* [ ` syntax-tree/mdast-util-directive ` ] ( https://github.com/syntax-tree/mdast-util-directive )
114
- — parse directives
155
+ — directives
115
156
* [ ` syntax-tree/mdast-util-frontmatter ` ] ( https://github.com/syntax-tree/mdast-util-frontmatter )
116
- — parse frontmatter (YAML, TOML, more)
157
+ — frontmatter (YAML, TOML, more)
117
158
* [ ` syntax-tree/mdast-util-gfm ` ] ( https://github.com/syntax-tree/mdast-util-gfm )
118
- — parse GFM
159
+ — GFM
119
160
* [ ` syntax-tree/mdast-util-gfm-autolink-literal ` ] ( https://github.com/syntax-tree/mdast-util-gfm-autolink-literal )
120
- — parse GFM autolink literals
161
+ — GFM autolink literals
121
162
* [ ` syntax-tree/mdast-util-gfm-footnote ` ] ( https://github.com/syntax-tree/mdast-util-gfm-footnote )
122
- — parse GFM footnotes
163
+ — GFM footnotes
123
164
* [ ` syntax-tree/mdast-util-gfm-strikethrough ` ] ( https://github.com/syntax-tree/mdast-util-gfm-strikethrough )
124
- — parse GFM strikethrough
165
+ — GFM strikethrough
125
166
* [ ` syntax-tree/mdast-util-gfm-table ` ] ( https://github.com/syntax-tree/mdast-util-gfm-table )
126
- — parse GFM tables
167
+ — GFM tables
127
168
* [ ` syntax-tree/mdast-util-gfm-task-list-item ` ] ( https://github.com/syntax-tree/mdast-util-gfm-task-list-item )
128
- — parse GFM task list items
169
+ — GFM task list items
129
170
* [ ` syntax-tree/mdast-util-math ` ] ( https://github.com/syntax-tree/mdast-util-math )
130
- — parse math
171
+ — math
131
172
* [ ` syntax-tree/mdast-util-mdx ` ] ( https://github.com/syntax-tree/mdast-util-mdx )
132
- — parse MDX or MDX.js
173
+ — MDX
133
174
* [ ` syntax-tree/mdast-util-mdx-expression ` ] ( https://github.com/syntax-tree/mdast-util-mdx-expression )
134
- — parse MDX or MDX.js expressions
175
+ — MDX expressions
135
176
* [ ` syntax-tree/mdast-util-mdx-jsx ` ] ( https://github.com/syntax-tree/mdast-util-mdx-jsx )
136
- — parse MDX or MDX.js JSX
177
+ — MDX JSX
137
178
* [ ` syntax-tree/mdast-util-mdxjs-esm ` ] ( https://github.com/syntax-tree/mdast-util-mdxjs-esm )
138
- — parse MDX.js ESM
179
+ — MDX ESM
180
+
181
+ ## Syntax
182
+
183
+ Markdown is parsed according to CommonMark.
184
+ Extensions can add support for other syntax.
185
+ If you’re interested in extending markdown,
186
+ [ more information is available in micromark’s readme] [ micromark-extend ] .
187
+
188
+ ## Syntax tree
189
+
190
+ The syntax tree is [ mdast] [ ] .
191
+
192
+ ## Types
193
+
194
+ This package is fully typed with [ TypeScript] [ ] .
195
+ It exports the types ` Value ` , ` Encoding ` , ` Options ` , ` Extension ` , ` Handle ` ,
196
+ ` Transform ` , ` Token ` , ` CompileContext ` , ` OnEnterError ` , ` OnExitError ` , which
197
+ model the interfaces used in parameters, options, and extensions.
198
+
199
+ ## Compatibility
200
+
201
+ Projects maintained by the unified collective are compatible with all maintained
202
+ versions of Node.js.
203
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
204
+ Our projects sometimes work with older versions, but this is not guaranteed.
139
205
140
206
## Security
141
207
142
208
As markdown is sometimes used for HTML, and improper use of HTML can open you up
143
209
to a [ cross-site scripting (XSS)] [ xss ] attack, use of ` mdast-util-from-markdown `
144
210
can also be unsafe.
145
211
When going to HTML, use this utility in combination with
146
- [ ` hast-util-sanitize ` ] [ sanitize ] to make the tree safe.
212
+ [ ` hast-util-sanitize ` ] [ hast-util- sanitize] to make the tree safe.
147
213
148
214
## Related
149
215
216
+ * [ ` syntax-tree/mdast-util-to-markdown ` ] ( https://github.com/syntax-tree/mdast-util-to-markdown )
217
+ — serialize mdast as markdown
150
218
* [ ` micromark/micromark ` ] ( https://github.com/micromark/micromark )
151
- — the smallest commonmark-compliant markdown parser that exists
219
+ — parse markdown
152
220
* [ ` remarkjs/remark ` ] ( https://github.com/remarkjs/remark )
153
- — markdown processor powered by plugins
154
- * [ ` syntax-tree/mdast-util-to-markdown ` ] ( https://github.com/syntax-tree/mdast-util-to-markdown )
155
- — serialize mdast to markdown
221
+ — process markdown
156
222
157
223
## Contribute
158
224
@@ -198,6 +264,8 @@ abide by its terms.
198
264
199
265
[ npm ] : https://docs.npmjs.com/cli/install
200
266
267
+ [ skypack ] : https://www.skypack.dev
268
+
201
269
[ license ] : license
202
270
203
271
[ author ] : https://wooorm.com
@@ -208,6 +276,10 @@ abide by its terms.
208
276
209
277
[ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
210
278
279
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
280
+
281
+ [ typescript ] : https://www.typescriptlang.org
282
+
211
283
[ mdast ] : https://github.com/syntax-tree/mdast
212
284
213
285
[ root ] : https://github.com/syntax-tree/mdast#root
@@ -218,10 +290,14 @@ abide by its terms.
218
290
219
291
[ xss ] : https://en.wikipedia.org/wiki/Cross-site_scripting
220
292
221
- [ sanitize ] : https://github.com/syntax-tree/hast-util-sanitize
293
+ [ hast-util- sanitize] : https://github.com/syntax-tree/hast-util-sanitize
222
294
223
295
[ micromark ] : https://github.com/micromark/micromark
224
296
225
297
[ micromark-extensions ] : https://github.com/micromark/micromark#optionsextensions
226
298
299
+ [ micromark-extend ] : https://github.com/micromark/micromark#extensions
300
+
227
301
[ remark ] : https://github.com/remarkjs/remark
302
+
303
+ [ remark-parse ] : https://github.com/remarkjs/remark/tree/main/packages/remark-parse
0 commit comments