Project: syntax-tree/mdast-util-mdxjs-esm

Package: mdast-util-mdxjs-esm@0.1.1

  1. Dependencies: 0·Dependents: 1
  2. mdast extension to parse and serialize MDX.js ESM (import/exports)
  1. markdown 142
  2. util 128
  3. utility 124
  4. unist 120
  5. mdast 89
  6. mdast-util 29
  7. mdx 29
  8. markup 17
  9. mdxjs 8
  10. extension 7
  11. export 4
  12. esm 3
  13. import 2


Build Coverage Downloads Size Sponsors Backers Chat

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).



npm install mdast-util-mdxjs-esm


Say we have an MDX.js file, example.mdx:

import a from 'b'
export var c = ''


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]


var out = toMarkdown(tree, {extensions: [mdxjsEsm.toMarkdown]})


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 = ''





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.



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'


  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.


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.


MIT © Titus Wormer