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) JSX.
13
- When parsing (` from-markdown ` ), must be combined with
14
- [ ` micromark-extension-mdx-jsx ` ] [ extension ] .
15
-
16
- This utility handles parsing and serializing.
17
- See [ ` micromark-extension-mdx-jsx ` ] [ extension ] for how the syntax works.
11
+ Extensions to parse and serialize JSX between mdast and markdown
12
+
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When to use this] ( #when-to-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` mdxJsxFromMarkdown() ` ] ( #mdxjsxfrommarkdown )
21
+ * [ ` mdxJsxToMarkdown() ` ] ( #mdxjsxtomarkdown )
22
+ * [ Syntax tree] ( #syntax-tree )
23
+ * [ Nodes] ( #nodes )
24
+ * [ Mixin] ( #mixin )
25
+ * [ Content model] ( #content-model )
26
+ * [ Types] ( #types )
27
+ * [ Compatibility] ( #compatibility )
28
+ * [ Related] ( #related )
29
+ * [ Contribute] ( #contribute )
30
+ * [ License] ( #license )
31
+
32
+ ## What is this?
33
+
34
+ This package contains extensions that add support for the JSX syntax enabled by
35
+ MDX to [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
36
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
37
+
38
+ [ JSX] [ ] is an XML-like syntax extension to ECMAScript (JavaScript), which MDX
39
+ brings to markdown.
40
+ For more info on MDX, see [ What is MDX?] [ what-is-mdx ]
18
41
19
42
## When to use this
20
43
21
- Use [ ` mdast-util-mdx ` ] [ mdast-util-mdx ] if you want all of MDX / MDX.js.
22
- Use this otherwise.
44
+ These tools are all rather low-level.
45
+ In most cases, you’d want to use [ ` remark-mdx ` ] [ remark-mdx ] with [ remark] [ ]
46
+ instead.
23
47
24
- ## Install
48
+ When you are working with syntax trees, and want all of MDX, use
49
+ [ ` mdast-util-mdx ` ] [ mdast-util-mdx ] instead.
50
+
51
+ When working with ` micromark ` (through ` mdast-util-from-markdown ` ), you’d want
52
+ to combine this package with
53
+ [ ` micromark-extension-mdx-jsx ` ] [ micromark-extension-mdx-jsx ] .
25
54
26
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
27
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
55
+ ## Install
28
56
29
- [ npm] [ ] :
57
+ This package is [ ESM only] [ esm ] .
58
+ In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
30
59
31
60
``` sh
32
61
npm install mdast-util-mdx-jsx
33
62
```
34
63
64
+ In Deno with [ Skypack] [ ] :
65
+
66
+ ``` js
67
+ import {mdxJsxFromMarkdown , mdxJsxToMarkdown } from ' https://cdn.skypack.dev/mdast-util-mdx-jsx@1?dts'
68
+ ```
69
+
70
+ In browsers with [ Skypack] [ ] :
71
+
72
+ ``` html
73
+ <script type =" module" >
74
+ import {mdxJsxFromMarkdown , mdxJsxToMarkdown } from ' https://cdn.skypack.dev/mdast-util-mdx-jsx@1?min'
75
+ </script >
76
+ ```
77
+
35
78
## Use
36
79
37
- Say we have an MDX.js file, ` example.mdx ` :
80
+ Say our document ` example.mdx ` contains :
38
81
39
82
``` mdx
40
83
<Box >
@@ -46,7 +89,7 @@ Say we have an MDX.js file, `example.mdx`:
46
89
<abbr title = " Hypertext Markup Language" >HTML</abbr > is a lovely language.
47
90
```
48
91
49
- And our module, ` example.js ` , looks as follows:
92
+ …and our module ` example.js ` looks as follows:
50
93
51
94
``` js
52
95
import fs from ' node:fs'
@@ -70,7 +113,7 @@ const out = toMarkdown(tree, {extensions: [mdxJsxToMarkdown()]})
70
113
console .log (out)
71
114
```
72
115
73
- Now, running ` node example ` yields (positional info removed for brevity):
116
+ …now running ` node example.js ` yields (positional info removed for brevity):
74
117
75
118
``` js
76
119
{
@@ -164,43 +207,52 @@ Now, running `node example` yields (positional info removed for brevity):
164
207
165
208
## API
166
209
210
+ This package exports the following identifiers: ` mdxJsxFromMarkdown ` ,
211
+ ` mdxJsxToMarkdown ` .
212
+ There is no default export.
213
+
167
214
### ` mdxJsxFromMarkdown() `
168
215
169
- ### ` mdxJsxToMarkdown() `
216
+ Function that can be called to get an extension for
217
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
218
+
219
+ When using [ ` micromark-extension-mdx-jsx ` ] [ micromark-extension-mdx-jsx ]
220
+ with ` options.addResult ` , nodes will have a ` data.estree ` field set to an
221
+ [ ESTree] [ ] .
170
222
171
- Support MDX (or MDX.js) JSX.
172
- The exports are extensions, respectively for
173
- [ ` mdast-util-from-markdown ` ] [ from-markdown ] and
174
- [ ` mdast-util-to-markdown ` ] [ to-markdown ] .
223
+ ### ` mdxJsxToMarkdown() `
175
224
176
- When using the [ syntax extension ] [ extension ] with ` addResult ` , nodes will have a
177
- ` data.estree ` field set to an [ ESTree ] [ ] .
225
+ Function that can be called to get an extension for
226
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
178
227
179
- There are no options, but passing [ ` options.quote ` ] [ quote ] to
180
- ` mdast-util-to-markdown ` is honored for attributes.
228
+ This extension configures ` mdast-util-to-markdown ` with
229
+ [ ` options.fences: true ` ] [ mdast-util-to-markdown-fences ] and
230
+ [ ` options.resourceLink: true ` ] [ mdast-util-to-markdown-resourcelink ] too, do not
231
+ overwrite them!
181
232
182
- this extension configures [ ` mdast-util-to-markdown ` ] [ to-markdown ] with
183
- ` fences: true ` and ` resourceLink: true ` too, do not overwrite them!
233
+ Passing [ ` options.quote ` ] [ mdast-util-to-markdown-quote ] to
234
+ ` mdast-util-to-markdown ` is honored for
235
+ attributes.
184
236
185
237
## Syntax tree
186
238
187
239
The following interfaces are added to ** [ mdast] [ ] ** by this utility.
188
240
189
241
### Nodes
190
242
191
- #### ` MDXJsxFlowElement `
243
+ ###### ` MdxJsxFlowElement `
192
244
193
245
``` idl
194
- interface MDXJsxFlowElement <: Parent {
246
+ interface MdxJsxFlowElement <: Parent {
195
247
type: "mdxJsxFlowElement"
196
248
}
197
249
198
- MDXJsxFlowElement includes MDXJsxElement
250
+ MdxJsxFlowElement includes MdxJsxElement
199
251
```
200
252
201
- ** MDXJsxFlowElement ** (** [ Parent] [ dfn-parent ] ** ) represents JSX in flow (block).
253
+ ** MdxJsxFlowElement ** (** [ Parent] [ dfn-parent ] ** ) represents JSX in flow (block).
202
254
It can be used where ** [ flow] [ dfn-content-flow ] ** content is expected.
203
- It includes the mixin ** [ MDXJsxElement ] [ dfn-mixin-mdx-jsx-element ] ** .
255
+ It includes the mixin ** [ MdxJsxElement ] [ dfn-mixin-mdx-jsx-element ] ** .
204
256
205
257
For example, the following markdown:
206
258
@@ -221,21 +273,21 @@ Yields:
221
273
}
222
274
```
223
275
224
- #### ` MDXJsxTextElement `
276
+ ###### ` MdxJsxTextElement `
225
277
226
278
``` idl
227
- interface MDXJsxTextElement <: Parent {
279
+ interface MdxJsxTextElement <: Parent {
228
280
type: "mdxJsxTextElement"
229
281
}
230
282
231
- MDXJsxTextElement includes MDXJsxElement
283
+ MdxJsxTextElement includes MdxJsxElement
232
284
```
233
285
234
- ** MDXJsxTextElement ** (** [ Parent] [ dfn-parent ] ** ) represents JSX in text (span,
286
+ ** MdxJsxTextElement ** (** [ Parent] [ dfn-parent ] ** ) represents JSX in text (span,
235
287
inline).
236
288
It can be used where ** [ phrasing] [ dfn-content-phrasing ] ** content is
237
289
expected.
238
- It includes the mixin ** [ MDXJsxElement ] [ dfn-mixin-mdx-jsx-element ] ** .
290
+ It includes the mixin ** [ MdxJsxElement ] [ dfn-mixin-mdx-jsx-element ] ** .
239
291
240
292
For example, the following markdown:
241
293
@@ -256,78 +308,104 @@ Yields:
256
308
257
309
### Mixin
258
310
259
- ### ` MDXJsxElement `
311
+ ###### ` MdxJsxElement `
260
312
261
313
``` idl
262
- interface mixin MDXJsxElement {
314
+ interface mixin MdxJsxElement {
263
315
name: string?
264
- attributes: [MDXJsxExpressionAttribute | MDXJsxAttribute ]
316
+ attributes: [MdxJsxExpressionAttribute | MdxJsxAttribute ]
265
317
}
266
318
267
- interface MDXJsxExpressionAttribute <: Literal {
319
+ interface MdxJsxExpressionAttribute <: Literal {
268
320
type: "mdxJsxExpressionAttribute"
269
321
}
270
322
271
- interface MDXJsxAttribute <: Node {
323
+ interface MdxJsxAttribute <: Node {
272
324
type: "mdxJsxAttribute"
273
325
name: string
274
- value: MDXJsxAttributeValueExpression | string?
326
+ value: MdxJsxAttributeValueExpression | string?
275
327
}
276
328
277
- interface MDXJsxAttributeValueExpression <: Literal {
329
+ interface MdxJsxAttributeValueExpression <: Literal {
278
330
type: "mdxJsxAttributeValueExpression"
279
331
}
280
332
```
281
333
282
- ** MDXJsxElement ** represents a JSX element.
334
+ ** MdxJsxElement ** represents a JSX element.
283
335
284
336
The ` name ` field can be present and represents an identifier.
285
337
Without ` name ` , the element represents a fragment, in which case no attributes
286
338
must be present.
287
339
288
340
The ` attributes ` field represents information associated with the node.
289
- The value of the ` attributes ` field is a list of ** MDXJsxExpressionAttribute **
290
- and ** MDXJsxAttribute ** nodes.
341
+ The value of the ` attributes ` field is a list of ** MdxJsxExpressionAttribute **
342
+ and ** MdxJsxAttribute ** nodes.
291
343
292
- ** MDXJsxExpressionAttribute ** represents an expression (typically in a
344
+ ** MdxJsxExpressionAttribute ** represents an expression (typically in a
293
345
programming language) that when evaluated results in multiple attributes.
294
346
295
- ** MDXJsxAttribute ** represents a single attribute.
347
+ ** MdxJsxAttribute ** represents a single attribute.
296
348
The ` name ` field must be present.
297
349
The ` value ` field can be present, in which case it is either a string (a static
298
350
value) or an expression (typically in a programming language) that when
299
351
evaluated results in an attribute value.
300
352
301
353
### Content model
302
354
303
- #### ` FlowContent ` (MDX JSX)
355
+ ###### ` FlowContent ` (MDX JSX)
304
356
305
357
``` idl
306
- type MDXJsxFlowContent = MDXJsxFlowElement | FlowContent
358
+ type MdxJsxFlowContent = MdxJsxFlowElement | FlowContent
307
359
```
308
360
309
- #### ` PhrasingContent ` (MDX JSX)
361
+ ###### ` PhrasingContent ` (MDX JSX)
310
362
311
363
``` idl
312
- type MDXJsxPhrasingContent = MDXJsxTextElement | PhrasingContent
364
+ type MdxJsxPhrasingContent = MdxJsxTextElement | PhrasingContent
313
365
```
314
366
367
+ ## Types
368
+
369
+ This package is fully typed with [ TypeScript] [ ] .
370
+ It exports the ` MdxJsxAttributeValueExpression ` , ` MdxJsxAttribute ` ,
371
+ ` MdxJsxExpressionAttribute ` , ` MdxJsxFlowElement ` , and ` MdxJsxTextElement ` types.
372
+
373
+ It also registers the node types with ` @types/mdast `
374
+
375
+ If you’re working with the syntax tree, make sure to import this plugin
376
+ somewhere in your types, as that registers the new node types in the tree.
377
+
378
+ ``` js
379
+ /** @typedef {import('remark-math')} */
380
+
381
+ import {visit } from ' unist-util-visit'
382
+
383
+ /** @type {import('mdast').Root} */
384
+ const tree = getMdastNodeSomeHow ()
385
+
386
+ visit (tree, (node ) => {
387
+ // `node` can now be one of the JSX nodes.
388
+ })
389
+ ```
390
+
391
+ ## Compatibility
392
+
393
+ Projects maintained by the unified collective are compatible with all maintained
394
+ versions of Node.js.
395
+ As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
396
+ Our projects sometimes work with older versions, but this is not guaranteed.
397
+
398
+ This plugin works with ` mdast-util-from-markdown ` version 1+ and
399
+ ` mdast-util-to-markdown ` version 1+.
400
+
315
401
## Related
316
402
317
- * [ ` remarkjs/remark ` ] [ remark ]
318
- — markdown processor powered by plugins
319
- * [ ` remarkjs/remark-mdx ` ] [ remark-mdx ]
320
- — remark plugin to support MDX
321
- * [ ` syntax-tree/mdast-util-from-markdown ` ] [ from-markdown ]
322
- — mdast parser using ` micromark ` to create mdast from markdown
323
- * [ ` syntax-tree/mdast-util-to-markdown ` ] [ to-markdown ]
324
- — mdast serializer to create markdown from mdast
403
+ * [ ` micromark/micromark-extension-mdx-jsx ` ] [ micromark-extension-mdx-jsx ]
404
+ — support MDX JSX in micromark
325
405
* [ ` syntax-tree/mdast-util-mdx ` ] [ mdast-util-mdx ]
326
- — mdast utility to support all of MDX
327
- * [ ` micromark/micromark ` ] [ micromark ]
328
- — the smallest commonmark-compliant markdown parser that exists
329
- * [ ` micromark/micromark-extension-mdx-jsx ` ] [ extension ]
330
- — micromark extension to parse JSX
406
+ — support MDX in mdast
407
+ * [ ` remarkjs/remark-mdx ` ] [ remark-mdx ]
408
+ — support MDX in remark
331
409
332
410
## Contribute
333
411
@@ -371,6 +449,8 @@ abide by its terms.
371
449
372
450
[ npm ] : https://docs.npmjs.com/cli/install
373
451
452
+ [ skypack ] : https://www.skypack.dev
453
+
374
454
[ license ] : license
375
455
376
456
[ author ] : https://wooorm.com
@@ -381,22 +461,20 @@ abide by its terms.
381
461
382
462
[ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
383
463
384
- [ mdast ] : https://github.com/syntax-tree/mdast
464
+ [ esm ] : https://gist. github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
385
465
386
- [ remark ] : https://github.com/remarkjs/remark
466
+ [ typescript ] : https://www.typescriptlang.org
387
467
388
- [ from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
468
+ [ mdast ] : https://github.com/syntax-tree/mdast
389
469
390
- [ to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
470
+ [ remark ] : https://github.com/remarkjs/remark
391
471
392
- [ micromark ] : https://github.com/micromark/micromark
472
+ [ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
393
473
394
- [ extension ] : https://github.com/micromark/micromark-extension-mdxjs-esm
474
+ [ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
395
475
396
476
[ mdast-util-mdx ] : https://github.com/syntax-tree/mdast-util-mdx
397
477
398
- [ quote ] : https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
399
-
400
478
[ estree ] : https://github.com/estree/estree
401
479
402
480
[ dfn-parent ] : https://github.com/syntax-tree/mdast#parent
@@ -408,3 +486,15 @@ abide by its terms.
408
486
[ dfn-mixin-mdx-jsx-element ] : #mdxjsxelement
409
487
410
488
[ remark-mdx ] : https://github.com/mdx-js/mdx/tree/main/packages/remark-mdx
489
+
490
+ [ jsx ] : https://facebook.github.io/jsx/
491
+
492
+ [ what-is-mdx ] : https://mdxjs.com/docs/what-is-mdx/
493
+
494
+ [ micromark-extension-mdx-jsx ] : https://github.com/micromark/micromark-extension-mdx-jsx
495
+
496
+ [ mdast-util-to-markdown-quote ] : https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
497
+
498
+ [ mdast-util-to-markdown-fences ] : https://github.com/syntax-tree/mdast-util-to-markdown#optionsfences
499
+
500
+ [ mdast-util-to-markdown-resourcelink ] : https://github.com/syntax-tree/mdast-util-to-markdown#optionsresourcelink
0 commit comments