Skip to content

Commit f73220a

Browse files
committed
Update docs
1 parent 1fc88ac commit f73220a

File tree

1 file changed

+164
-74
lines changed

1 file changed

+164
-74
lines changed

readme.md

Lines changed: 164 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,76 @@
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) JSX.
13-
When parsing (`from-markdown`), must be combined with
14-
[`micromark-extension-mdx-jsx`][extension].
15-
16-
This utility handles parsing and serializing.
17-
See [`micromark-extension-mdx-jsx`][extension] for how the syntax works.
11+
Extensions to parse and serialize JSX between mdast and markdown
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+
* [`mdxJsxFromMarkdown()`](#mdxjsxfrommarkdown)
21+
* [`mdxJsxToMarkdown()`](#mdxjsxtomarkdown)
22+
* [Syntax tree](#syntax-tree)
23+
* [Nodes](#nodes)
24+
* [Mixin](#mixin)
25+
* [Content model](#content-model)
26+
* [Types](#types)
27+
* [Compatibility](#compatibility)
28+
* [Related](#related)
29+
* [Contribute](#contribute)
30+
* [License](#license)
31+
32+
## What is this?
33+
34+
This package contains extensions that add support for the JSX syntax enabled by
35+
MDX to [`mdast-util-from-markdown`][mdast-util-from-markdown] and
36+
[`mdast-util-to-markdown`][mdast-util-to-markdown].
37+
38+
[JSX][] is an XML-like syntax extension to ECMAScript (JavaScript), which MDX
39+
brings to markdown.
40+
For more info on MDX, see [What is MDX?][what-is-mdx]
1841

1942
## When to use this
2043

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

24-
## Install
48+
When you are working with syntax trees, and want all of MDX, use
49+
[`mdast-util-mdx`][mdast-util-mdx] instead.
50+
51+
When working with `micromark` (through `mdast-util-from-markdown`), you’d want
52+
to combine this package with
53+
[`micromark-extension-mdx-jsx`][micromark-extension-mdx-jsx].
2554

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.
55+
## Install
2856

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

3160
```sh
3261
npm install mdast-util-mdx-jsx
3362
```
3463

64+
In Deno with [Skypack][]:
65+
66+
```js
67+
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'https://cdn.skypack.dev/mdast-util-mdx-jsx@1?dts'
68+
```
69+
70+
In browsers with [Skypack][]:
71+
72+
```html
73+
<script type="module">
74+
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'https://cdn.skypack.dev/mdast-util-mdx-jsx@1?min'
75+
</script>
76+
```
77+
3578
## Use
3679

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

3982
```mdx
4083
<Box>
@@ -46,7 +89,7 @@ Say we have an MDX.js file, `example.mdx`:
4689
<abbr title="Hypertext Markup Language">HTML</abbr> is a lovely language.
4790
```
4891

49-
And our module, `example.js`, looks as follows:
92+
…and our module `example.js` looks as follows:
5093

5194
```js
5295
import fs from 'node:fs'
@@ -70,7 +113,7 @@ const out = toMarkdown(tree, {extensions: [mdxJsxToMarkdown()]})
70113
console.log(out)
71114
```
72115

73-
Now, running `node example` yields (positional info removed for brevity):
116+
…now running `node example.js` yields (positional info removed for brevity):
74117

75118
```js
76119
{
@@ -164,43 +207,52 @@ Now, running `node example` yields (positional info removed for brevity):
164207

165208
## API
166209

210+
This package exports the following identifiers: `mdxJsxFromMarkdown`,
211+
`mdxJsxToMarkdown`.
212+
There is no default export.
213+
167214
### `mdxJsxFromMarkdown()`
168215

169-
### `mdxJsxToMarkdown()`
216+
Function that can be called to get an extension for
217+
[`mdast-util-from-markdown`][mdast-util-from-markdown].
218+
219+
When using [`micromark-extension-mdx-jsx`][micromark-extension-mdx-jsx]
220+
with `options.addResult`, nodes will have a `data.estree` field set to an
221+
[ESTree][].
170222

171-
Support MDX (or MDX.js) JSX.
172-
The exports are extensions, respectively for
173-
[`mdast-util-from-markdown`][from-markdown] and
174-
[`mdast-util-to-markdown`][to-markdown].
223+
### `mdxJsxToMarkdown()`
175224

176-
When using the [syntax extension][extension] with `addResult`, nodes will have a
177-
`data.estree` field set to an [ESTree][].
225+
Function that can be called to get an extension for
226+
[`mdast-util-to-markdown`][mdast-util-to-markdown].
178227

179-
There are no options, but passing [`options.quote`][quote] to
180-
`mdast-util-to-markdown` is honored for attributes.
228+
This extension configures `mdast-util-to-markdown` with
229+
[`options.fences: true`][mdast-util-to-markdown-fences] and
230+
[`options.resourceLink: true`][mdast-util-to-markdown-resourcelink] too, do not
231+
overwrite them!
181232

182-
this extension configures [`mdast-util-to-markdown`][to-markdown] with
183-
`fences: true` and `resourceLink: true` too, do not overwrite them!
233+
Passing [`options.quote`][mdast-util-to-markdown-quote] to
234+
`mdast-util-to-markdown` is honored for
235+
attributes.
184236

185237
## Syntax tree
186238

187239
The following interfaces are added to **[mdast][]** by this utility.
188240

189241
### Nodes
190242

191-
#### `MDXJsxFlowElement`
243+
###### `MdxJsxFlowElement`
192244

193245
```idl
194-
interface MDXJsxFlowElement <: Parent {
246+
interface MdxJsxFlowElement <: Parent {
195247
type: "mdxJsxFlowElement"
196248
}
197249
198-
MDXJsxFlowElement includes MDXJsxElement
250+
MdxJsxFlowElement includes MdxJsxElement
199251
```
200252

201-
**MDXJsxFlowElement** (**[Parent][dfn-parent]**) represents JSX in flow (block).
253+
**MdxJsxFlowElement** (**[Parent][dfn-parent]**) represents JSX in flow (block).
202254
It can be used where **[flow][dfn-content-flow]** content is expected.
203-
It includes the mixin **[MDXJsxElement][dfn-mixin-mdx-jsx-element]**.
255+
It includes the mixin **[MdxJsxElement][dfn-mixin-mdx-jsx-element]**.
204256

205257
For example, the following markdown:
206258

@@ -221,21 +273,21 @@ Yields:
221273
}
222274
```
223275

224-
#### `MDXJsxTextElement`
276+
###### `MdxJsxTextElement`
225277

226278
```idl
227-
interface MDXJsxTextElement <: Parent {
279+
interface MdxJsxTextElement <: Parent {
228280
type: "mdxJsxTextElement"
229281
}
230282
231-
MDXJsxTextElement includes MDXJsxElement
283+
MdxJsxTextElement includes MdxJsxElement
232284
```
233285

234-
**MDXJsxTextElement** (**[Parent][dfn-parent]**) represents JSX in text (span,
286+
**MdxJsxTextElement** (**[Parent][dfn-parent]**) represents JSX in text (span,
235287
inline).
236288
It can be used where **[phrasing][dfn-content-phrasing]** content is
237289
expected.
238-
It includes the mixin **[MDXJsxElement][dfn-mixin-mdx-jsx-element]**.
290+
It includes the mixin **[MdxJsxElement][dfn-mixin-mdx-jsx-element]**.
239291

240292
For example, the following markdown:
241293

@@ -256,78 +308,104 @@ Yields:
256308

257309
### Mixin
258310

259-
### `MDXJsxElement`
311+
###### `MdxJsxElement`
260312

261313
```idl
262-
interface mixin MDXJsxElement {
314+
interface mixin MdxJsxElement {
263315
name: string?
264-
attributes: [MDXJsxExpressionAttribute | MDXJsxAttribute]
316+
attributes: [MdxJsxExpressionAttribute | MdxJsxAttribute]
265317
}
266318
267-
interface MDXJsxExpressionAttribute <: Literal {
319+
interface MdxJsxExpressionAttribute <: Literal {
268320
type: "mdxJsxExpressionAttribute"
269321
}
270322
271-
interface MDXJsxAttribute <: Node {
323+
interface MdxJsxAttribute <: Node {
272324
type: "mdxJsxAttribute"
273325
name: string
274-
value: MDXJsxAttributeValueExpression | string?
326+
value: MdxJsxAttributeValueExpression | string?
275327
}
276328
277-
interface MDXJsxAttributeValueExpression <: Literal {
329+
interface MdxJsxAttributeValueExpression <: Literal {
278330
type: "mdxJsxAttributeValueExpression"
279331
}
280332
```
281333

282-
**MDXJsxElement** represents a JSX element.
334+
**MdxJsxElement** represents a JSX element.
283335

284336
The `name` field can be present and represents an identifier.
285337
Without `name`, the element represents a fragment, in which case no attributes
286338
must be present.
287339

288340
The `attributes` field represents information associated with the node.
289-
The value of the `attributes` field is a list of **MDXJsxExpressionAttribute**
290-
and **MDXJsxAttribute** nodes.
341+
The value of the `attributes` field is a list of **MdxJsxExpressionAttribute**
342+
and **MdxJsxAttribute** nodes.
291343

292-
**MDXJsxExpressionAttribute** represents an expression (typically in a
344+
**MdxJsxExpressionAttribute** represents an expression (typically in a
293345
programming language) that when evaluated results in multiple attributes.
294346

295-
**MDXJsxAttribute** represents a single attribute.
347+
**MdxJsxAttribute** represents a single attribute.
296348
The `name` field must be present.
297349
The `value` field can be present, in which case it is either a string (a static
298350
value) or an expression (typically in a programming language) that when
299351
evaluated results in an attribute value.
300352

301353
### Content model
302354

303-
#### `FlowContent` (MDX JSX)
355+
###### `FlowContent` (MDX JSX)
304356

305357
```idl
306-
type MDXJsxFlowContent = MDXJsxFlowElement | FlowContent
358+
type MdxJsxFlowContent = MdxJsxFlowElement | FlowContent
307359
```
308360

309-
#### `PhrasingContent` (MDX JSX)
361+
###### `PhrasingContent` (MDX JSX)
310362

311363
```idl
312-
type MDXJsxPhrasingContent = MDXJsxTextElement | PhrasingContent
364+
type MdxJsxPhrasingContent = MdxJsxTextElement | PhrasingContent
313365
```
314366

367+
## Types
368+
369+
This package is fully typed with [TypeScript][].
370+
It exports the `MdxJsxAttributeValueExpression`, `MdxJsxAttribute`,
371+
`MdxJsxExpressionAttribute`, `MdxJsxFlowElement`, and `MdxJsxTextElement` types.
372+
373+
It also registers the node types with `@types/mdast`
374+
375+
If you’re working with the syntax tree, make sure to import this plugin
376+
somewhere in your types, as that registers the new node types in the tree.
377+
378+
```js
379+
/** @typedef {import('remark-math')} */
380+
381+
import {visit} from 'unist-util-visit'
382+
383+
/** @type {import('mdast').Root} */
384+
const tree = getMdastNodeSomeHow()
385+
386+
visit(tree, (node) => {
387+
// `node` can now be one of the JSX nodes.
388+
})
389+
```
390+
391+
## Compatibility
392+
393+
Projects maintained by the unified collective are compatible with all maintained
394+
versions of Node.js.
395+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
396+
Our projects sometimes work with older versions, but this is not guaranteed.
397+
398+
This plugin works with `mdast-util-from-markdown` version 1+ and
399+
`mdast-util-to-markdown` version 1+.
400+
315401
## Related
316402

317-
* [`remarkjs/remark`][remark]
318-
— markdown processor powered by plugins
319-
* [`remarkjs/remark-mdx`][remark-mdx]
320-
— remark plugin to support MDX
321-
* [`syntax-tree/mdast-util-from-markdown`][from-markdown]
322-
— mdast parser using `micromark` to create mdast from markdown
323-
* [`syntax-tree/mdast-util-to-markdown`][to-markdown]
324-
— mdast serializer to create markdown from mdast
403+
* [`micromark/micromark-extension-mdx-jsx`][micromark-extension-mdx-jsx]
404+
— support MDX JSX in micromark
325405
* [`syntax-tree/mdast-util-mdx`][mdast-util-mdx]
326-
— mdast utility to support all of MDX
327-
* [`micromark/micromark`][micromark]
328-
— the smallest commonmark-compliant markdown parser that exists
329-
* [`micromark/micromark-extension-mdx-jsx`][extension]
330-
— micromark extension to parse JSX
406+
— support MDX in mdast
407+
* [`remarkjs/remark-mdx`][remark-mdx]
408+
— support MDX in remark
331409

332410
## Contribute
333411

@@ -371,6 +449,8 @@ abide by its terms.
371449

372450
[npm]: https://docs.npmjs.com/cli/install
373451

452+
[skypack]: https://www.skypack.dev
453+
374454
[license]: license
375455

376456
[author]: https://wooorm.com
@@ -381,22 +461,20 @@ abide by its terms.
381461

382462
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
383463

384-
[mdast]: https://github.com/syntax-tree/mdast
464+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
385465

386-
[remark]: https://github.com/remarkjs/remark
466+
[typescript]: https://www.typescriptlang.org
387467

388-
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
468+
[mdast]: https://github.com/syntax-tree/mdast
389469

390-
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
470+
[remark]: https://github.com/remarkjs/remark
391471

392-
[micromark]: https://github.com/micromark/micromark
472+
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
393473

394-
[extension]: https://github.com/micromark/micromark-extension-mdxjs-esm
474+
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
395475

396476
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx
397477

398-
[quote]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
399-
400478
[estree]: https://github.com/estree/estree
401479

402480
[dfn-parent]: https://github.com/syntax-tree/mdast#parent
@@ -408,3 +486,15 @@ abide by its terms.
408486
[dfn-mixin-mdx-jsx-element]: #mdxjsxelement
409487

410488
[remark-mdx]: https://github.com/mdx-js/mdx/tree/main/packages/remark-mdx
489+
490+
[jsx]: https://facebook.github.io/jsx/
491+
492+
[what-is-mdx]: https://mdxjs.com/docs/what-is-mdx/
493+
494+
[micromark-extension-mdx-jsx]: https://github.com/micromark/micromark-extension-mdx-jsx
495+
496+
[mdast-util-to-markdown-quote]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsquote
497+
498+
[mdast-util-to-markdown-fences]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsfences
499+
500+
[mdast-util-to-markdown-resourcelink]: https://github.com/syntax-tree/mdast-util-to-markdown#optionsresourcelink

0 commit comments

Comments
 (0)