mermaid-ast 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +302 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/parser/flowchart-parser.d.ts +17 -0
- package/dist/parser/flowchart-parser.d.ts.map +1 -0
- package/dist/parser/flowchart-parser.js +398 -0
- package/dist/parser/index.d.ts +17 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +36 -0
- package/dist/parser/sequence-parser.d.ts +16 -0
- package/dist/parser/sequence-parser.d.ts.map +1 -0
- package/dist/parser/sequence-parser.js +502 -0
- package/dist/renderer/flowchart-renderer.d.ts +11 -0
- package/dist/renderer/flowchart-renderer.d.ts.map +1 -0
- package/dist/renderer/flowchart-renderer.js +324 -0
- package/dist/renderer/index.d.ts +13 -0
- package/dist/renderer/index.d.ts.map +1 -0
- package/dist/renderer/index.js +22 -0
- package/dist/renderer/sequence-renderer.d.ts +11 -0
- package/dist/renderer/sequence-renderer.d.ts.map +1 -0
- package/dist/renderer/sequence-renderer.js +270 -0
- package/dist/types/flowchart.d.ts +107 -0
- package/dist/types/flowchart.d.ts.map +1 -0
- package/dist/types/flowchart.js +22 -0
- package/dist/types/index.d.ts +26 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +19 -0
- package/dist/types/sequence.d.ts +188 -0
- package/dist/types/sequence.d.ts.map +1 -0
- package/dist/types/sequence.js +17 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Emily
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# mermaid-ast
|
|
2
|
+
|
|
3
|
+
[](https://randomlabs.ai)
|
|
4
|
+
|
|
5
|
+
Parse and render Mermaid diagrams to/from AST (Abstract Syntax Tree).
|
|
6
|
+
|
|
7
|
+
This library provides a way to programmatically work with Mermaid diagrams by parsing them into a structured AST and rendering them back to Mermaid syntax. It uses the official mermaid.js JISON parsers to ensure compatibility.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Parse** Mermaid diagrams into typed AST structures
|
|
12
|
+
- **Render** ASTs back to valid Mermaid syntax
|
|
13
|
+
- **Round-trip guarantee**: `render(parse(text))` produces semantically equivalent diagrams
|
|
14
|
+
- **Full TypeScript support** with comprehensive type definitions
|
|
15
|
+
- **Cross-runtime support**: Works in Bun, Node.js, and Deno
|
|
16
|
+
|
|
17
|
+
## Supported Diagram Types
|
|
18
|
+
|
|
19
|
+
- **Flowchart** (`flowchart`, `graph`)
|
|
20
|
+
- **Sequence Diagram** (`sequenceDiagram`)
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# Bun
|
|
26
|
+
bun add mermaid-ast
|
|
27
|
+
|
|
28
|
+
# npm
|
|
29
|
+
npm install mermaid-ast
|
|
30
|
+
|
|
31
|
+
# pnpm
|
|
32
|
+
pnpm add mermaid-ast
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Basic Parsing and Rendering
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { parse, render } from "mermaid-ast";
|
|
41
|
+
|
|
42
|
+
// Parse any supported diagram
|
|
43
|
+
const ast = parse(`flowchart LR
|
|
44
|
+
A[Start] --> B{Decision}
|
|
45
|
+
B -->|Yes| C[OK]
|
|
46
|
+
B -->|No| D[Cancel]`);
|
|
47
|
+
|
|
48
|
+
// Render back to Mermaid syntax
|
|
49
|
+
const output = render(ast);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Flowchart Diagrams
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { parseFlowchart, renderFlowchart } from "mermaid-ast";
|
|
56
|
+
|
|
57
|
+
const ast = parseFlowchart(`flowchart TD
|
|
58
|
+
A[Start] --> B{Is it working?}
|
|
59
|
+
B -->|Yes| C[Great!]
|
|
60
|
+
B -->|No| D[Debug]
|
|
61
|
+
D --> B`);
|
|
62
|
+
|
|
63
|
+
// Access AST properties
|
|
64
|
+
console.log(ast.direction); // "TD"
|
|
65
|
+
console.log(ast.nodes.size); // 4
|
|
66
|
+
console.log(ast.links.length); // 4
|
|
67
|
+
|
|
68
|
+
// Modify the AST
|
|
69
|
+
ast.nodes.get("A")!.text = { text: "Begin", type: "text" };
|
|
70
|
+
|
|
71
|
+
// Render back
|
|
72
|
+
const output = renderFlowchart(ast);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Sequence Diagrams
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { parseSequence, renderSequence } from "mermaid-ast";
|
|
79
|
+
|
|
80
|
+
const ast = parseSequence(`sequenceDiagram
|
|
81
|
+
participant A as Alice
|
|
82
|
+
participant B as Bob
|
|
83
|
+
A->>B: Hello Bob!
|
|
84
|
+
B-->>A: Hi Alice!`);
|
|
85
|
+
|
|
86
|
+
// Access AST properties
|
|
87
|
+
console.log(ast.actors.size); // 2
|
|
88
|
+
console.log(ast.statements.length); // 2
|
|
89
|
+
|
|
90
|
+
// Render back
|
|
91
|
+
const output = renderSequence(ast);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Diagram Type Detection
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { detectDiagramType } from "mermaid-ast";
|
|
98
|
+
|
|
99
|
+
detectDiagramType("flowchart LR\n A --> B"); // "flowchart"
|
|
100
|
+
detectDiagramType("sequenceDiagram\n A->>B: Hi"); // "sequence"
|
|
101
|
+
detectDiagramType("unknown diagram"); // null
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## API Reference
|
|
105
|
+
|
|
106
|
+
### Core Functions
|
|
107
|
+
|
|
108
|
+
| Function | Description |
|
|
109
|
+
|----------|-------------|
|
|
110
|
+
| `parse(input: string): MermaidAST` | Parse any supported diagram |
|
|
111
|
+
| `render(ast: MermaidAST): string` | Render any supported AST |
|
|
112
|
+
| `detectDiagramType(input: string): DiagramType \| null` | Detect diagram type |
|
|
113
|
+
|
|
114
|
+
### Flowchart Functions
|
|
115
|
+
|
|
116
|
+
| Function | Description |
|
|
117
|
+
|----------|-------------|
|
|
118
|
+
| `parseFlowchart(input: string): FlowchartAST` | Parse flowchart diagram |
|
|
119
|
+
| `renderFlowchart(ast: FlowchartAST): string` | Render flowchart AST |
|
|
120
|
+
| `isFlowchartDiagram(input: string): boolean` | Check if input is flowchart |
|
|
121
|
+
|
|
122
|
+
### Sequence Diagram Functions
|
|
123
|
+
|
|
124
|
+
| Function | Description |
|
|
125
|
+
|----------|-------------|
|
|
126
|
+
| `parseSequence(input: string): SequenceAST` | Parse sequence diagram |
|
|
127
|
+
| `renderSequence(ast: SequenceAST): string` | Render sequence AST |
|
|
128
|
+
| `isSequenceDiagram(input: string): boolean` | Check if input is sequence |
|
|
129
|
+
|
|
130
|
+
## Supported Flowchart Features
|
|
131
|
+
|
|
132
|
+
- **Directions**: LR, RL, TB, TD, BT
|
|
133
|
+
- **Node shapes**: Rectangle, rounded, diamond, stadium, subroutine, cylinder, circle, asymmetric, rhombus, hexagon, parallelogram, trapezoid, double-circle
|
|
134
|
+
- **Link types**: Arrow, open, cross, circle
|
|
135
|
+
- **Link strokes**: Normal, thick, dotted
|
|
136
|
+
- **Link labels**: Text on connections
|
|
137
|
+
- **Subgraphs**: With titles and directions
|
|
138
|
+
- **Styling**: `classDef`, `class`, `style`, `linkStyle`
|
|
139
|
+
- **Interactions**: `click` handlers with callbacks or URLs
|
|
140
|
+
|
|
141
|
+
## Supported Sequence Diagram Features
|
|
142
|
+
|
|
143
|
+
- **Participants/Actors**: With aliases
|
|
144
|
+
- **Message types**: Solid/dashed lines, arrows/open ends, async
|
|
145
|
+
- **Activations**: `activate`/`deactivate`
|
|
146
|
+
- **Control flow**: `loop`, `alt`/`else`, `opt`, `par`, `critical`, `break`
|
|
147
|
+
- **Grouping**: `rect` backgrounds
|
|
148
|
+
- **Notes**: `note left of`, `note right of`, `note over`
|
|
149
|
+
- **Actor lifecycle**: `create`, `destroy`
|
|
150
|
+
|
|
151
|
+
## Development
|
|
152
|
+
|
|
153
|
+
### Prerequisites
|
|
154
|
+
|
|
155
|
+
- [Bun](https://bun.sh/) runtime
|
|
156
|
+
|
|
157
|
+
### Commands
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
# Install dependencies
|
|
161
|
+
bun install
|
|
162
|
+
|
|
163
|
+
# Run tests
|
|
164
|
+
bun test
|
|
165
|
+
|
|
166
|
+
# Run specific test suites
|
|
167
|
+
bun test tests/unit # Unit tests
|
|
168
|
+
bun test tests/roundtrip # Round-trip tests
|
|
169
|
+
bun test tests/compatibility # Mermaid.js compatibility tests
|
|
170
|
+
|
|
171
|
+
# Type checking
|
|
172
|
+
bun run typecheck
|
|
173
|
+
|
|
174
|
+
# Build
|
|
175
|
+
bun run build
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Using the Justfile
|
|
179
|
+
|
|
180
|
+
If you have [just](https://github.com/casey/just) installed:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
just mermaid-ast test # Run all tests
|
|
184
|
+
just mermaid-ast test-roundtrip # Run round-trip tests
|
|
185
|
+
just mermaid-ast dagger-test-all # Test in all runtimes via Dagger
|
|
186
|
+
just mermaid-ast sync-parsers 11.4.2 # Sync parsers from mermaid version
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Updating Mermaid Parsers
|
|
190
|
+
|
|
191
|
+
This library vendors JISON parsers from mermaid.js. To update to a new mermaid version:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
# Sync from a specific version
|
|
195
|
+
bun run sync-parsers -- 11.4.2
|
|
196
|
+
|
|
197
|
+
# Or using just
|
|
198
|
+
just mermaid-ast sync-parsers 11.4.2
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
The sync script will:
|
|
202
|
+
1. Clone the specified mermaid version
|
|
203
|
+
2. Extract the JISON grammar files
|
|
204
|
+
3. Compile them to JavaScript parsers
|
|
205
|
+
4. Update the VERSION file
|
|
206
|
+
5. Run tests to verify compatibility
|
|
207
|
+
|
|
208
|
+
### Manual Update Process
|
|
209
|
+
|
|
210
|
+
If you need to update manually:
|
|
211
|
+
|
|
212
|
+
1. Check the [mermaid releases](https://github.com/mermaid-js/mermaid/releases) for the target version
|
|
213
|
+
2. Run the sync script with that version
|
|
214
|
+
3. Run the full test suite to verify compatibility
|
|
215
|
+
4. Review any failing tests - they may indicate breaking changes in the grammar
|
|
216
|
+
|
|
217
|
+
## Architecture
|
|
218
|
+
|
|
219
|
+
```
|
|
220
|
+
mermaid-ast/
|
|
221
|
+
├── src/
|
|
222
|
+
│ ├── parser/ # Diagram parsers
|
|
223
|
+
│ │ ├── flowchart-parser.ts
|
|
224
|
+
│ │ └── sequence-parser.ts
|
|
225
|
+
│ ├── renderer/ # AST to text renderers
|
|
226
|
+
│ │ ├── flowchart-renderer.ts
|
|
227
|
+
│ │ └── sequence-renderer.ts
|
|
228
|
+
│ ├── types/ # TypeScript type definitions
|
|
229
|
+
│ │ ├── flowchart.ts
|
|
230
|
+
│ │ └── sequence.ts
|
|
231
|
+
│ └── vendored/ # Vendored mermaid parsers
|
|
232
|
+
│ ├── grammars/ # Original JISON grammar files
|
|
233
|
+
│ └── parsers/ # Compiled JavaScript parsers
|
|
234
|
+
├── tests/
|
|
235
|
+
│ ├── unit/ # Unit tests for parser/renderer
|
|
236
|
+
│ ├── roundtrip/ # Round-trip verification tests
|
|
237
|
+
│ └── compatibility/ # Mermaid.js SVG compatibility tests
|
|
238
|
+
└── scripts/
|
|
239
|
+
└── sync-parsers.ts # Parser sync script
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## License
|
|
243
|
+
|
|
244
|
+
MIT
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## How This Library Was Built
|
|
249
|
+
|
|
250
|
+
This library was built entirely through conversation with [Slate](https://randomlabs.ai), an AI coding assistant. Below are the prompts used:
|
|
251
|
+
|
|
252
|
+
### Initial Request
|
|
253
|
+
|
|
254
|
+
> I want a TypeScript library to parse and render Mermaid AST, with extensive unit tests, support for the entire syntax supported by the current version of mermaid-js, tests that show specifically that mermaid.js produces absolutely the same output for before-the-roundtrip and after-the-roundtrip diagrams, and documentation on how to bring itself up to date with newer versions of mermaid.js.
|
|
255
|
+
|
|
256
|
+
### Runtime & Scope
|
|
257
|
+
|
|
258
|
+
> go with bun
|
|
259
|
+
>
|
|
260
|
+
> tests in Dagger that demonstrate that it works the same in bun, deno, and node.js
|
|
261
|
+
>
|
|
262
|
+
> subset for now
|
|
263
|
+
>
|
|
264
|
+
> depend, sure (on @mermaid-js/parser)
|
|
265
|
+
|
|
266
|
+
### Parser Approach
|
|
267
|
+
|
|
268
|
+
> absolutely you must not use any regex approach whatsoever
|
|
269
|
+
>
|
|
270
|
+
> study the mermaidjs/parser package thoroughly
|
|
271
|
+
>
|
|
272
|
+
> ok please study first how mermaidjs parses diagrams and then we'll discuss
|
|
273
|
+
>
|
|
274
|
+
> ok but would you vendor the parsers and have like a script to re-vendor, or what would you do? how's it done usually?
|
|
275
|
+
>
|
|
276
|
+
> lets have a sync script ok
|
|
277
|
+
|
|
278
|
+
### Project Setup
|
|
279
|
+
|
|
280
|
+
> all these commands must be in justfile in mermaid-ast/
|
|
281
|
+
>
|
|
282
|
+
> can we make it work from the root?
|
|
283
|
+
|
|
284
|
+
### Quality Check
|
|
285
|
+
|
|
286
|
+
> ok cool now tell me if theres any tech debt or like anything halfassed
|
|
287
|
+
|
|
288
|
+
### Final Polish
|
|
289
|
+
|
|
290
|
+
> ok cool do 1 2, no need for 3, need everything in 5, need 6, need 7. make a todo list then discuss
|
|
291
|
+
>
|
|
292
|
+
> use mermaid js rendering to svg. readme should include just implemented. for (3) explain how exactly it happened
|
|
293
|
+
|
|
294
|
+
### Publishing
|
|
295
|
+
|
|
296
|
+
> create a repo in neongreen github and move the package there
|
|
297
|
+
>
|
|
298
|
+
> and make it private at first
|
|
299
|
+
>
|
|
300
|
+
> ok now finally in ~/code/mermaid-ast/AGENTS.md please document how this library was made etc. because i'm going to start a new agent there to continue work
|
|
301
|
+
>
|
|
302
|
+
> add a badge there: proudly built with Slate. and include my prompts from this convo
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mermaid-ast
|
|
3
|
+
*
|
|
4
|
+
* A library for parsing and rendering Mermaid diagrams to/from AST.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { parse, render } from "mermaid-ast";
|
|
9
|
+
*
|
|
10
|
+
* const diagram = `
|
|
11
|
+
* flowchart LR
|
|
12
|
+
* A[Start] --> B[End]
|
|
13
|
+
* `;
|
|
14
|
+
*
|
|
15
|
+
* const ast = parse(diagram);
|
|
16
|
+
* const output = render(ast);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export * from "./types/index.js";
|
|
20
|
+
export { parse, parseFlowchart, parseSequence, detectDiagramType, isFlowchartDiagram, isSequenceDiagram, } from "./parser/index.js";
|
|
21
|
+
export { render, renderFlowchart, renderSequence, } from "./renderer/index.js";
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,cAAc,kBAAkB,CAAC;AAGjC,OAAO,EACL,KAAK,EACL,cAAc,EACd,aAAa,EACb,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,MAAM,EACN,eAAe,EACf,cAAc,GACf,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mermaid-ast
|
|
3
|
+
*
|
|
4
|
+
* A library for parsing and rendering Mermaid diagrams to/from AST.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { parse, render } from "mermaid-ast";
|
|
9
|
+
*
|
|
10
|
+
* const diagram = `
|
|
11
|
+
* flowchart LR
|
|
12
|
+
* A[Start] --> B[End]
|
|
13
|
+
* `;
|
|
14
|
+
*
|
|
15
|
+
* const ast = parse(diagram);
|
|
16
|
+
* const output = render(ast);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
// Export types
|
|
20
|
+
export * from "./types/index.js";
|
|
21
|
+
// Export parser functions
|
|
22
|
+
export { parse, parseFlowchart, parseSequence, detectDiagramType, isFlowchartDiagram, isSequenceDiagram, } from "./parser/index.js";
|
|
23
|
+
// Export renderer functions (to be implemented)
|
|
24
|
+
export { render, renderFlowchart, renderSequence, } from "./renderer/index.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowchart Parser
|
|
3
|
+
*
|
|
4
|
+
* Parses Mermaid flowchart syntax into an AST using the vendored JISON parser.
|
|
5
|
+
* The JISON parser calls methods on a `yy` object - we provide our own implementation
|
|
6
|
+
* that builds an AST instead of mermaid's internal db structure.
|
|
7
|
+
*/
|
|
8
|
+
import { type FlowchartAST } from "../types/flowchart.js";
|
|
9
|
+
/**
|
|
10
|
+
* Parse a flowchart diagram string into an AST
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseFlowchart(input: string): FlowchartAST;
|
|
13
|
+
/**
|
|
14
|
+
* Detect if input is a flowchart diagram
|
|
15
|
+
*/
|
|
16
|
+
export declare function isFlowchartDiagram(input: string): boolean;
|
|
17
|
+
//# sourceMappingURL=flowchart-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flowchart-parser.d.ts","sourceRoot":"","sources":["../../src/parser/flowchart-parser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,KAAK,YAAY,EAalB,MAAM,uBAAuB,CAAC;AA+Z/B;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,CAkB1D;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAQzD"}
|