Markdown to mdast AST Parser

2.0.3 · active · verified Sun Apr 19

mdast-util-from-markdown is a foundational utility in the syntax-tree ecosystem, designed to parse Markdown input into an mdast (Markdown Abstract Syntax Tree) syntax tree. It leverages the micromark tokenizer internally and transforms its output into a tree structure. The current stable version is 2.0.3, with frequent patch and minor releases, while major versions like 2.0.0 introduce significant breaking changes such as the shift to ESM-only and a minimum Node.js 16 requirement. This package provides a lower-level API for direct AST manipulation, making it suitable for developers who need fine-grained control over the parsing process or wish to integrate custom syntax extensions like GFM, MDX, Math, or Frontmatter. It is distinct from micromark (which focuses on direct HTML output) and remark-parse (which offers a higher-level, more abstracted content processing experience within the remark ecosystem).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to asynchronously read a Markdown file and parse its content into an mdast syntax tree using the `fromMarkdown` utility.

import fs from 'node:fs/promises'
import {fromMarkdown} from 'mdast-util-from-markdown'

async function parseExample() {
  // Assume 'example.md' contains '## Hello, *World*!'
  const doc = await fs.readFile('example.md', 'utf8')
  const tree = fromMarkdown(doc)

  console.log(JSON.stringify(tree, null, 2))
  // Expected output:
  // {
  //   "type": "root",
  //   "children": [
  //     {
  //       "type": "heading",
  //       "depth": 2,
  //       "children": [
  //         { "type": "text", "value": "Hello, " },
  //         { "type": "emphasis", "children": [{ "type": "text", "value": "World" }] },
  //         { "type": "text", "value": "!" }
  //       ]
  //     }
  //   ]
  // }
}

parseExample().catch(console.error)

view raw JSON →