Skip to content

Commit 66245a8

Browse files
committed
Add improved docs
1 parent f01210d commit 66245a8

File tree

1 file changed

+118
-51
lines changed

1 file changed

+118
-51
lines changed

readme.md

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

11-
Extension for [`mdast-util-from-markdown`][from-markdown] and/or
12-
[`mdast-util-to-markdown`][to-markdown] to support MDX.js ESM import/exports.
13-
When parsing (`from-markdown`), must be combined with
14-
[`micromark-extension-mdxjs-esm`][extension].
15-
16-
This utility handles parsing and serializing.
17-
See [`micromark-extension-mdxjs-esm`][extension] for how the syntax works.
11+
[mdast][] extensions to parse and serialize [MDX][] ESM import/exports.
12+
13+
## Contents
14+
15+
* [What is this?](#what-is-this)
16+
* [When to use this](#when-to-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`mdxjsEsmFromMarkdown`](#mdxjsesmfrommarkdown)
21+
* [`mdxjsEsmToMarkdown`](#mdxjsesmtomarkdown)
22+
* [Syntax tree](#syntax-tree)
23+
* [Nodes](#nodes)
24+
* [Content model](#content-model)
25+
* [Types](#types)
26+
* [Compatibility](#compatibility)
27+
* [Related](#related)
28+
* [Contribute](#contribute)
29+
* [License](#license)
30+
31+
## What is this?
32+
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].
1836

1937
## When to use this
2038

21-
Use [`mdast-util-mdx`][mdast-util-mdx] if you want all of MDX / MDX.js.
22-
Use this otherwise.
39+
These tools are all rather low-level.
40+
In most cases, you’d want to use [`remark-mdx`][remark-mdx] with remark instead.
2341

24-
## Install
42+
When you are working with syntax trees and want all of MDX, use
43+
[`mdast-util-mdx`][mdast-util-mdx] instead.
44+
45+
When working with `mdast-util-from-markdown`, you must combine this package with
46+
[`micromark-extension-mdxjs-esm`][extension].
2547

26-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
27-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
48+
## Install
2849

29-
[npm][]:
50+
This package is [ESM only][esm].
51+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
3052

3153
```sh
3254
npm install mdast-util-mdxjs-esm
3355
```
3456

57+
In Deno with [`esm.sh`][esmsh]:
58+
59+
```js
60+
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'https://esm.sh/mdast-util-mdxjs-esm@1'
61+
```
62+
63+
In browsers with [`esm.sh`][esmsh]:
64+
65+
```html
66+
<script type="module">
67+
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'https://esm.sh/mdast-util-mdxjs-esm@1?bundle'
68+
</script>
69+
```
70+
3571
## Use
3672

37-
Say we have an MDX.js file, `example.mdx`:
73+
Say our document `example.mdx` contains:
3874

3975
```mdx
4076
import a from 'b'
41-
export var c = ''
77+
export const c = ''
4278

4379
d
4480
```
4581

46-
And our module, `example.js`, looks as follows:
82+
…and our module `example.js` looks as follows:
4783

4884
```js
49-
import fs from 'fs'
85+
import fs from 'node:fs/promises'
5086
import * as acorn from 'acorn'
5187
import {fromMarkdown} from 'mdast-util-from-markdown'
5288
import {toMarkdown} from 'mdast-util-to-markdown'
5389
import {mdxjsEsm} from 'micromark-extension-mdxjs-esm'
5490
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'mdast-util-mdxjs-esm'
5591

56-
const doc = fs.readFileSync('example.mdx')
92+
const doc = await fs.readFile('example.mdx')
5793

5894
const tree = fromMarkdown(doc, {
5995
extensions: [mdxjsEsm({acorn, addResult: true})],
@@ -67,15 +103,15 @@ const out = toMarkdown(tree, {extensions: [mdxjsEsmToMarkdown]})
67103
console.log(out)
68104
```
69105

70-
Now, running `node example` yields (positional info removed for brevity):
106+
…now running `node example.js` yields (positional info removed for brevity):
71107

72108
```js
73109
{
74110
type: 'root',
75111
children: [
76112
{
77113
type: 'mdxjsEsm',
78-
value: "import a from 'b'\nexport var c = ''",
114+
value: "import a from 'b'\nexport const c = ''",
79115
data: {
80116
estree: {
81117
type: 'Program',
@@ -101,7 +137,7 @@ Now, running `node example` yields (positional info removed for brevity):
101137
init: {type: 'Literal', value: '', raw: "''"}
102138
}
103139
],
104-
kind: 'var'
140+
kind: 'const'
105141
},
106142
specifiers: [],
107143
source: null
@@ -118,28 +154,27 @@ Now, running `node example` yields (positional info removed for brevity):
118154

119155
```markdown
120156
import a from 'b'
121-
export var c = ''
157+
export const c = ''
122158

123159
d
124160
```
125161

126162
## API
127163

128-
This package exports the following identifier: `mdxjsEsmFromMarkdown`,
164+
This package exports the identifiers `mdxjsEsmFromMarkdown` and
129165
`mdxjsEsmToMarkdown`.
130166
There is no default export.
131167

132168
### `mdxjsEsmFromMarkdown`
133169

134-
### `mdxjsEsmToMarkdown`
135-
136-
Support MDX.js ESM import/exports.
137-
The exports are extensions, respectively for
138-
[`mdast-util-from-markdown`][from-markdown] and
139-
[`mdast-util-to-markdown`][to-markdown].
170+
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].
140171

141172
When using the [syntax extension with `addResult`][extension], nodes will have
142-
a `data.estree` field set to an [ESTree][]
173+
a `data.estree` field set to an [ESTree][].
174+
175+
### `mdxjsEsmToMarkdown`
176+
177+
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].
143178

144179
## Syntax tree
145180

@@ -186,27 +221,53 @@ type FlowContentMdxjsEsm = MDXJSEsm | FlowContent
186221
Note that when ESM is present, it can only exist as top-level content: if it has
187222
a *[parent][dfn-parent]*, that parent must be **[Root][dfn-root]**.
188223

224+
## Types
225+
226+
This package is fully typed with [TypeScript][].
227+
It exports the additional type `MdxjsEsm`.
228+
229+
It also registers the node types with `@types/mdast`.
230+
If you’re working with the syntax tree, make sure to import this utility
231+
somewhere in your types, as that registers the new node types in the tree.
232+
233+
```js
234+
/**
235+
* @typedef {import('mdast-util-mdxjs-esm')}
236+
*/
237+
238+
import {visit} from 'unist-util-visit'
239+
240+
/** @type {import('mdast').Root} */
241+
const tree = getMdastNodeSomeHow()
242+
243+
visit(tree, (node) => {
244+
// `node` can now be an ESM node.
245+
})
246+
```
247+
248+
## Compatibility
249+
250+
Projects maintained by the unified collective are compatible with all maintained
251+
versions of Node.js.
252+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
253+
Our projects sometimes work with older versions, but this is not guaranteed.
254+
255+
This plugin works with `mdast-util-from-markdown` version 1+ and
256+
`mdast-util-to-markdown` version 1+.
257+
189258
## Related
190259

191-
* [`remarkjs/remark`][remark]
192-
— markdown processor powered by plugins
193260
* [`remarkjs/remark-mdx`][remark-mdx]
194261
— remark plugin to support MDX
195-
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
196-
— mdast parser using `micromark` to create mdast from markdown
197-
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
198-
— mdast serializer to create markdown from mdast
199262
* [`syntax-tree/mdast-util-mdx`][mdast-util-mdx]
200263
— mdast utility to support MDX
201-
* [`micromark/micromark`][micromark]
202-
— the smallest commonmark-compliant markdown parser that exists
203264
* [`micromark/micromark-extension-mdxjs-esm`][extension]
204265
— micromark extension to parse MDX.js ESM
205266

206267
## Contribute
207268

208-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
209-
started.
269+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
270+
ways to get started.
210271
See [`support.md`][support] for ways to get help.
211272

212273
This project has a [code of conduct][coc].
@@ -247,30 +308,34 @@ abide by its terms.
247308

248309
[npm]: https://docs.npmjs.com/cli/install
249310

311+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
312+
313+
[esmsh]: https://esm.sh
314+
315+
[typescript]: https://www.typescriptlang.org
316+
250317
[license]: license
251318

252319
[author]: https://wooorm.com
253320

254-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
321+
[health]: https://github.com/syntax-tree/.github
255322

256-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
323+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
257324

258-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
325+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
259326

260-
[mdast]: https://github.com/syntax-tree/mdast
327+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
261328

262-
[remark]: https://github.com/remarkjs/remark
329+
[mdast]: https://github.com/syntax-tree/mdast
263330

264-
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
331+
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
265332

266-
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
333+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
267334

268-
[micromark]: https://github.com/micromark/micromark
335+
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx
269336

270337
[extension]: https://github.com/micromark/micromark-extension-mdxjs-esm
271338

272-
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx
273-
274339
[estree]: https://github.com/estree/estree
275340

276341
[dfn-literal]: https://github.com/syntax-tree/mdast#literal
@@ -281,4 +346,6 @@ abide by its terms.
281346

282347
[dfn-flow-content]: #flowcontent-mdxjs-esm
283348

284-
[remark-mdx]: https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx
349+
[remark-mdx]: https://mdxjs.com/packages/remark-mdx/
350+
351+
[mdx]: https://mdxjs.com

0 commit comments

Comments
 (0)