8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ mdast] [ ] extensions to parse and serialize [ MDX] [ ] expressions.
11
+ [ mdast] [ ] extensions to parse and serialize [ MDX] [ ] expressions ( ` {Math.PI} ` ) .
12
12
13
13
## Contents
14
14
19
19
* [ API] ( #api )
20
20
* [ ` mdxExpressionFromMarkdown ` ] ( #mdxexpressionfrommarkdown )
21
21
* [ ` mdxExpressionToMarkdown ` ] ( #mdxexpressiontomarkdown )
22
+ * [ ` MdxFlowExpression ` ] ( #mdxflowexpression )
23
+ * [ ` MdxTextExpression ` ] ( #mdxtextexpression )
24
+ * [ ` MdxFlowExpressionHast ` ] ( #mdxflowexpressionhast )
25
+ * [ ` MdxTextExpressionHast ` ] ( #mdxtextexpressionhast )
26
+ * [ HTML] ( #html )
27
+ * [ Syntax] ( #syntax )
22
28
* [ Syntax tree] ( #syntax-tree )
23
29
* [ Nodes] ( #nodes )
24
30
* [ Content model] ( #content-model )
30
36
31
37
## What is this?
32
38
33
- This package contains extensions that add support for the expression syntax
34
- enabled by MDX to [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] and
35
- [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
39
+ This package contains two extensions that add support for MDX expression syntax
40
+ in markdown to [ mdast] [ ] .
41
+ These extensions plug into
42
+ [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] (to support parsing
43
+ expressions in markdown into a syntax tree) and
44
+ [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] (to support serializing
45
+ expressions in syntax trees to markdown).
36
46
37
47
## When to use this
38
48
39
- These tools are all rather low-level.
40
- In most cases, you’d want to use [ ` remark-mdx ` ] [ remark-mdx ] with remark instead.
49
+ You can use these extensions when you are working with
50
+ ` mdast-util-from-markdown ` and ` mdast-util-to-markdown ` already.
51
+
52
+ When working with ` mdast-util-from-markdown ` , you must combine this package
53
+ with [ ` micromark-extension-mdx-expression ` ] [ extension ] .
41
54
42
55
When you are working with syntax trees and want all of MDX, use
43
56
[ ` mdast-util-mdx ` ] [ mdast-util-mdx ] instead.
44
57
45
- When working with ` mdast-util-from-markdown ` , you must combine this package with
46
- [ ` micromark-extension-mdx-expression ` ] [ extension ] .
58
+ All these packages are used in [ ` remark-mdx ` ] [ remark-mdx ] , which
59
+ focusses on making it easier to transform content by abstracting these
60
+ internals away.
47
61
48
62
## Install
49
63
50
64
This package is [ ESM only] [ esm ] .
51
- In Node.js (version 12.20+, 14.14+, or 16.0+), install with [ npm] [ ] :
65
+ In Node.js (version 14.14+ and 16.0+), install with [ npm] [ ] :
52
66
53
67
``` sh
54
68
npm install mdast-util-mdx-expression
@@ -168,36 +182,117 @@ b {true}.
168
182
169
183
## API
170
184
171
- This package exports the identifiers ` mdxExpressionFromMarkdown ` and
172
- ` mdxExpressionToMarkdown ` .
185
+ This package exports the identifiers
186
+ [ ` mdxExpressionFromMarkdown ` ] [ api-mdx-expression-from-markdown ] and
187
+ [ ` mdxExpressionToMarkdown ` ] [ api-mdx-expression-to-markdown ] .
173
188
There is no default export.
174
189
175
190
### ` mdxExpressionFromMarkdown `
176
191
177
- Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] .
192
+ Extension for [ ` mdast-util-from-markdown ` ] [ mdast-util-from-markdown ] to enable
193
+ MDX expressions.
178
194
179
- When using the [ syntax extension with ` addResult ` ] [ extension ] , nodes will have
180
- a ` data.estree ` field set to an [ ESTree] [ ] .
195
+ When using the [ micromark syntax extension] [ extension ] with ` addResult ` , nodes
196
+ will have a ` data.estree ` field set to an ESTree [ ` Program ` ] [ program ] node .
181
197
182
198
### ` mdxExpressionToMarkdown `
183
199
184
- Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] .
200
+ Extension for [ ` mdast-util-to-markdown ` ] [ mdast-util-to-markdown ] to enable MDX
201
+ expressions.
202
+
203
+ ### ` MdxFlowExpression `
204
+
205
+ MDX expression node, occurring in flow (block) (TypeScript type).
206
+
207
+ ###### Type
208
+
209
+ ``` ts
210
+ import type {Program } from ' estree-jsx'
211
+ import type {Literal } from ' mdast'
212
+
213
+ interface MdxFlowExpression extends Literal {
214
+ type: ' mdxFlowExpression'
215
+ data? : {estree? : Program | null | undefined } & Literal [' data' ]
216
+ }
217
+ ```
218
+
219
+ ### ` MdxTextExpression `
220
+
221
+ MDX expression node, occurring in text (block) (TypeScript type).
222
+
223
+ ###### Type
224
+
225
+ ``` ts
226
+ import type {Program } from ' estree-jsx'
227
+ import type {Literal } from ' mdast'
228
+
229
+ interface MdxTextExpression extends Literal {
230
+ type: ' mdxTextExpression'
231
+ data? : {estree? : Program | null | undefined } & Literal [' data' ]
232
+ }
233
+ ```
234
+
235
+ ### ` MdxFlowExpressionHast `
236
+
237
+ Same as [ ` MdxFlowExpression ` ] [ api-mdx-flow-expression ] , but registered with
238
+ ` @types/hast ` (TypeScript type).
239
+
240
+ ###### Type
241
+
242
+ ``` ts
243
+ import type {Program } from ' estree-jsx'
244
+ import type {Literal } from ' hast'
245
+
246
+ interface MdxFlowExpressionHast extends Literal {
247
+ type: ' mdxFlowExpression'
248
+ data? : {estree? : Program | null | undefined } & Literal [' data' ]
249
+ }
250
+ ```
251
+
252
+ ### ` MdxTextExpressionHast `
253
+
254
+ Same as [ ` MdxTextExpression ` ] [ api-mdx-text-expression ] , but registered with
255
+ ` @types/hast ` (TypeScript type).
256
+
257
+ ###### Type
258
+
259
+ ``` ts
260
+ import type {Program } from ' estree-jsx'
261
+ import type {Literal } from ' hast'
262
+
263
+ interface MdxTextExpressionHast extends Literal {
264
+ type: ' mdxTextExpression'
265
+ data? : {estree? : Program | null | undefined } & Literal [' data' ]
266
+ }
267
+ ```
268
+
269
+ ## HTML
270
+
271
+ MDX expressions have no representation in HTML.
272
+ Though, when you are dealing with MDX, you will likely go * through* hast.
273
+ You can enable passing MDX expressions through to hast by configuring
274
+ [ ` mdast-util-to-hast ` ] [ mdast-util-to-hast ] with
275
+ ` passThrough: ['mdxFlowExpression', 'mdxTextExpression'] ` .
276
+
277
+ ## Syntax
278
+
279
+ See [ Syntax in ` micromark-extension-mdx-expression ` ] [ syntax ] .
185
280
186
281
## Syntax tree
187
282
188
283
The following interfaces are added to ** [ mdast] [ ] ** by this utility.
189
284
190
285
### Nodes
191
286
192
- #### ` MDXFlowExpression `
287
+ #### ` MdxFlowExpression `
193
288
194
289
``` idl
195
- interface MDXFlowExpression <: Literal {
290
+ interface MdxFlowExpression <: Literal {
196
291
type: "mdxFlowExpression"
197
292
}
198
293
```
199
294
200
- ** MDXFlowExpression ** (** [ Literal] [ dfn-literal ] ** ) represents a JavaScript
295
+ ** MdxFlowExpression ** (** [ Literal] [ dfn-literal ] ** ) represents a JavaScript
201
296
expression embedded in flow (block).
202
297
It can be used where ** [ flow] [ dfn-flow-content ] ** content is expected.
203
298
Its content is represented by its ` value ` field.
@@ -216,15 +311,15 @@ Yields:
216
311
{type: ' mdxFlowExpression' , value: ' \n 1 + 1\n ' }
217
312
```
218
313
219
- #### ` MDXTextExpression `
314
+ #### ` MdxTextExpression `
220
315
221
316
``` idl
222
- interface MDXTextExpression <: Literal {
317
+ interface MdxTextExpression <: Literal {
223
318
type: "mdxTextExpression"
224
319
}
225
320
```
226
321
227
- ** MDXTextExpression ** (** [ Literal] [ dfn-literal ] ** ) represents a JavaScript
322
+ ** MdxTextExpression ** (** [ Literal] [ dfn-literal ] ** ) represents a JavaScript
228
323
expression embedded in text (span, inline).
229
324
It can be used where ** [ phrasing] [ dfn-phrasing-content ] ** content is expected.
230
325
Its content is represented by its ` value ` field.
@@ -246,21 +341,24 @@ Yields:
246
341
#### ` FlowContent ` (MDX expression)
247
342
248
343
``` idl
249
- type FlowContentMdxExpression = MDXFlowExpression | FlowContent
344
+ type FlowContentMdxExpression = MdxFlowExpression | FlowContent
250
345
```
251
346
252
347
#### ` PhrasingContent ` (MDX expression)
253
348
254
349
``` idl
255
- type PhrasingContentMdxExpression = MDXTextExpression | PhrasingContent
350
+ type PhrasingContentMdxExpression = MdxTextExpression | PhrasingContent
256
351
```
257
352
258
353
## Types
259
354
260
355
This package is fully typed with [ TypeScript] [ ] .
261
- It exports the additional types ` MdxFlowExpression ` and ` MdxTextExpression ` .
356
+ It exports the additional types [ ` MdxFlowExpression ` ] [ api-mdx-flow-expression ] ,
357
+ [ ` MdxFlowExpressionHast ` ] [ api-mdx-flow-expression-hast ] ,
358
+ [ ` MdxTextExpression ` ] [ api-mdx-text-expression ] , and
359
+ [ ` MdxTextExpressionHast ` ] [ api-mdx-text-expression-hast ] .
262
360
263
- It also registers the node types with ` @types/mdast ` .
361
+ It also registers the node types with ` @types/mdast ` and ` @types/hast ` .
264
362
If you’re working with the syntax tree, make sure to import this utility
265
363
somewhere in your types, as that registers the new node types in the tree.
266
364
@@ -283,7 +381,7 @@ visit(tree, (node) => {
283
381
284
382
Projects maintained by the unified collective are compatible with all maintained
285
383
versions of Node.js.
286
- As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
384
+ As of now, that is Node.js 14.14+ and 16.0+.
287
385
Our projects sometimes work with older versions, but this is not guaranteed.
288
386
289
387
This plugin works with ` mdast-util-from-markdown ` version 1+ and
@@ -362,6 +460,8 @@ abide by its terms.
362
460
363
461
[ mdast ] : https://github.com/syntax-tree/mdast
364
462
463
+ [ mdast-util-to-hast ] : https://github.com/syntax-tree/mdast-util-to-hast
464
+
365
465
[ mdast-util-from-markdown ] : https://github.com/syntax-tree/mdast-util-from-markdown
366
466
367
467
[ mdast-util-to-markdown ] : https://github.com/syntax-tree/mdast-util-to-markdown
@@ -370,14 +470,28 @@ abide by its terms.
370
470
371
471
[ extension ] : https://github.com/micromark/micromark-extension-mdx-expression
372
472
373
- [ estree ] : https://github.com/estree/estree
473
+ [ syntax ] : https://github.com/micromark/micromark-extension-mdx-expression#syntax
374
474
375
- [ dfn-literal ] : https://github.com/syntax-tree/mdast#literal
376
-
377
- [ dfn-flow-content ] : #flowcontent-mdx-expression
475
+ [ program ] : https://github.com/estree/estree/blob/master/es2015.md#programs
378
476
379
- [ dfn-phrasing-content ] : #phrasingcontent-mdx-expression
477
+ [ dfn-literal ] : https://github.com/syntax-tree/mdast#literal
380
478
381
479
[ remark-mdx ] : https://mdxjs.com/packages/remark-mdx/
382
480
383
481
[ mdx ] : https://mdxjs.com
482
+
483
+ [ api-mdx-expression-from-markdown ] : #mdxexpressionfrommarkdown
484
+
485
+ [ api-mdx-expression-to-markdown ] : #mdxexpressiontomarkdown
486
+
487
+ [ api-mdx-flow-expression ] : #mdxflowexpression
488
+
489
+ [ api-mdx-text-expression ] : #mdxtextexpression
490
+
491
+ [ api-mdx-flow-expression-hast ] : #mdxflowexpressionhast
492
+
493
+ [ api-mdx-text-expression-hast ] : #mdxtextexpressionhast
494
+
495
+ [ dfn-flow-content ] : #flowcontent-mdx-expression
496
+
497
+ [ dfn-phrasing-content ] : #phrasingcontent-mdx-expression
0 commit comments