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.
11
+ [ mdast] [ ] extensions to parse and serialize [ MDX] [ ] ESM ( import/exports) .
12
12
13
13
## Contents
14
14
19
19
* [ API] ( #api )
20
20
* [ ` mdxjsEsmFromMarkdown ` ] ( #mdxjsesmfrommarkdown )
21
21
* [ ` mdxjsEsmToMarkdown ` ] ( #mdxjsesmtomarkdown )
22
+ * [ ` MdxjsEsm ` ] ( #mdxjsesm )
23
+ * [ HTML] ( #html )
24
+ * [ Syntax] ( #syntax )
22
25
* [ Syntax tree] ( #syntax-tree )
23
26
* [ Nodes] ( #nodes )
24
27
* [ Content model] ( #content-model )
30
33
31
34
## What is this?
32
35
33
- This package contains extensions that add support for the ESM syntax enabled
34
- by MDX to [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
35
- [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
36
+ This package contains two extensions that add support for MDX ESM syntax in
37
+ markdown to [ mdast] [ ] .
38
+ These extensions plug into
39
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] (to support parsing
40
+ ESM in markdown into a syntax tree) and
41
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] (to support serializing
42
+ ESM in syntax trees to markdown).
36
43
37
44
## When to use this
38
45
39
- These tools are all rather low-level.
40
- In most cases, you’d want to use [ ` remark-mdx ` ] [ remark-mdx ] with remark instead.
46
+ You can use these extensions when you are working with
47
+ ` mdast-util-from-markdown ` and ` mdast-util-to-markdown ` already.
48
+
49
+ When working with ` mdast-util-from-markdown ` , you must combine this package
50
+ with [ ` micromark-extension-mdxjs-esm ` ] [ extension ] .
41
51
42
52
When you are working with syntax trees and want all of MDX, use
43
53
[ ` mdast-util-mdx ` ] [ mdast-util-mdx ] instead.
44
54
45
- When working with ` mdast-util-from-markdown ` , you must combine this package with
46
- [ ` micromark-extension-mdxjs-esm ` ] [ extension ] .
55
+ All these packages are used in [ ` remark-mdx ` ] [ remark-mdx ] , which
56
+ focusses on making it easier to transform content by abstracting these
57
+ internals away.
47
58
48
59
## Install
49
60
50
61
This package is [ ESM only] [ esm ] .
51
- In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
62
+ In Node.js (version 14.14+ and 16.0+), install with [ npm] [ ] :
52
63
53
64
``` sh
54
65
npm install mdast-util-mdxjs-esm
161
172
162
173
## API
163
174
164
- This package exports the identifiers ` mdxjsEsmFromMarkdown ` and
165
- ` mdxjsEsmToMarkdown ` .
175
+ This package exports the identifiers
176
+ [ ` mdxjsEsmFromMarkdown ` ] [ api-mdxjs-esm-from-markdown ] and
177
+ [ ` mdxjsEsmToMarkdown ` ] [ api-mdxjs-esm-to-markdown ] .
166
178
There is no default export.
167
179
168
180
### ` mdxjsEsmFromMarkdown `
169
181
170
- Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
182
+ Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] to enable
183
+ MDX ESM.
171
184
172
- When using the [ syntax extension with ` addResult ` ] [ extension ] , nodes will have
173
- a ` data.estree ` field set to an [ ESTree] [ ] .
185
+ When using the [ micromark syntax extension] [ extension ] with ` addResult ` , nodes
186
+ will have a ` data.estree ` field set to an ESTree [ ` Program ` ] [ program ] node .
174
187
175
188
### ` mdxjsEsmToMarkdown `
176
189
177
- Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
190
+ Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] to enable MDX
191
+ ESM.
192
+
193
+ ### ` MdxjsEsm `
194
+
195
+ MDX ESM (import/export) node (TypeScript type).
196
+
197
+ ###### Type
198
+
199
+ ``` ts
200
+ import type {Literal } from ' mdast'
201
+ import type {Program } from ' estree-jsx'
202
+
203
+ interface MdxjsEsm extends Literal {
204
+ type: ' mdxjsEsm'
205
+ data? : {estree? : Program | null | undefined }
206
+ }
207
+ ```
208
+
209
+ ## HTML
210
+
211
+ MDX ESM has no representation in HTML.
212
+ Though, when you are dealing with MDX, you will likely go * through* hast.
213
+ You can enable passing MDX ESM through to hast by configuring
214
+ [ ` mdast-util-to-hast ` ] [ mdast-util-to-hast ] with ` passThrough: ['mdxjsEsm'] ` .
215
+
216
+ ## Syntax
217
+
218
+ See [ Syntax in ` micromark-extension-mdxjs-esm ` ] [ syntax ] .
178
219
179
220
## Syntax tree
180
221
181
222
The following interfaces are added to ** [ mdast] [ ] ** by this utility.
182
223
183
224
### Nodes
184
225
185
- #### ` MDXJSEsm `
226
+ #### ` MdxjsEsm `
186
227
187
228
``` idl
188
- interface MDXJSEsm <: Literal {
229
+ interface MdxjsEsm <: Literal {
189
230
type: "mdxjsEsm"
190
231
}
191
232
```
192
233
193
- ** MDXJSEsm ** (** [ Literal] [ dfn-literal ] ** ) represents ESM import/exports embedded
194
- in MDX.
234
+ ** MdxjsEsm ** (** [ Literal] [ dfn-literal ] ** ) represents ESM import/exports
235
+ embedded in MDX.
195
236
It can be used where ** [ flow] [ dfn-flow-content ] ** content is expected.
196
237
Its content is represented by its ` value ` field.
197
238
@@ -215,7 +256,7 @@ Yields:
215
256
#### ` FlowContent ` (MDX.js ESM)
216
257
217
258
``` idl
218
- type FlowContentMdxjsEsm = MDXJSEsm | FlowContent
259
+ type FlowContentMdxjsEsm = MdxjsEsm | FlowContent
219
260
```
220
261
221
262
Note that when ESM is present, it can only exist as top-level content: if it has
@@ -224,9 +265,9 @@ a *[parent][dfn-parent]*, that parent must be **[Root][dfn-root]**.
224
265
## Types
225
266
226
267
This package is fully typed with [ TypeScript] [ ] .
227
- It exports the additional type ` MdxjsEsm ` .
268
+ It exports the additional type [ ` MdxjsEsm ` ] [ api-mdxjs-esm ] .
228
269
229
- It also registers the node types with ` @types/mdast ` .
270
+ It also registers the node type with ` @types/mdast ` .
230
271
If you’re working with the syntax tree, make sure to import this utility
231
272
somewhere in your types, as that registers the new node types in the tree.
232
273
@@ -249,7 +290,7 @@ visit(tree, (node) => {
249
290
250
291
Projects maintained by the unified collective are compatible with all maintained
251
292
versions of Node.js.
252
- As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
293
+ As of now, that is Node.js 14.14+ and 16.0+.
253
294
Our projects sometimes work with older versions, but this is not guaranteed.
254
295
255
296
This plugin works with ` mdast-util-from-markdown ` version 1+ and
@@ -328,6 +369,8 @@ abide by its terms.
328
369
329
370
[ mdast ] : https://github.com/syntax-tree/mdast
330
371
372
+ [ mdast-util-to-hast ] : https://github.com/syntax-tree/mdast-util-to-hast
373
+
331
374
[ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
332
375
333
376
[ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
@@ -336,7 +379,9 @@ abide by its terms.
336
379
337
380
[ extension ] : https://github.com/micromark/micromark-extension-mdxjs-esm
338
381
339
- [ estree ] : https://github.com/estree/estree
382
+ [ syntax ] : https://github.com/micromark/micromark-extension-mdxjs-esm#syntax
383
+
384
+ [ program ] : https://github.com/estree/estree/blob/master/es2015.md#programs
340
385
341
386
[ dfn-literal ] : https://github.com/syntax-tree/mdast#literal
342
387
@@ -349,3 +394,9 @@ abide by its terms.
349
394
[ remark-mdx ] : https://mdxjs.com/packages/remark-mdx/
350
395
351
396
[ mdx ] : https://mdxjs.com
397
+
398
+ [ api-mdxjs-esm-from-markdown ] : #mdxjsesmfrommarkdown
399
+
400
+ [ api-mdxjs-esm-to-markdown ] : #mdxjsesmtomarkdown
401
+
402
+ [ api-mdxjs-esm ] : #mdxjsesm
0 commit comments