Skip to content

Commit 8eb8dba

Browse files
committed
Add improved docs
1 parent 44fa2fe commit 8eb8dba

File tree

1 file changed

+93
-30
lines changed

1 file changed

+93
-30
lines changed

readme.md

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

11-
[mdast][] extensions to parse and serialize [MDX][]: ESM import/exports,
12-
JavaScript expressions, and JSX.
11+
[mdast][] extensions to parse and serialize [MDX][]: ESM, JSX, and expressions.
1312

1413
## Contents
1514

@@ -20,6 +19,9 @@ JavaScript expressions, and JSX.
2019
* [API](#api)
2120
* [`mdxFromMarkdown()`](#mdxfrommarkdown)
2221
* [`mdxToMarkdown(options?)`](#mdxtomarkdownoptions)
22+
* [`ToMarkdownOptions`](#tomarkdownoptions)
23+
* [HTML](#html)
24+
* [Syntax](#syntax)
2325
* [Syntax tree](#syntax-tree)
2426
* [Types](#types)
2527
* [Compatibility](#compatibility)
@@ -29,18 +31,23 @@ JavaScript expressions, and JSX.
2931

3032
## What is this?
3133

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 />`).
34+
This package contains two extensions that add support for MDX syntax in
35+
markdown to [mdast][]: ESM (`import x from 'y'`), JSX (`<a />`), and
36+
expressions (`{Math.PI}`).
37+
These extensions plug into
38+
[`mdast-util-from-markdown`][mdast-util-from-markdown] (to support parsing
39+
MDX in markdown into a syntax tree) and
40+
[`mdast-util-to-markdown`][mdast-util-to-markdown] (to support serializing
41+
MDX in syntax trees to markdown).
3742

3843
## When to use this
3944

40-
These tools are all rather low-level.
41-
In many cases, you’d want to use [`remark-mdx`][remark-mdx] with remark instead.
45+
You can use these extensions when you are working with
46+
`mdast-util-from-markdown` and `mdast-util-to-markdown` already.
47+
48+
When working with `mdast-util-from-markdown`, you must combine this package
49+
with [`micromark-extension-mdx`][mdx] or [`micromark-extension-mdxjs`][mdxjs].
4250

43-
Use this if you’re dealing with the AST manually and want to support all of MDX.
4451
Instead of this package, you can also use the extensions separately:
4552

4653
* [`mdast-util-mdx-expression`](https://github.com/syntax-tree/mdast-util-mdx-expression)
@@ -50,14 +57,14 @@ Instead of this package, you can also use the extensions separately:
5057
* [`mdast-util-mdxjs-esm`](https://github.com/syntax-tree/mdast-util-mdxjs-esm)
5158
— support MDX ESM
5259

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].
60+
All these packages are used in [`remark-mdx`][remark-mdx], which
61+
focusses on making it easier to transform content by abstracting these
62+
internals away.
5663

5764
## Install
5865

5966
This package is [ESM only][esm].
60-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
67+
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
6168

6269
```sh
6370
npm install mdast-util-mdx
@@ -262,25 +269,71 @@ Which you can also put inline: {1+1}.
262269

263270
## API
264271

265-
This package exports the identifiers `mdxFromMarkdown` and `mdxToMarkdown`.
272+
This package exports the identifiers [`mdxFromMarkdown`][api-mdx-from-markdown]
273+
and [`mdxToMarkdown`][api-mdx-to-markdown].
266274
There is no default export.
267275

268276
### `mdxFromMarkdown()`
269277

270-
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown].
278+
Create an extension for [`mdast-util-from-markdown`][mdast-util-from-markdown]
279+
to enable MDX (ESM, JSX, expressions).
280+
281+
###### Returns
282+
283+
Extension for `mdast-util-from-markdown` to enable MDX
284+
([`FromMarkdownExtension`][from-markdown-extension]).
271285

272-
When using the [syntax extension with `addResult`][mdxjs], nodes will have
273-
a `data.estree` field set to an [ESTree][].
286+
When using the [syntax extensions with `addResult`][mdxjs], ESM and expression
287+
nodes will have `data.estree` fields set to ESTree [`Program`][program] node.
274288

275289
### `mdxToMarkdown(options?)`
276290

291+
Create an extension for [`mdast-util-to-markdown`][mdast-util-to-markdown]
292+
to enable MDX (ESM, JSX, expressions).
293+
277294
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown].
278295

279-
##### `options`
296+
###### Parameters
297+
298+
* `options` ([`ToMarkdownOptions`][api-to-markdown-options])
299+
— configuration
300+
301+
###### Returns
302+
303+
Extension for `mdast-util-to-markdown` to enable MDX
304+
([`FromMarkdownExtension`][to-markdown-extension]).
305+
306+
### `ToMarkdownOptions`
307+
308+
Configuration (TypeScript type).
309+
310+
###### Fields
280311

281-
Configuration (optional).
282-
Currently passes through `quote`, `quoteSmart`, `tightSelfClosing`, and
283-
`printWidth` to [`mdast-util-mdx-jsx`][options].
312+
* `quote` (`'"'` or `"'"`, default: `'"'`)
313+
— preferred quote to use around attribute values
314+
* `quoteSmart` (`boolean`, default: `false`)
315+
— use the other quote if that results in less bytes
316+
* `tightSelfClosing` (`boolean`, default: `false`)
317+
— do not use an extra space when closing self-closing elements: `<img/>`
318+
instead of `<img />`
319+
* `printWidth` (`number`, default: `Infinity`)
320+
— try and wrap syntax at this width.
321+
When set to a finite number (say, `80`), the formatter will print
322+
attributes on separate lines when a tag doesn’t fit on one line.
323+
The normal behavior is to print attributes with spaces between them instead
324+
of line endings
325+
326+
## HTML
327+
328+
MDX has no representation in HTML.
329+
Though, when you are dealing with MDX, you will likely go *through* hast.
330+
You can enable passing MDX through to hast by configuring
331+
[`mdast-util-to-hast`][mdast-util-to-hast] with `passThrough: ['mdxjsEsm',
332+
'mdxFlowExpression', 'mdxJsxFlowElement', 'mdxJsxTextElement', 'mdxTextExpression']`.
333+
334+
## Syntax
335+
336+
See [Syntax in `micromark-extension-mdxjs`][mdxjs].
284337

285338
## Syntax tree
286339

@@ -300,9 +353,9 @@ This package is fully typed with [TypeScript][].
300353
It exports the additional types `MdxFlowExpression`, `MdxTextExpression`,
301354
`MdxjsEsm`, `MdxJsxAttributeValueExpression`, `MdxJsxAttribute`,
302355
`MdxJsxExpressionAttribute`, `MdxJsxFlowElement`,
303-
`MdxJsxTextElement`, and `ToMarkdownOptions`.
356+
`MdxJsxTextElement`, and [`ToMarkdownOptions`][api-to-markdown-options].
304357

305-
It also registers the node types with `@types/mdast`.
358+
It also registers the node types with `@types/mdast` and `@types/hast`.
306359
If you’re working with the syntax tree, make sure to import this utility
307360
somewhere in your types, as that registers the new node types in the tree.
308361

@@ -325,19 +378,19 @@ visit(tree, (node) => {
325378

326379
Projects maintained by the unified collective are compatible with all maintained
327380
versions of Node.js.
328-
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
381+
As of now, that is Node.js 14.14+ and 16.0+.
329382
Our projects sometimes work with older versions, but this is not guaranteed.
330383

331384
This utility works with `mdast-util-from-markdown` version 1+ and
332385
`mdast-util-to-markdown` version 1+.
333386

334387
## Related
335388

336-
* [`remarkjs/remark-mdx`][remark-mdx]
389+
* [`remark-mdx`][remark-mdx]
337390
— remark plugin to support MDX
338-
* [`micromark/micromark-extension-mdx`][mdx]
391+
* [`micromark-extension-mdx`][mdx]
339392
— micromark extension to parse MDX
340-
* [`micromark/micromark-extension-mdxjs`][mdxjs]
393+
* [`micromark-extension-mdxjs`][mdxjs]
341394
— micromark extension to parse JavaScript-aware MDX
342395

343396
## Contribute
@@ -406,14 +459,24 @@ abide by its terms.
406459

407460
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
408461

462+
[from-markdown-extension]: https://github.com/syntax-tree/mdast-util-from-markdown#extension
463+
409464
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
410465

466+
[to-markdown-extension]: https://github.com/syntax-tree/mdast-util-to-markdown#options
467+
468+
[mdast-util-to-hast]: https://github.com/syntax-tree/mdast-util-to-hast
469+
411470
[mdx]: https://github.com/micromark/micromark-extension-mdx
412471

413472
[mdxjs]: https://github.com/micromark/micromark-extension-mdxjs
414473

415474
[remark-mdx]: https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx
416475

417-
[options]: https://github.com/syntax-tree/mdast-util-mdx-jsx#mdxjsxtomarkdownoptions
476+
[program]: https://github.com/estree/estree/blob/master/es2015.md#programs
477+
478+
[api-mdx-from-markdown]: #mdxfrommarkdown
479+
480+
[api-mdx-to-markdown]: #mdxtomarkdownoptions
418481

419-
[estree]: https://github.com/estree/estree
482+
[api-to-markdown-options]: #tomarkdownoptions

0 commit comments

Comments
 (0)