mdast-util-mdxjs-esm
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -7,3 +7,3 @@ /** | ||
* @typedef {import('estree-jsx').Program} Program | ||
* @typedef {import('./complex-types').MdxjsEsm} MdxjsEsm | ||
* @typedef {import('./complex-types.js').MdxjsEsm} MdxjsEsm | ||
* | ||
@@ -21,3 +21,3 @@ * @typedef {MdxjsEsm} MDXJSEsm - Deprecated name, prefer `MdxjsEsm` | ||
export type Program = import('estree-jsx').Program | ||
export type MdxjsEsm = import('./complex-types').MdxjsEsm | ||
export type MdxjsEsm = import('./complex-types.js').MdxjsEsm | ||
/** | ||
@@ -24,0 +24,0 @@ * - Deprecated name, prefer `MdxjsEsm` |
@@ -7,3 +7,3 @@ /** | ||
* @typedef {import('estree-jsx').Program} Program | ||
* @typedef {import('./complex-types').MdxjsEsm} MdxjsEsm | ||
* @typedef {import('./complex-types.js').MdxjsEsm} MdxjsEsm | ||
* | ||
@@ -10,0 +10,0 @@ * @typedef {MdxjsEsm} MDXJSEsm - Deprecated name, prefer `MdxjsEsm` |
{ | ||
"name": "mdast-util-mdxjs-esm", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "mdast extension to parse and serialize MDX.js ESM (import/exports)", | ||
@@ -53,3 +53,3 @@ "license": "MIT", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
@@ -61,3 +61,3 @@ "rimraf": "^3.0.0", | ||
"unist-util-remove-position": "^4.0.0", | ||
"xo": "^0.47.0" | ||
"xo": "^0.50.0" | ||
}, | ||
@@ -64,0 +64,0 @@ "scripts": { |
167
readme.md
@@ -11,22 +11,44 @@ # mdast-util-mdxjs-esm | ||
Extension for [`mdast-util-from-markdown`][from-markdown] and/or | ||
[`mdast-util-to-markdown`][to-markdown] to support MDX.js ESM import/exports. | ||
When parsing (`from-markdown`), must be combined with | ||
[`micromark-extension-mdxjs-esm`][extension]. | ||
[mdast][] extensions to parse and serialize [MDX][] ESM import/exports. | ||
This utility handles parsing and serializing. | ||
See [`micromark-extension-mdxjs-esm`][extension] for how the syntax works. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When to use this](#when-to-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`mdxjsEsmFromMarkdown`](#mdxjsesmfrommarkdown) | ||
* [`mdxjsEsmToMarkdown`](#mdxjsesmtomarkdown) | ||
* [Syntax tree](#syntax-tree) | ||
* [Nodes](#nodes) | ||
* [Content model](#content-model) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package contains extensions that add support for the ESM syntax enabled | ||
by MDX to [`mdast-util-from-markdown`][mdast-util-from-markdown] and | ||
[`mdast-util-to-markdown`][mdast-util-to-markdown]. | ||
## When to use this | ||
Use [`mdast-util-mdx`][mdast-util-mdx] if you want all of MDX / MDX.js. | ||
Use this otherwise. | ||
These tools are all rather low-level. | ||
In most cases, you’d want to use [`remark-mdx`][remark-mdx] with remark instead. | ||
When you are working with syntax trees and want all of MDX, use | ||
[`mdast-util-mdx`][mdast-util-mdx] instead. | ||
When working with `mdast-util-from-markdown`, you must combine this package with | ||
[`micromark-extension-mdxjs-esm`][extension]. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -36,9 +58,23 @@ npm install mdast-util-mdxjs-esm | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'https://esm.sh/mdast-util-mdxjs-esm@1' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'https://esm.sh/mdast-util-mdxjs-esm@1?bundle' | ||
</script> | ||
``` | ||
## Use | ||
Say we have an MDX.js file, `example.mdx`: | ||
Say our document `example.mdx` contains: | ||
```mdx | ||
import a from 'b' | ||
export var c = '' | ||
export const c = '' | ||
@@ -48,6 +84,6 @@ d | ||
And our module, `example.js`, looks as follows: | ||
…and our module `example.js` looks as follows: | ||
```js | ||
import fs from 'fs' | ||
import fs from 'node:fs/promises' | ||
import * as acorn from 'acorn' | ||
@@ -59,3 +95,3 @@ import {fromMarkdown} from 'mdast-util-from-markdown' | ||
const doc = fs.readFileSync('example.mdx') | ||
const doc = await fs.readFile('example.mdx') | ||
@@ -74,3 +110,3 @@ const tree = fromMarkdown(doc, { | ||
Now, running `node example` yields (positional info removed for brevity): | ||
…now running `node example.js` yields (positional info removed for brevity): | ||
@@ -83,3 +119,3 @@ ```js | ||
type: 'mdxjsEsm', | ||
value: "import a from 'b'\nexport var c = ''", | ||
value: "import a from 'b'\nexport const c = ''", | ||
data: { | ||
@@ -110,3 +146,3 @@ estree: { | ||
], | ||
kind: 'var' | ||
kind: 'const' | ||
}, | ||
@@ -128,3 +164,3 @@ specifiers: [], | ||
import a from 'b' | ||
export var c = '' | ||
export const c = '' | ||
@@ -136,3 +172,3 @@ d | ||
This package exports the following identifier: `mdxjsEsmFromMarkdown`, | ||
This package exports the identifiers `mdxjsEsmFromMarkdown` and | ||
`mdxjsEsmToMarkdown`. | ||
@@ -143,12 +179,11 @@ There is no default export. | ||
Extension for [`mdast-util-from-markdown`][mdast-util-from-markdown]. | ||
When using the [syntax extension with `addResult`][extension], nodes will have | ||
a `data.estree` field set to an [ESTree][]. | ||
### `mdxjsEsmToMarkdown` | ||
Support MDX.js ESM import/exports. | ||
The exports are extensions, respectively for | ||
[`mdast-util-from-markdown`][from-markdown] and | ||
[`mdast-util-to-markdown`][to-markdown]. | ||
Extension for [`mdast-util-to-markdown`][mdast-util-to-markdown]. | ||
When using the [syntax extension with `addResult`][extension], nodes will have | ||
a `data.estree` field set to an [ESTree][] | ||
## Syntax tree | ||
@@ -199,16 +234,42 @@ | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional type `MdxjsEsm`. | ||
It also registers the node types with `@types/mdast`. | ||
If you’re working with the syntax tree, make sure to import this utility | ||
somewhere in your types, as that registers the new node types in the tree. | ||
```js | ||
/** | ||
* @typedef {import('mdast-util-mdxjs-esm')} | ||
*/ | ||
import {visit} from 'unist-util-visit' | ||
/** @type {import('mdast').Root} */ | ||
const tree = getMdastNodeSomeHow() | ||
visit(tree, (node) => { | ||
// `node` can now be an ESM node. | ||
}) | ||
``` | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
This plugin works with `mdast-util-from-markdown` version 1+ and | ||
`mdast-util-to-markdown` version 1+. | ||
## Related | ||
* [`remarkjs/remark`][remark] | ||
— markdown processor powered by plugins | ||
* [`remarkjs/remark-mdx`][remark-mdx] | ||
— remark plugin to support MDX | ||
* [`syntax-tree/mdast-util-from-markdown`][from-markdown] | ||
— mdast parser using `micromark` to create mdast from markdown | ||
* [`syntax-tree/mdast-util-to-markdown`][to-markdown] | ||
— mdast serializer to create markdown from mdast | ||
* [`syntax-tree/mdast-util-mdx`][mdast-util-mdx] | ||
— mdast utility to support MDX | ||
* [`micromark/micromark`][micromark] | ||
— the smallest commonmark-compliant markdown parser that exists | ||
* [`micromark/micromark-extension-mdxjs-esm`][extension] | ||
@@ -219,4 +280,4 @@ — micromark extension to parse MDX.js ESM | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -262,2 +323,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -267,22 +334,20 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[health]: https://github.com/syntax-tree/.github | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[mdast]: https://github.com/syntax-tree/mdast | ||
[remark]: https://github.com/remarkjs/remark | ||
[mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown | ||
[from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown | ||
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown | ||
[to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown | ||
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx | ||
[micromark]: https://github.com/micromark/micromark | ||
[extension]: https://github.com/micromark/micromark-extension-mdxjs-esm | ||
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx | ||
[estree]: https://github.com/estree/estree | ||
@@ -298,2 +363,4 @@ | ||
[remark-mdx]: https://github.com/mdx-js/mdx/tree/next/packages/remark-mdx | ||
[remark-mdx]: https://mdxjs.com/packages/remark-mdx/ | ||
[mdx]: https://mdxjs.com |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15514
352