Skip to content

Parsing MDX

Lanexio Parser implements MDX 3.0 — Markdown extended with JSX elements, ESM statements, and expressions. The MDX grammar builds on the CommonMark and GFM engine, so all standard Markdown features work alongside MDX constructs.

  1. Install the package.

    Terminal window
    pnpm add @lanexio/parser-grammar-mdx
  2. Parse an MDX document.

    import { parseMdx } from '@lanexio/parser-grammar-mdx';
    const encoder = new TextEncoder();
    const tree = parseMdx(encoder.encode(`
    import { Chart } from './chart'
    # Sales Report
    <Chart data={[1, 2, 3]} />
    Revenue grew **{pct}%** year over year.
    `));

MDX extends Markdown with three constructs:

  • ESMimport and export statements at the document level
  • JSX — block and inline elements with attributes, fragments, and nested content
  • Expressions{ ... } braces in flow and text contexts

Standard Markdown features (headings, lists, code blocks, emphasis, links, images, tables, task lists, footnotes) all work inside MDX documents.

OptionTypeDefaultDescription
gfmbooleantrueEnable GFM extensions (tables, task lists, strikethrough)

The MDX parser delegates to the Markdown engine, so all Markdown parse options are available.

import { MdxKind } from '@lanexio/parser-grammar-mdx';
const cursor = tree.cursor();
cursor.gotoFirstChild();
// Walk top-level nodes:
// MdxEsm (import/export blocks)
// Heading, Paragraph (Markdown content)
// MdxJsxFlowElement (component blocks)
// MdxFlowExpression ({...} in flow position)