npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@crafter/mermaid-parser

v0.0.3

Published

Zero-dependency Mermaid diagram parser that outputs an AST with source spans.

Downloads

179

Readme

@crafter/mermaid-parser

Zero-dependency Mermaid diagram parser that outputs an AST with source spans.

Features

  • Zero dependencies - Pure TypeScript implementation
  • Source spans - Every AST node includes accurate line/column/offset information
  • Error recovery - Parser continues on errors and collects diagnostics
  • Type-safe - Full TypeScript support with strict mode
  • Synchronous - All parsing is synchronous

Supported Diagrams

  • ✅ Flowchart (graph, flowchart)
  • ✅ State Diagram (stateDiagram-v2)
  • ✅ Sequence Diagram (sequenceDiagram)
  • ✅ Class Diagram (classDiagram)
  • ✅ ER Diagram (erDiagram)
  • ✅ Pie Chart (pie)
  • ✅ Gantt Chart (gantt)
  • ✅ Mindmap (mindmap)

Installation

bun add @crafter/mermaid-parser

Usage

import { parse } from "@crafter/mermaid-parser";

const source = `
graph TD
	A[Start] --> B{Decision}
	B -->|Yes| C[Success]
	B -->|No| D[Failure]
`;

const result = parse(source);

if (result.ast) {
	console.log("Nodes:", result.ast.nodes);
	console.log("Edges:", result.ast.edges);
}

for (const diagnostic of result.diagnostics) {
	console.log(`${diagnostic.severity}: ${diagnostic.message}`);
}

API

parse(source: string): ParseResult<DiagramAST>

Parses Mermaid source code and returns an AST with diagnostics.

interface ParseResult<T> {
	ast: T | null;
	diagnostics: ParseDiagnostic[];
}

detectDiagramType(source: string): DiagramType | null

Detects the diagram type from the source code.

type DiagramType = "flowchart" | "sequence" | "class" | "er" | "state" | "pie" | "gantt" | "mindmap";

AST Types

Flowchart

interface FlowchartAST {
	type: "flowchart";
	direction: Direction;
	nodes: Map<string, FlowchartNode>;
	edges: FlowchartEdge[];
	subgraphs: FlowchartSubgraph[];
	classDefs: Map<string, Record<string, string>>;
	classAssignments: Map<string, string>;
	nodeStyles: Map<string, Record<string, string>>;
	span: SourceSpan;
}

interface FlowchartNode {
	id: string;
	label: string;
	shape: NodeShape;
	span: SourceSpan;
}

interface FlowchartEdge {
	source: string;
	target: string;
	label?: string;
	style: EdgeStyle;
	hasArrowStart: boolean;
	hasArrowEnd: boolean;
	span: SourceSpan;
}

Sequence Diagram

interface SequenceAST {
	type: "sequence";
	participants: SequenceParticipant[];
	messages: Array<SequenceMessage | SequenceBlock | SequenceNote>;
	span: SourceSpan;
}

Class Diagram

interface ClassAST {
	type: "class";
	classes: Map<string, ClassDefinition>;
	relations: ClassRelation[];
	namespaces: ClassNamespace[];
	span: SourceSpan;
}

ER Diagram

interface ERAST {
	type: "er";
	entities: Map<string, EREntity>;
	relations: ERRelation[];
	span: SourceSpan;
}

Source Spans

Every AST node includes a SourceSpan with accurate position information:

interface SourceSpan {
	start: { line: number; column: number; offset: number };
	end: { line: number; column: number; offset: number };
}

This enables:

  • Syntax highlighting
  • Error reporting
  • Code navigation
  • Refactoring tools

Error Recovery

The parser continues parsing even when it encounters errors:

const source = `graph TD
	A --> B
	invalid line here
	B --> C
`;

const result = parse(source);
// result.ast contains valid nodes A, B, C
// result.diagnostics contains warning about invalid line

Examples

Flowchart with Subgraphs

const source = `
graph TD
	A[Start] --> B[Process]

	subgraph Processing
		B --> C[Step 1]
		C --> D[Step 2]
	end

	D --> E[End]

	classDef highlight fill:#f9f
	class C highlight
`;

const result = parse(source);
console.log(result.ast?.subgraphs);

Sequence Diagram with Blocks

const source = `
sequenceDiagram
	participant Alice
	actor Bob

	loop Every minute
		Alice->>Bob: Ping
		Bob-->>Alice: Pong
	end

	Note over Alice,Bob: Communication complete
`;

const result = parse(source);
console.log(result.ast?.messages);

Class Diagram with Relations

const source = `
classDiagram
	class Animal {
		+String name
		+makeSound()
	}

	class Dog {
		+bark()
	}

	Animal <|-- Dog
	Owner "1" --> "*" Dog : owns
`;

const result = parse(source);
console.log(result.ast?.relations);

ER Diagram

const source = `
erDiagram
	CUSTOMER ||--o{ ORDER : places
	ORDER ||--|{ LINE-ITEM : contains

	CUSTOMER {
		string name PK
		string email UK
	}

	ORDER {
		int id PK
		date created_at
	}
`;

const result = parse(source);
console.log(result.ast?.entities);
console.log(result.ast?.relations);

License

MIT