Skip to content

Commit bcb401e

Browse files
committed
Add improved docs
1 parent 76b3694 commit bcb401e

File tree

1 file changed

+138
-46
lines changed

1 file changed

+138
-46
lines changed

readme.md

+138-46
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,78 @@
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 (or MDX.js) in
13-
**[mdast][]**.
14-
When parsing (`from-markdown`), must be combined with either
15-
[`micromark-extension-mdx`][mdx] or [`micromark-extension-mdxjs`][mdxjs].
11+
[mdast][] extensions to parse and serialize [MDX][]: ESM import/exports,
12+
JavaScript expressions, and JSX.
13+
14+
## Contents
15+
16+
* [What is this?](#what-is-this)
17+
* [When to use this](#when-to-use-this)
18+
* [Install](#install)
19+
* [Use](#use)
20+
* [API](#api)
21+
* [`mdxFromMarkdown()`](#mdxfrommarkdown)
22+
* [`mdxToMarkdown(options?)`](#mdxtomarkdownoptions)
23+
* [Syntax tree](#syntax-tree)
24+
* [Types](#types)
25+
* [Compatibility](#compatibility)
26+
* [Related](#related)
27+
* [Contribute](#contribute)
28+
* [License](#license)
29+
30+
## What is this?
31+
32+
This package contains extensions for
33+
[`mdast-util-from-markdown`][mdast-util-from-markdown] and
34+
[`mdast-util-to-markdown`][mdast-util-to-markdown] to enable the features that
35+
MDX adds to markdown: import/exports (`export x from 'y'`), expressions
36+
(`{z}`), and JSX (`<Component />`).
1637

1738
## When to use this
1839

40+
These tools are all rather low-level.
41+
In many cases, you’d want to use [`remark-mdx`][remark-mdx] with remark instead.
42+
1943
Use this if you’re dealing with the AST manually and want to support all of MDX.
20-
You can also use the extensions separately:
44+
Instead of this package, you can also use the extensions separately:
2145

2246
* [`mdast-util-mdx-expression`](https://github.com/syntax-tree/mdast-util-mdx-expression)
23-
— support MDX (or MDX.js) expressions
47+
— support MDX expressions
2448
* [`mdast-util-mdx-jsx`](https://github.com/syntax-tree/mdast-util-mdx-jsx)
25-
— support MDX (or MDX.js) JSX
49+
— support MDX JSX
2650
* [`mdast-util-mdxjs-esm`](https://github.com/syntax-tree/mdast-util-mdxjs-esm)
27-
— support MDX.js ESM
51+
— support MDX ESM
2852

29-
## Install
53+
When working with `mdast-util-from-markdown`, you must combine this package with
54+
[`micromark/micromark-extension-mdx`][mdx] or
55+
[`micromark/micromark-extension-mdxjs`][mdxjs].
3056

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

34-
[npm][]:
59+
This package is [ESM only][esm].
60+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
3561

3662
```sh
3763
npm install mdast-util-mdx
3864
```
3965

66+
In Deno with [`esm.sh`][esmsh]:
67+
68+
```js
69+
import {mdxFromMarkdown, mdxToMarkdown} from 'https://esm.sh/mdast-util-mdx@2'
70+
```
71+
72+
In browsers with [`esm.sh`][esmsh]:
73+
74+
```html
75+
<script type="module">
76+
import {mdxFromMarkdown, mdxToMarkdown} from 'https://esm.sh/mdast-util-mdx@2?bundle'
77+
</script>
78+
```
79+
4080
## Use
4181

42-
Say we have the following file, `example.mdx`:
82+
Say our document `example.mdx` contains:
4383

4484
```markdown
4585
import Box from "place"
@@ -59,16 +99,16 @@ Which you can also put inline: {1+1}.
5999
</Box>
60100
```
61101

62-
And our script, `example.js`, looks as follows:
102+
…and our module `example.js` looks as follows:
63103

64104
```js
65-
import fs from 'node:fs'
105+
import fs from 'node:fs/promises'
66106
import {fromMarkdown} from 'mdast-util-from-markdown'
67107
import {toMarkdown} from 'mdast-util-to-markdown'
68108
import {mdxjs} from 'micromark-extension-mdxjs'
69109
import {mdxFromMarkdown, mdxToMarkdown} from 'mdast-util-mdx'
70110

71-
const doc = fs.readFileSync('example.mdx')
111+
const doc = await fs.readFile('example.mdx')
72112

73113
const tree = fromMarkdown(doc, {
74114
extensions: [mdxjs()],
@@ -82,7 +122,7 @@ const out = toMarkdown(tree, {extensions: [mdxToMarkdown()]})
82122
console.log(out)
83123
```
84124

85-
Now, running `node example` yields (positional info removed for brevity):
125+
…now running `node example.js` yields (positional info removed for brevity):
86126

87127
```js
88128
{
@@ -222,42 +262,88 @@ Which you can also put inline: {1+1}.
222262

223263
## API
224264

225-
This package exports the following identifier: `mdxFromMarkdown`,
226-
`mdxToMarkdown`.
265+
This package exports the identifiers `mdxFromMarkdown` and `mdxToMarkdown`.
227266
There is no default export.
228267

229268
### `mdxFromMarkdown()`
230269

270+
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].
271+
272+
When using the [syntax extension with `addResult`][mdxjs], nodes will have
273+
a `data.estree` field set to an [ESTree][].
274+
231275
### `mdxToMarkdown(options?)`
232276

233-
Support MDX (or MDX.js).
234-
The exports are functions that can be called to respectively get an extension
235-
for [`mdast-util-from-markdown`][from-markdown] and
236-
[`mdast-util-to-markdown`][to-markdown].
277+
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].
278+
279+
##### `options`
280+
281+
Configuration (optional).
282+
Currently passes through `quote`, `quoteSmart`, `tightSelfClosing`, and
283+
`printWidth` to [`mdast-util-mdx-jsx`][options].
284+
285+
## Syntax tree
286+
287+
This utility combines several mdast utilities.
288+
See their readmes for the node types supported in the tree:
289+
290+
* [`mdast-util-mdx-expression`](https://github.com/syntax-tree/mdast-util-mdx-expression#syntax-tree)
291+
— support MDX expressions
292+
* [`mdast-util-mdx-jsx`](https://github.com/syntax-tree/mdast-util-mdx-jsx#syntax-tree)
293+
— support MDX JSX
294+
* [`mdast-util-mdxjs-esm`](https://github.com/syntax-tree/mdast-util-mdxjs-esm#syntax-tree)
295+
— support MDX ESM
296+
297+
## Types
237298

238-
The options to `mdxToMarkdown` are [passed to `mdxJsxToMarkdown`][options].
299+
This package is fully typed with [TypeScript][].
300+
It exports the additional types `MdxFlowExpression`, `MdxTextExpression`,
301+
`MdxjsEsm`, `MdxJsxAttributeValueExpression`, `MdxJsxAttribute`,
302+
`MdxJsxExpressionAttribute`, `MdxJsxFlowElement`,
303+
`MdxJsxTextElement`, and `ToMarkdownOptions`.
304+
305+
It also registers the node types with `@types/mdast`.
306+
If you’re working with the syntax tree, make sure to import this utility
307+
somewhere in your types, as that registers the new node types in the tree.
308+
309+
```js
310+
/**
311+
* @typedef {import('mdast-util-mdx')}
312+
*/
313+
314+
import {visit} from 'unist-util-visit'
315+
316+
/** @type {import('mdast').Root} */
317+
const tree = getMdastNodeSomeHow()
318+
319+
visit(tree, (node) => {
320+
// `node` can now be an expression, JSX, or ESM node.
321+
})
322+
```
323+
324+
## Compatibility
325+
326+
Projects maintained by the unified collective are compatible with all maintained
327+
versions of Node.js.
328+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
329+
Our projects sometimes work with older versions, but this is not guaranteed.
330+
331+
This utility works with `mdast-util-from-markdown` version 1+ and
332+
`mdast-util-to-markdown` version 1+.
239333

240334
## Related
241335

242-
* [`remarkjs/remark`][remark]
243-
— markdown processor powered by plugins
244336
* [`remarkjs/remark-mdx`][remark-mdx]
245-
— remark plugin to support MDX (or MDX.js)
246-
* [`micromark/micromark`][micromark]
247-
— the smallest commonmark-compliant markdown parser that exists
337+
— remark plugin to support MDX
248338
* [`micromark/micromark-extension-mdx`][mdx]
249339
— micromark extension to parse MDX
250340
* [`micromark/micromark-extension-mdxjs`][mdxjs]
251-
— micromark extension to parse MDX.js
252-
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
253-
— mdast parser using `micromark` to create mdast from markdown
254-
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
255-
— mdast serializer to create markdown from mdast
341+
— micromark extension to parse JavaScript-aware MDX
256342

257343
## Contribute
258344

259-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
260-
started.
345+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
346+
ways to get started.
261347
See [`support.md`][support] for ways to get help.
262348

263349
This project has a [code of conduct][coc].
@@ -298,25 +384,29 @@ abide by its terms.
298384

299385
[npm]: https://docs.npmjs.com/cli/install
300386

387+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
388+
389+
[esmsh]: https://esm.sh
390+
391+
[typescript]: https://www.typescriptlang.org
392+
301393
[license]: license
302394

303395
[author]: https://wooorm.com
304396

305-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
397+
[health]: https://github.com/syntax-tree/.github
306398

307-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
399+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
308400

309-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
401+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
310402

311-
[mdast]: https://github.com/syntax-tree/mdast
312-
313-
[remark]: https://github.com/remarkjs/remark
403+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
314404

315-
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
405+
[mdast]: https://github.com/syntax-tree/mdast
316406

317-
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
407+
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
318408

319-
[micromark]: https://github.com/micromark/micromark
409+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
320410

321411
[mdx]: https://github.com/micromark/micromark-extension-mdx
322412

@@ -325,3 +415,5 @@ abide by its terms.
325415
[remark-mdx]: https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx
326416

327417
[options]: https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxtomarkdownoptions
418+
419+
[estree]: https://github.com/estree/estree

0 commit comments

Comments
 (0)