English
Parser
@mmd-toolkit/parser parses Mermaid diagram text into an abstract syntax tree (AST) for inspecting and processing diagrams in JavaScript or TypeScript.
You can try the parser online in the Playground without installing anything.
Installation
Install the parser in your project:
sh
npm install @mmd-toolkit/parserIt is an ES module and needs Node.js ^20.19.0 || ^22.13.0 || >=24.
To use the TypeScript types, also install @mmd-toolkit/ast, at the version of the parser or a later compatible one:
sh
npm install --save-dev @mmd-toolkit/astParse a diagram
Pass one diagram's source text, including its diagram header, to parse():
ts
import { parse } from "@mmd-toolkit/parser"
const root = parse("flowchart LR\n A[Start] --> B\n")
console.log(root.diagramType) // "flowchart-v2"parse() returns the root node of the AST (Root) synchronously. In this example, diagramType identifies the parsed diagram as a flowchart.
Errors
When parsing fails, parse() throws the exported error class ParseError: the input is rejected, including when no diagram type is recognized.
Next steps
- API: input, return value, exported types, and error handling.
- Traversal: visit nodes to inspect and analyze a diagram.
- Compatibility: syntax guarantees and known differences from mermaid.js.
- AST definition (GitHub): detailed node types and fields.