Skip to content

Parser API ​

Import the functions, error classes, and types below from @mmd-toolkit/parser. See Getting started for installation.

parse(text) ​

The signature is parse(text: string): Root. Pass the source text of one Mermaid diagram, including its diagram header. The function returns a Root synchronously, or throws an error if it cannot parse the input.

Return value ​

Root holds the diagram body and the source records needed to work with it:

FieldTypeContents
type"Root"The root node's type.
diagramTypeDiagramTypeThe diagram id used by mermaid.js, such as flowchart-v2.
bodyDiagramBodyThe diagram's AST node.
frontmatterFrontmatter | nullThe frontmatter record, or null when absent.
commentsreadonly Comment[]All comments, in source order.
directivesreadonly Directive[]All directives, in source order.
tokensreadonly Token[]All tokens, in source order.
rangeRangeThe source range of the whole input.
locLocationThe location of the whole input.

comments and directives are empty arrays when none are present. Frontmatter and directive contents are preserved as text; their YAML or JSON contents are not parsed into objects.

The AST preserves every part of the input. Whitespace and line terminators are not nodes; with the original source text and the ranges, you can reproduce the input exactly.

Tokens and comments are available from Root.tokens and Root.comments. Their fields and the token kinds are defined in the token and comment definitions on GitHub. The root, nodes, tokens, comments, directives, and frontmatter carry source positions in range and loc. See the position definitions on GitHub for their format.

Types and diagram-specific fields ​

@mmd-toolkit/ast (see Installation) exports these types, as well as a type for each diagram node:

TypesPurpose
RootThe result of parse().
Node, NodeBaseAll AST node types, and the fields common to nodes.
DiagramBody, DiagramTypeDiagram body node types and diagram ids.
Token, TokenKind, Comment, Directive, FrontmatterSource records and token kinds.
Range, Location, PositionSource positions.

@mmd-toolkit/parser exports the types Visitor and VisitorKeys, the traversal callbacks and the child-field mapping.

Use a node's type field to narrow its TypeScript type before reading diagram-specific fields. This example reads the direction and statement count of a flowchart:

ts
import { parse } from "@mmd-toolkit/parser"
import type { DiagramBody, Root } from "@mmd-toolkit/ast"

const root: Root = parse("flowchart LR\n  A[Start] --> B\n")
const body: DiagramBody = root.body

if (body.type === "FlowchartDiagram") {
  console.log(body.direction) // "LR"
  console.log(body.body.length) // 1
}

The AST definition on GitHub describes the individual node types and the fields of directives and frontmatter.

Errors ​

ParseError ​

ParseError extends SyntaxError. It is thrown when the input is rejected, including when no diagram type is recognized. Its name is "ParseError", and its message describes the failure.

FieldTypeContents
indexnumberThe start offset of the reported range, equal to range[0].
rangeRangeThe source range reported for the error.
locLocationThe location corresponding to range.

The error position rules on GitHub explain how the parser chooses the reported range for each kind of failure.

Handling errors ​

Use the exported class to tell a parse error from other errors. This incomplete flowchart reaches the ParseError branch:

ts
import { parse, ParseError } from "@mmd-toolkit/parser"

try {
  parse("flowchart LR\n  A -->\n")
} catch (error) {
  if (error instanceof ParseError) {
    console.error(error.message, error.range)
  } else {
    throw error
  }
}

Traversal API ​

traverseNodes(node: Node, visitor: Visitor): void visits the starting node and its descendants. Visitor requires enterNode and leaveNode callbacks.

visitorKeys, of type VisitorKeys, maps each node type to its child-field names in traversal order.

Traversing the AST explains callback arguments, visitation order, and examples of inspecting nodes.

Third-party tools for Mermaid diagram text.