8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ mdast] [ ] extensions to parse and serialize [ MDX] [ ] : ESM import/exports,
12
- JavaScript expressions, and JSX.
11
+ [ mdast] [ ] extensions to parse and serialize [ MDX] [ ] : ESM, JSX, and expressions.
13
12
14
13
## Contents
15
14
@@ -20,6 +19,9 @@ JavaScript expressions, and JSX.
20
19
* [ API] ( #api )
21
20
* [ ` mdxFromMarkdown() ` ] ( #mdxfrommarkdown )
22
21
* [ ` mdxToMarkdown(options?) ` ] ( #mdxtomarkdownoptions )
22
+ * [ ` ToMarkdownOptions ` ] ( #tomarkdownoptions )
23
+ * [ HTML] ( #html )
24
+ * [ Syntax] ( #syntax )
23
25
* [ Syntax tree] ( #syntax-tree )
24
26
* [ Types] ( #types )
25
27
* [ Compatibility] ( #compatibility )
@@ -29,18 +31,23 @@ JavaScript expressions, and JSX.
29
31
30
32
## What is this?
31
33
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 /> ` ).
34
+ This package contains two extensions that add support for MDX syntax in
35
+ markdown to [ mdast] [ ] : ESM (` import x from 'y' ` ), JSX (` <a /> ` ), and
36
+ expressions (` {Math.PI} ` ).
37
+ These extensions plug into
38
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] (to support parsing
39
+ MDX in markdown into a syntax tree) and
40
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] (to support serializing
41
+ MDX in syntax trees to markdown).
37
42
38
43
## When to use this
39
44
40
- These tools are all rather low-level.
41
- In many cases, you’d want to use [ ` remark-mdx ` ] [ remark-mdx ] with remark instead.
45
+ You can use these extensions when you are working with
46
+ ` mdast-util-from-markdown ` and ` mdast-util-to-markdown ` already.
47
+
48
+ When working with ` mdast-util-from-markdown ` , you must combine this package
49
+ with [ ` micromark-extension-mdx ` ] [ mdx ] or [ ` micromark-extension-mdxjs ` ] [ mdxjs ] .
42
50
43
- Use this if you’re dealing with the AST manually and want to support all of MDX.
44
51
Instead of this package, you can also use the extensions separately:
45
52
46
53
* [ ` mdast-util-mdx-expression ` ] ( https://github.com/syntax-tree/mdast-util-mdx-expression )
@@ -50,14 +57,14 @@ Instead of this package, you can also use the extensions separately:
50
57
* [ ` mdast-util-mdxjs-esm ` ] ( https://github.com/syntax-tree/mdast-util-mdxjs-esm )
51
58
— support MDX ESM
52
59
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 ] .
60
+ All these packages are used in [ ` remark-mdx ` ] [ remark-mdx ] , which
61
+ focusses on making it easier to transform content by abstracting these
62
+ internals away .
56
63
57
64
## Install
58
65
59
66
This package is [ ESM only] [ esm ] .
60
- In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
67
+ In Node.js (version 14.14+ and 16.0+), install with [ npm] [ ] :
61
68
62
69
``` sh
63
70
npm install mdast-util-mdx
@@ -262,25 +269,71 @@ Which you can also put inline: {1+1}.
262
269
263
270
## API
264
271
265
- This package exports the identifiers ` mdxFromMarkdown ` and ` mdxToMarkdown ` .
272
+ This package exports the identifiers [ ` mdxFromMarkdown ` ] [ api-mdx-from-markdown ]
273
+ and [ ` mdxToMarkdown ` ] [ api-mdx-to-markdown ] .
266
274
There is no default export.
267
275
268
276
### ` mdxFromMarkdown() `
269
277
270
- Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
278
+ Create an extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ]
279
+ to enable MDX (ESM, JSX, expressions).
280
+
281
+ ###### Returns
282
+
283
+ Extension for ` mdast-util-from-markdown ` to enable MDX
284
+ ([ ` FromMarkdownExtension ` ] [ from-markdown-extension ] ).
271
285
272
- When using the [ syntax extension with ` addResult ` ] [ mdxjs ] , nodes will have
273
- a ` data.estree ` field set to an [ ESTree ] [ ] .
286
+ When using the [ syntax extensions with ` addResult ` ] [ mdxjs ] , ESM and expression
287
+ nodes will have ` data.estree ` fields set to ESTree [ ` Program ` ] [ program ] node .
274
288
275
289
### ` mdxToMarkdown(options?) `
276
290
291
+ Create an extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ]
292
+ to enable MDX (ESM, JSX, expressions).
293
+
277
294
Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
278
295
279
- ##### ` options `
296
+ ###### Parameters
297
+
298
+ * ` options ` ([ ` ToMarkdownOptions ` ] [ api-to-markdown-options ] )
299
+ — configuration
300
+
301
+ ###### Returns
302
+
303
+ Extension for ` mdast-util-to-markdown ` to enable MDX
304
+ ([ ` FromMarkdownExtension ` ] [ to-markdown-extension ] ).
305
+
306
+ ### ` ToMarkdownOptions `
307
+
308
+ Configuration (TypeScript type).
309
+
310
+ ###### Fields
280
311
281
- Configuration (optional).
282
- Currently passes through ` quote ` , ` quoteSmart ` , ` tightSelfClosing ` , and
283
- ` printWidth ` to [ ` mdast-util-mdx-jsx ` ] [ options ] .
312
+ * ` quote ` (` '"' ` or ` "'" ` , default: ` '"' ` )
313
+ — preferred quote to use around attribute values
314
+ * ` quoteSmart ` (` boolean ` , default: ` false ` )
315
+ — use the other quote if that results in less bytes
316
+ * ` tightSelfClosing ` (` boolean ` , default: ` false ` )
317
+ — do not use an extra space when closing self-closing elements: ` <img/> `
318
+ instead of ` <img /> `
319
+ * ` printWidth ` (` number ` , default: ` Infinity ` )
320
+ — try and wrap syntax at this width.
321
+ When set to a finite number (say, ` 80 ` ), the formatter will print
322
+ attributes on separate lines when a tag doesn’t fit on one line.
323
+ The normal behavior is to print attributes with spaces between them instead
324
+ of line endings
325
+
326
+ ## HTML
327
+
328
+ MDX has no representation in HTML.
329
+ Though, when you are dealing with MDX, you will likely go * through* hast.
330
+ You can enable passing MDX through to hast by configuring
331
+ [ ` mdast-util-to-hast ` ] [ mdast-util-to-hast ] with `passThrough: [ 'mdxjsEsm',
332
+ 'mdxFlowExpression', 'mdxJsxFlowElement', 'mdxJsxTextElement', 'mdxTextExpression'] `.
333
+
334
+ ## Syntax
335
+
336
+ See [ Syntax in ` micromark-extension-mdxjs ` ] [ mdxjs ] .
284
337
285
338
## Syntax tree
286
339
@@ -300,9 +353,9 @@ This package is fully typed with [TypeScript][].
300
353
It exports the additional types ` MdxFlowExpression ` , ` MdxTextExpression ` ,
301
354
` MdxjsEsm ` , ` MdxJsxAttributeValueExpression ` , ` MdxJsxAttribute ` ,
302
355
` MdxJsxExpressionAttribute ` , ` MdxJsxFlowElement ` ,
303
- ` MdxJsxTextElement ` , and ` ToMarkdownOptions ` .
356
+ ` MdxJsxTextElement ` , and [ ` ToMarkdownOptions ` ] [ api-to-markdown-options ] .
304
357
305
- It also registers the node types with ` @types/mdast ` .
358
+ It also registers the node types with ` @types/mdast ` and ` @types/hast ` .
306
359
If you’re working with the syntax tree, make sure to import this utility
307
360
somewhere in your types, as that registers the new node types in the tree.
308
361
@@ -325,19 +378,19 @@ visit(tree, (node) => {
325
378
326
379
Projects maintained by the unified collective are compatible with all maintained
327
380
versions of Node.js.
328
- As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
381
+ As of now, that is Node.js 14.14+ and 16.0+.
329
382
Our projects sometimes work with older versions, but this is not guaranteed.
330
383
331
384
This utility works with ` mdast-util-from-markdown ` version 1+ and
332
385
` mdast-util-to-markdown ` version 1+.
333
386
334
387
## Related
335
388
336
- * [ ` remarkjs/ remark-mdx` ] [ remark-mdx ]
389
+ * [ ` remark-mdx ` ] [ remark-mdx ]
337
390
— remark plugin to support MDX
338
- * [ ` micromark/micromark -extension-mdx ` ] [ mdx ]
391
+ * [ ` micromark-extension-mdx ` ] [ mdx ]
339
392
— micromark extension to parse MDX
340
- * [ ` micromark/micromark -extension-mdxjs ` ] [ mdxjs ]
393
+ * [ ` micromark-extension-mdxjs ` ] [ mdxjs ]
341
394
— micromark extension to parse JavaScript-aware MDX
342
395
343
396
## Contribute
@@ -406,14 +459,24 @@ abide by its terms.
406
459
407
460
[ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
408
461
462
+ [ from-markdown-extension ] : https://github.com/syntax-tree/mdast-util-from-markdown#extension
463
+
409
464
[ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
410
465
466
+ [ to-markdown-extension ] : https://github.com/syntax-tree/mdast-util-to-markdown#options
467
+
468
+ [ mdast-util-to-hast ] : https://github.com/syntax-tree/mdast-util-to-hast
469
+
411
470
[ mdx ] : https://github.com/micromark/micromark-extension-mdx
412
471
413
472
[ mdxjs ] : https://github.com/micromark/micromark-extension-mdxjs
414
473
415
474
[ remark-mdx ] : https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx
416
475
417
- [ options ] : https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxtomarkdownoptions
476
+ [ program ] : https://github.com/estree/estree/blob/master/es2015.md#programs
477
+
478
+ [ api-mdx-from-markdown ] : #mdxfrommarkdown
479
+
480
+ [ api-mdx-to-markdown ] : #mdxtomarkdownoptions
418
481
419
- [ estree ] : https://github.com/estree/estree
482
+ [ api-to-markdown-options ] : #tomarkdownoptions
0 commit comments