Skip to content

Commit 671dee9

Browse files
committed
Add improved docs
1 parent c8cc75a commit 671dee9

File tree

2 files changed

+80
-24
lines changed

2 files changed

+80
-24
lines changed

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
* @typedef {import('../index.js').MdxjsEsm} MdxjsEsm
1212
*/
1313

14+
// To do: next major: expose functions.
15+
1416
/**
1517
* Extension for `mdast-util-from-markdown` to enable MDX ESM.
1618
*
19+
* When using the syntax extension with `addResult`, nodes will have a
20+
* `data.estree` field set to an ESTree `Program` node.
21+
*
1722
* @type {FromMarkdownExtension}
1823
*/
1924
export const mdxjsEsmFromMarkdown = {

readme.md

Lines changed: 75 additions & 24 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][] ESM import/exports.
11+
[mdast][] extensions to parse and serialize [MDX][] ESM (import/exports).
1212

1313
## Contents
1414

@@ -19,6 +19,9 @@
1919
* [API](#api)
2020
* [`mdxjsEsmFromMarkdown`](#mdxjsesmfrommarkdown)
2121
* [`mdxjsEsmToMarkdown`](#mdxjsesmtomarkdown)
22+
* [`MdxjsEsm`](#mdxjsesm)
23+
* [HTML](#html)
24+
* [Syntax](#syntax)
2225
* [Syntax tree](#syntax-tree)
2326
* [Nodes](#nodes)
2427
* [Content model](#content-model)
@@ -30,25 +33,33 @@
3033

3134
## What is this?
3235

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

3744
## When to use this
3845

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

4252
When you are working with syntax trees and want all of MDX, use
4353
[`mdast-util-mdx`][mdast-util-mdx] instead.
4454

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

4859
## Install
4960

5061
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][]:
5263

5364
```sh
5465
npm install mdast-util-mdxjs-esm
@@ -161,37 +172,67 @@ d
161172

162173
## API
163174

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].
166178
There is no default export.
167179

168180
### `mdxjsEsmFromMarkdown`
169181

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

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

175188
### `mdxjsEsmToMarkdown`
176189

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

179220
## Syntax tree
180221

181222
The following interfaces are added to **[mdast][]** by this utility.
182223

183224
### Nodes
184225

185-
#### `MDXJSEsm`
226+
#### `MdxjsEsm`
186227

187228
```idl
188-
interface MDXJSEsm <: Literal {
229+
interface MdxjsEsm <: Literal {
189230
type: "mdxjsEsm"
190231
}
191232
```
192233

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.
195236
It can be used where **[flow][dfn-flow-content]** content is expected.
196237
Its content is represented by its `value` field.
197238

@@ -215,7 +256,7 @@ Yields:
215256
#### `FlowContent` (MDX.js ESM)
216257

217258
```idl
218-
type FlowContentMdxjsEsm = MDXJSEsm | FlowContent
259+
type FlowContentMdxjsEsm = MdxjsEsm | FlowContent
219260
```
220261

221262
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]**.
224265
## Types
225266

226267
This package is fully typed with [TypeScript][].
227-
It exports the additional type `MdxjsEsm`.
268+
It exports the additional type [`MdxjsEsm`][api-mdxjs-esm].
228269

229-
It also registers the node types with `@types/mdast`.
270+
It also registers the node type with `@types/mdast`.
230271
If you’re working with the syntax tree, make sure to import this utility
231272
somewhere in your types, as that registers the new node types in the tree.
232273

@@ -249,7 +290,7 @@ visit(tree, (node) => {
249290

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

255296
This plugin works with `mdast-util-from-markdown` version 1+ and
@@ -328,6 +369,8 @@ abide by its terms.
328369

329370
[mdast]: https://github.com/syntax-tree/mdast
330371

372+
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast
373+
331374
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
332375

333376
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
@@ -336,7 +379,9 @@ abide by its terms.
336379

337380
[extension]: https://github.com/micromark/micromark-extension-mdxjs-esm
338381

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
340385

341386
[dfn-literal]: https://github.com/syntax-tree/mdast#literal
342387

@@ -349,3 +394,9 @@ abide by its terms.
349394
[remark-mdx]: https://mdxjs.com/packages/remark-mdx/
350395

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

Comments
 (0)