micromark-extension-mdxjs-esm
micromark extension to support MDX ESM (import x from 'y'
).
Contents
- What is this?
- When to use this
- Install
- Use
- API
- Authoring
- Syntax
- Errors
- Tokens
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package contains extensions that add support for ESM enabled by MDX to micromark
. It matches how imports and exports work in JavaScript through acorn.
When to use this
These tools are all low-level. In many cases, you want to use remark-mdx
with remark instead. When you are using mdx-js/mdx
, that is already included.
Even when you want to use micromark
, you likely want to use micromark-extension-mdxjs
to support all MDX features. That extension includes this extension.
When working with mdast-util-from-markdown
, you must combine this package with mdast-util-mdxjs-esm
.
Install
This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with npm:
npm install micromark-extension-mdxjs-esm
In Deno with esm.sh
:
import {mdxjsEsm} from 'https://esm.sh/micromark-extension-mdxjs-esm@1'
In browsers with esm.sh
:
<script type="module">
import {mdxjsEsm} from 'https://esm.sh/micromark-extension-mdxjs-esm@1?bundle'
</script>
Use
import * as acorn from 'acorn'
import {micromark} from 'micromark'
import {mdxjsEsm} from 'micromark-extension-mdxjs-esm'
const output = micromark('import a from "b"\n\n# c', {
extensions: [mdxjsEsm({acorn})]
})
console.log(output)
Yields:
<h1>c</h1>
…which is useless: go to a syntax tree with mdast-util-from-markdown
and mdast-util-mdxjs-esm
instead.
API
This package exports the identifier mdxjsEsm
. There is no default export.
The export map supports the endorsed development
condition. Run node --conditions development module.js
to get instrumented dev code. Without this condition, production code is loaded.
mdxjsEsm(options)
Add support for MDX ESM import/exports.
Function called with options to get a syntax extension for micromark. That extension can be passed in extensions
.
options
Configuration (required).
options.acorn
Acorn parser to use (Acorn
, required).
options.acornOptions
Options to pass to acorn (Object
, default: {ecmaVersion: 2020, locations: true, sourceType: 'module'}
). All fields except for locations
can be set.
options.addResult
Whether to add an estree
field to mdxjsEsm
tokens with results from acorn (boolean
, default: false
).
Authoring
When authoring markdown with ESM, make sure to follow import and export statements with blank lines before more markdown.
Syntax
All valid imports and exports are supported, depending on what the given acorn instance and configuration supports.
When the lowercase strings export
or import
are found, followed by unicode whitespace (\s
), we expect JavaScript. Otherwise, like normal in markdown, we exit and it’ll end up as a paragraph. We continue parsing until we find a line ending followed by a blank line. At that point, we parse with acorn: it if parses, we found our block. Otherwise, if parsing failed at the last character, we assume it’s a blank line in code: we continue on until the next blank line and try again. Otherwise, the acorn error is thrown.
import a from "b"
import * as a from "b"
import {a} from "b"
import {a as b} from "c"
import a, {b as c} from "d"
import a, * as b from "c"
import "a"
export var a = ""
export const a = ""
export let a = ""
export var a, b
export var a = "a", b = "b"
export function a() {}
export class a {}
export var {a} = {}
export var {a: b} = {}
export var [a] = []
export default a = 1
export default function a() {}
export default class a {}
export * from "a"
export * as a from "b"
export {a} from "b"
export {a as b} from "c"
export {default} from "b"
export {default as a, b} from "c"
{/* Blank lines are supported in expressions: */}
export function a() {
return "b"
}
{/* A blank line must be used after import/exports: this is incorrect! */}
import a from "b"
## Hello, world!
Errors
Could not parse import/exports with acorn: $error
This error occurs if acorn crashes (source: micromark-extension-mdxjs-esm
, rule id: acorn
). For example:
import 1/1
Unexpected $type
in code: only import/exports are supported
This error occurs when a non-ESM construct is found (source: micromark-extension-mdxjs-esm
, rule id: non-esm
). For example:
export var a = 1
var b
Tokens
An mdxjsEsm
token is used to reflect the block of import/exports in markdown.
It includes:
lineEnding
for the\r
,\n
, and\r\n
lineEndingBlank
for the same characters but when after potential whitespace and another line endingwhitespace
for markdown spaces and tabs in blank linesmdxjsEsmData
for any character in a line ofmdxjsEsm
Types
This package is fully typed with TypeScript. It exports the additional type Options
.
Compatibility
This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. It also works in Deno and modern browsers.
Security
This package deals with compiling JavaScript. If you do not trust the JavaScript, this package does nothing to change that.
Related
micromark/micromark-extension-mdxjs
— micromark extension to support MDXsyntax-tree/mdast-util-mdxjs-esm
— mdast utility to support MDX ESMremark-mdx
— remark plugin to support MDX syntax
Contribute
See contributing.md
in micromark/.github
for ways to get started. See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.