Skip to content

Commit 4ada83d

Browse files
committed
Add improved docs
1 parent 9ee5466 commit 4ada83d

File tree

1 file changed

+145
-31
lines changed

1 file changed

+145
-31
lines changed

readme.md

Lines changed: 145 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[mdast][] extensions to parse and serialize [MDX][] expressions.
11+
[mdast][] extensions to parse and serialize [MDX][] expressions (`{Math.PI}`).
1212

1313
## Contents
1414

@@ -19,6 +19,12 @@
1919
* [API](#api)
2020
* [`mdxExpressionFromMarkdown`](#mdxexpressionfrommarkdown)
2121
* [`mdxExpressionToMarkdown`](#mdxexpressiontomarkdown)
22+
* [`MdxFlowExpression`](#mdxflowexpression)
23+
* [`MdxTextExpression`](#mdxtextexpression)
24+
* [`MdxFlowExpressionHast`](#mdxflowexpressionhast)
25+
* [`MdxTextExpressionHast`](#mdxtextexpressionhast)
26+
* [HTML](#html)
27+
* [Syntax](#syntax)
2228
* [Syntax tree](#syntax-tree)
2329
* [Nodes](#nodes)
2430
* [Content model](#content-model)
@@ -30,25 +36,33 @@
3036

3137
## What is this?
3238

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).
3646

3747
## When to use this
3848

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].
4154

4255
When you are working with syntax trees and want all of MDX, use
4356
[`mdast-util-mdx`][mdast-util-mdx] instead.
4457

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.
4761

4862
## Install
4963

5064
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][]:
5266

5367
```sh
5468
npm install mdast-util-mdx-expression
@@ -168,36 +182,117 @@ b {true}.
168182

169183
## API
170184

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].
173188
There is no default export.
174189

175190
### `mdxExpressionFromMarkdown`
176191

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.
178194

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.
181197

182198
### `mdxExpressionToMarkdown`
183199

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].
185280

186281
## Syntax tree
187282

188283
The following interfaces are added to **[mdast][]** by this utility.
189284

190285
### Nodes
191286

192-
#### `MDXFlowExpression`
287+
#### `MdxFlowExpression`
193288

194289
```idl
195-
interface MDXFlowExpression <: Literal {
290+
interface MdxFlowExpression <: Literal {
196291
type: "mdxFlowExpression"
197292
}
198293
```
199294

200-
**MDXFlowExpression** (**[Literal][dfn-literal]**) represents a JavaScript
295+
**MdxFlowExpression** (**[Literal][dfn-literal]**) represents a JavaScript
201296
expression embedded in flow (block).
202297
It can be used where **[flow][dfn-flow-content]** content is expected.
203298
Its content is represented by its `value` field.
@@ -216,15 +311,15 @@ Yields:
216311
{type: 'mdxFlowExpression', value: '\n1 + 1\n'}
217312
```
218313

219-
#### `MDXTextExpression`
314+
#### `MdxTextExpression`
220315

221316
```idl
222-
interface MDXTextExpression <: Literal {
317+
interface MdxTextExpression <: Literal {
223318
type: "mdxTextExpression"
224319
}
225320
```
226321

227-
**MDXTextExpression** (**[Literal][dfn-literal]**) represents a JavaScript
322+
**MdxTextExpression** (**[Literal][dfn-literal]**) represents a JavaScript
228323
expression embedded in text (span, inline).
229324
It can be used where **[phrasing][dfn-phrasing-content]** content is expected.
230325
Its content is represented by its `value` field.
@@ -246,21 +341,24 @@ Yields:
246341
#### `FlowContent` (MDX expression)
247342

248343
```idl
249-
type FlowContentMdxExpression = MDXFlowExpression | FlowContent
344+
type FlowContentMdxExpression = MdxFlowExpression | FlowContent
250345
```
251346

252347
#### `PhrasingContent` (MDX expression)
253348

254349
```idl
255-
type PhrasingContentMdxExpression = MDXTextExpression | PhrasingContent
350+
type PhrasingContentMdxExpression = MdxTextExpression | PhrasingContent
256351
```
257352

258353
## Types
259354

260355
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].
262360

263-
It also registers the node types with `@types/mdast`.
361+
It also registers the node types with `@types/mdast` and `@types/hast`.
264362
If you’re working with the syntax tree, make sure to import this utility
265363
somewhere in your types, as that registers the new node types in the tree.
266364

@@ -283,7 +381,7 @@ visit(tree, (node) => {
283381

284382
Projects maintained by the unified collective are compatible with all maintained
285383
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+.
287385
Our projects sometimes work with older versions, but this is not guaranteed.
288386

289387
This plugin works with `mdast-util-from-markdown` version 1+ and
@@ -362,6 +460,8 @@ abide by its terms.
362460

363461
[mdast]: https://github.com/syntax-tree/mdast
364462

463+
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast
464+
365465
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
366466

367467
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
@@ -370,14 +470,28 @@ abide by its terms.
370470

371471
[extension]: https://github.com/micromark/micromark-extension-mdx-expression
372472

373-
[estree]: https://github.com/estree/estree
473+
[syntax]: https://github.com/micromark/micromark-extension-mdx-expression#syntax
374474

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
378476

379-
[dfn-phrasing-content]: #phrasingcontent-mdx-expression
477+
[dfn-literal]: https://github.com/syntax-tree/mdast#literal
380478

381479
[remark-mdx]: https://mdxjs.com/packages/remark-mdx/
382480

383481
[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

Comments
 (0)