8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- Extension for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and/or
12
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] to support MDX (or MDX.js) in
13
- ** [ mdast] [ ] ** .
14
- When parsing (` from-markdown ` ), must be combined with either
15
- [ ` micromark-extension-mdx ` ] [ mdx ] or [ ` micromark-extension-mdxjs ` ] [ mdxjs ] .
11
+ [ mdast] [ ] extensions to parse and serialize [ MDX] [ ] : ESM import/exports,
12
+ JavaScript expressions, and JSX.
13
+
14
+ ## Contents
15
+
16
+ * [ What is this?] ( #what-is-this )
17
+ * [ When to use this] ( #when-to-use-this )
18
+ * [ Install] ( #install )
19
+ * [ Use] ( #use )
20
+ * [ API] ( #api )
21
+ * [ ` mdxFromMarkdown() ` ] ( #mdxfrommarkdown )
22
+ * [ ` mdxToMarkdown(options?) ` ] ( #mdxtomarkdownoptions )
23
+ * [ Syntax tree] ( #syntax-tree )
24
+ * [ Types] ( #types )
25
+ * [ Compatibility] ( #compatibility )
26
+ * [ Related] ( #related )
27
+ * [ Contribute] ( #contribute )
28
+ * [ License] ( #license )
29
+
30
+ ## What is this?
31
+
32
+ This package contains extensions for
33
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
34
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] to enable the features that
35
+ MDX adds to markdown: import/exports (` export x from 'y' ` ), expressions
36
+ (` {z} ` ), and JSX (` <Component /> ` ).
16
37
17
38
## When to use this
18
39
40
+ These tools are all rather low-level.
41
+ In many cases, you’d want to use [ ` remark-mdx ` ] [ remark-mdx ] with remark instead.
42
+
19
43
Use this if you’re dealing with the AST manually and want to support all of MDX.
20
- You can also use the extensions separately:
44
+ Instead of this package, you can also use the extensions separately:
21
45
22
46
* [ ` mdast-util-mdx-expression ` ] ( https://github.com/syntax-tree/mdast-util-mdx-expression )
23
- — support MDX (or MDX.js) expressions
47
+ — support MDX expressions
24
48
* [ ` mdast-util-mdx-jsx ` ] ( https://github.com/syntax-tree/mdast-util-mdx-jsx )
25
- — support MDX (or MDX.js) JSX
49
+ — support MDX JSX
26
50
* [ ` mdast-util-mdxjs-esm ` ] ( https://github.com/syntax-tree/mdast-util-mdxjs-esm )
27
- — support MDX.js ESM
51
+ — support MDX ESM
28
52
29
- ## Install
53
+ When working with ` mdast-util-from-markdown ` , you must combine this package with
54
+ [ ` micromark/micromark-extension-mdx ` ] [ mdx ] or
55
+ [ ` micromark/micromark-extension-mdxjs ` ] [ mdxjs ] .
30
56
31
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
32
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
57
+ ## Install
33
58
34
- [ npm] [ ] :
59
+ This package is [ ESM only] [ esm ] .
60
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
35
61
36
62
``` sh
37
63
npm install mdast-util-mdx
38
64
```
39
65
66
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
67
+
68
+ ``` js
69
+ import {mdxFromMarkdown , mdxToMarkdown } from ' https://esm.sh/mdast-util-mdx@2'
70
+ ```
71
+
72
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
73
+
74
+ ``` html
75
+ <script type =" module" >
76
+ import {mdxFromMarkdown , mdxToMarkdown } from ' https://esm.sh/mdast-util-mdx@2?bundle'
77
+ </script >
78
+ ```
79
+
40
80
## Use
41
81
42
- Say we have the following file, ` example.mdx ` :
82
+ Say our document ` example.mdx ` contains :
43
83
44
84
``` markdown
45
85
import Box from "place"
@@ -59,16 +99,16 @@ Which you can also put inline: {1+1}.
59
99
</Box>
60
100
```
61
101
62
- And our script, ` example.js ` , looks as follows:
102
+ …and our module ` example.js ` looks as follows:
63
103
64
104
``` js
65
- import fs from ' node:fs'
105
+ import fs from ' node:fs/promises '
66
106
import {fromMarkdown } from ' mdast-util-from-markdown'
67
107
import {toMarkdown } from ' mdast-util-to-markdown'
68
108
import {mdxjs } from ' micromark-extension-mdxjs'
69
109
import {mdxFromMarkdown , mdxToMarkdown } from ' mdast-util-mdx'
70
110
71
- const doc = fs .readFileSync (' example.mdx' )
111
+ const doc = await fs .readFile (' example.mdx' )
72
112
73
113
const tree = fromMarkdown (doc, {
74
114
extensions: [mdxjs ()],
@@ -82,7 +122,7 @@ const out = toMarkdown(tree, {extensions: [mdxToMarkdown()]})
82
122
console .log (out)
83
123
```
84
124
85
- Now, running ` node example ` yields (positional info removed for brevity):
125
+ …now running ` node example.js ` yields (positional info removed for brevity):
86
126
87
127
``` js
88
128
{
@@ -222,42 +262,88 @@ Which you can also put inline: {1+1}.
222
262
223
263
## API
224
264
225
- This package exports the following identifier: ` mdxFromMarkdown ` ,
226
- ` mdxToMarkdown ` .
265
+ This package exports the identifiers ` mdxFromMarkdown ` and ` mdxToMarkdown ` .
227
266
There is no default export.
228
267
229
268
### ` mdxFromMarkdown() `
230
269
270
+ Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
271
+
272
+ When using the [ syntax extension with ` addResult ` ] [ mdxjs ] , nodes will have
273
+ a ` data.estree ` field set to an [ ESTree] [ ] .
274
+
231
275
### ` mdxToMarkdown(options?) `
232
276
233
- Support MDX (or MDX.js).
234
- The exports are functions that can be called to respectively get an extension
235
- for [ ` mdast-util-from-markdown ` ] [ from-markdown ] and
236
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] .
277
+ Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
278
+
279
+ ##### ` options `
280
+
281
+ Configuration (optional).
282
+ Currently passes through ` quote ` , ` quoteSmart ` , ` tightSelfClosing ` , and
283
+ ` printWidth ` to [ ` mdast-util-mdx-jsx ` ] [ options ] .
284
+
285
+ ## Syntax tree
286
+
287
+ This utility combines several mdast utilities.
288
+ See their readmes for the node types supported in the tree:
289
+
290
+ * [ ` mdast-util-mdx-expression ` ] ( https://github.com/syntax-tree/mdast-util-mdx-expression#syntax-tree )
291
+ — support MDX expressions
292
+ * [ ` mdast-util-mdx-jsx ` ] ( https://github.com/syntax-tree/mdast-util-mdx-jsx#syntax-tree )
293
+ — support MDX JSX
294
+ * [ ` mdast-util-mdxjs-esm ` ] ( https://github.com/syntax-tree/mdast-util-mdxjs-esm#syntax-tree )
295
+ — support MDX ESM
296
+
297
+ ## Types
237
298
238
- The options to ` mdxToMarkdown ` are [ passed to ` mdxJsxToMarkdown ` ] [ options ] .
299
+ This package is fully typed with [ TypeScript] [ ] .
300
+ It exports the additional types ` MdxFlowExpression ` , ` MdxTextExpression ` ,
301
+ ` MdxjsEsm ` , ` MdxJsxAttributeValueExpression ` , ` MdxJsxAttribute ` ,
302
+ ` MdxJsxExpressionAttribute ` , ` MdxJsxFlowElement ` ,
303
+ ` MdxJsxTextElement ` , and ` ToMarkdownOptions ` .
304
+
305
+ It also registers the node types with ` @types/mdast ` .
306
+ If you’re working with the syntax tree, make sure to import this utility
307
+ somewhere in your types, as that registers the new node types in the tree.
308
+
309
+ ``` js
310
+ /**
311
+ * @typedef {import('mdast-util-mdx')}
312
+ */
313
+
314
+ import {visit } from ' unist-util-visit'
315
+
316
+ /** @type {import('mdast').Root} */
317
+ const tree = getMdastNodeSomeHow ()
318
+
319
+ visit (tree, (node ) => {
320
+ // `node` can now be an expression, JSX, or ESM node.
321
+ })
322
+ ```
323
+
324
+ ## Compatibility
325
+
326
+ Projects maintained by the unified collective are compatible with all maintained
327
+ versions of Node.js.
328
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
329
+ Our projects sometimes work with older versions, but this is not guaranteed.
330
+
331
+ This utility works with ` mdast-util-from-markdown ` version 1+ and
332
+ ` mdast-util-to-markdown ` version 1+.
239
333
240
334
## Related
241
335
242
- * [ ` remarkjs/remark ` ] [ remark ]
243
- — markdown processor powered by plugins
244
336
* [ ` remarkjs/remark-mdx ` ] [ remark-mdx ]
245
- — remark plugin to support MDX (or MDX.js)
246
- * [ ` micromark/micromark ` ] [ micromark ]
247
- — the smallest commonmark-compliant markdown parser that exists
337
+ — remark plugin to support MDX
248
338
* [ ` micromark/micromark-extension-mdx ` ] [ mdx ]
249
339
— micromark extension to parse MDX
250
340
* [ ` micromark/micromark-extension-mdxjs ` ] [ mdxjs ]
251
- — micromark extension to parse MDX.js
252
- * [ ` syntax-tree/mdast-util-from-markdown ` ] [ from-markdown ]
253
- — mdast parser using ` micromark ` to create mdast from markdown
254
- * [ ` syntax-tree/mdast-util-to-markdown ` ] [ to-markdown ]
255
- — mdast serializer to create markdown from mdast
341
+ — micromark extension to parse JavaScript-aware MDX
256
342
257
343
## Contribute
258
344
259
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
260
- started.
345
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
346
+ ways to get started.
261
347
See [ ` support.md ` ] [ support ] for ways to get help.
262
348
263
349
This project has a [ code of conduct] [ coc ] .
@@ -298,25 +384,29 @@ abide by its terms.
298
384
299
385
[ npm ] : https://docs.npmjs.com/cli/install
300
386
387
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
388
+
389
+ [ esmsh ] : https://esm.sh
390
+
391
+ [ typescript ] : https://www.typescriptlang.org
392
+
301
393
[ license ] : license
302
394
303
395
[ author ] : https://wooorm.com
304
396
305
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
397
+ [ health ] : https://github.com/syntax-tree/.github
306
398
307
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
399
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
308
400
309
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct .md
401
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support .md
310
402
311
- [ mdast ] : https://github.com/syntax-tree/mdast
312
-
313
- [ remark ] : https://github.com/remarkjs/remark
403
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
314
404
315
- [ from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
405
+ [ mdast ] : https://github.com/syntax-tree/mdast
316
406
317
- [ to- markdown] : https://github.com/syntax-tree/mdast-util-to -markdown
407
+ [ mdast-util-from- markdown] : https://github.com/syntax-tree/mdast-util-from -markdown
318
408
319
- [ micromark ] : https://github.com/micromark/micromark
409
+ [ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
320
410
321
411
[ mdx ] : https://github.com/micromark/micromark-extension-mdx
322
412
@@ -325,3 +415,5 @@ abide by its terms.
325
415
[ remark-mdx ] : https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx
326
416
327
417
[ options ] : https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxtomarkdownoptions
418
+
419
+ [ estree ] : https://github.com/estree/estree
0 commit comments