mdast-util-mdxjs-esm
Extension for mdast-util-from-markdown
and/or
mdast-util-to-markdown
to support MDX.js ESM import/exports.
When parsing (from-markdown
), must be combined with
micromark-extension-mdxjs-esm
.
This utility handles parsing and serializing.
See micromark-extension-mdxjs-esm
for how the syntax works.
You probably should use micromark-extension-mdxjs
with
mdast-util-mdx
(which includes this package) to support all
of MDX.js.
Or use it all through remark-mdx
(remark).
Install
npm:
npm install mdast-util-mdxjs-esm
Use
Say we have an MDX.js file, example.mdx
:
import a from 'b'
export var c = ''
d
And our script, example.js
, looks as follows:
var fs = require('fs')
var acorn = require('acorn')
var syntax = require('micromark-extension-mdxjs-esm')
var fromMarkdown = require('mdast-util-from-markdown')
var toMarkdown = require('mdast-util-to-markdown')
var mdxjsEsm = require('mdast-util-mdxjs-esm')
var doc = fs.readFileSync('example.mdx')
var tree = fromMarkdown(doc, {
extensions: [syntax({acorn: acorn, addResult: true})],
mdastExtensions: [mdxjsEsm.fromMarkdown]
})
console.log(tree)
var out = toMarkdown(tree, {extensions: [mdxjsEsm.toMarkdown]})
console.log(out)
Now, running node example
yields (positional info removed for brevity):
{
type: 'root',
children: [
{
type: 'mdxjsEsm',
value: "import a from 'b'\nexport var c = ''",
data: {
estree: {
type: 'Program',
body: [
{
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportDefaultSpecifier',
local: {type: 'Identifier', name: 'a'}
}
],
source: {type: 'Literal', value: 'b', raw: "'b'"}
},
{
type: 'ExportNamedDeclaration',
declaration: {
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {type: 'Identifier', name: 'c'},
init: {type: 'Literal', value: '', raw: "''"}
}
],
kind: 'var'
},
specifiers: [],
source: null
}
],
sourceType: 'module'
}
}
},
{type: 'paragraph', children: [{type: 'text', value: 'd'}]}
]
}
import a from 'b'
export var c = ''
d
API
mdxjsEsm.fromMarkdown
mdxjsEsm.toMarkdown
Note: the separate extensions are also available at
mdast-util-mdxjs-esm/from-markdown
and mdast-util-mdxjs-esm/to-markdown
.
Support MDX.js ESM import/exports.
The exports are extensions, respectively for
mdast-util-from-markdown
and
mdast-util-to-markdown
.
When using the syntax extension with addResult
, nodes will have
a data.estree
field set to an ESTree
Syntax tree
The following interfaces are added to mdast by this utility.
Nodes
MDXJSEsm
interface MDXJSEsm <: Literal {
type: "mdxjsEsm"
}
MDXJSEsm (Literal) represents ESM import/exports embedded
in MDX.
It can be used where flow content is expected.
Its content is represented by its value
field.
For example, the following Markdown:
import a from 'b'
Yields:
{
type: 'mdxjsEsm',
value: 'import a from \'b\''
}
Content model
FlowContent
(MDX.js ESM)
type FlowContentMdxjsEsm = MDXJSEsm | FlowContent
Note that when ESM is present, it can only exist as top-level content: if it has
a parent, that parent must be Root.
Related
Contribute
See contributing.md
in syntax-tree/.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.
License
MIT © Titus Wormer