unified

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 135
  2. util 130
  3. utility 126
  4. unist 120
  5. mdast 88
  6. mdast-util 30
  7. mdx 27
  8. markup 17
  9. mdxjs 8
  10. extension 7
  11. export 4
  12. esm 3
  13. import 2

mdast-util-mdxjs-esm

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

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.

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