mermaid-ast 0.4.0 → 0.5.1
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/README.md +265 -85
- package/dist/class-diagram.d.ts +129 -0
- package/dist/class-diagram.d.ts.map +1 -0
- package/dist/class-diagram.js +341 -0
- package/dist/diagram-wrapper.d.ts +66 -0
- package/dist/diagram-wrapper.d.ts.map +1 -0
- package/dist/diagram-wrapper.js +59 -0
- package/dist/er-diagram.d.ts +214 -0
- package/dist/er-diagram.d.ts.map +1 -0
- package/dist/er-diagram.js +405 -0
- package/dist/flowchart-graph-ops.d.ts +58 -0
- package/dist/flowchart-graph-ops.d.ts.map +1 -0
- package/dist/flowchart-graph-ops.js +262 -0
- package/dist/flowchart-types.d.ts +58 -0
- package/dist/flowchart-types.d.ts.map +1 -0
- package/dist/flowchart-types.js +6 -0
- package/dist/flowchart.d.ts +304 -0
- package/dist/flowchart.d.ts.map +1 -0
- package/dist/flowchart.js +615 -0
- package/dist/gantt.d.ts +199 -0
- package/dist/gantt.d.ts.map +1 -0
- package/dist/gantt.js +452 -0
- package/dist/index.d.ts +22 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -2
- package/dist/journey.d.ts +150 -0
- package/dist/journey.d.ts.map +1 -0
- package/dist/journey.js +317 -0
- package/dist/mindmap.d.ts +142 -0
- package/dist/mindmap.d.ts.map +1 -0
- package/dist/mindmap.js +350 -0
- package/dist/parser/er-parser.d.ts +13 -0
- package/dist/parser/er-parser.d.ts.map +1 -0
- package/dist/parser/er-parser.js +119 -0
- package/dist/parser/gantt-parser.d.ts +13 -0
- package/dist/parser/gantt-parser.d.ts.map +1 -0
- package/dist/parser/gantt-parser.js +182 -0
- package/dist/parser/journey-parser.d.ts +13 -0
- package/dist/parser/journey-parser.d.ts.map +1 -0
- package/dist/parser/journey-parser.js +99 -0
- package/dist/parser/mindmap-parser.d.ts +13 -0
- package/dist/parser/mindmap-parser.d.ts.map +1 -0
- package/dist/parser/mindmap-parser.js +196 -0
- package/dist/parser/timeline-parser.d.ts +13 -0
- package/dist/parser/timeline-parser.d.ts.map +1 -0
- package/dist/parser/timeline-parser.js +97 -0
- package/dist/renderer/er-renderer.d.ts +19 -0
- package/dist/renderer/er-renderer.d.ts.map +1 -0
- package/dist/renderer/er-renderer.js +131 -0
- package/dist/renderer/flowchart-renderer.js +1 -1
- package/dist/renderer/gantt-renderer.d.ts +19 -0
- package/dist/renderer/gantt-renderer.d.ts.map +1 -0
- package/dist/renderer/gantt-renderer.js +118 -0
- package/dist/renderer/journey-renderer.d.ts +19 -0
- package/dist/renderer/journey-renderer.d.ts.map +1 -0
- package/dist/renderer/journey-renderer.js +40 -0
- package/dist/renderer/mindmap-renderer.d.ts +19 -0
- package/dist/renderer/mindmap-renderer.d.ts.map +1 -0
- package/dist/renderer/mindmap-renderer.js +98 -0
- package/dist/renderer/timeline-renderer.d.ts +19 -0
- package/dist/renderer/timeline-renderer.d.ts.map +1 -0
- package/dist/renderer/timeline-renderer.js +37 -0
- package/dist/sequence.d.ts +134 -0
- package/dist/sequence.d.ts.map +1 -0
- package/dist/sequence.js +457 -0
- package/dist/state-diagram.d.ts +117 -0
- package/dist/state-diagram.d.ts.map +1 -0
- package/dist/state-diagram.js +331 -0
- package/dist/timeline.d.ts +150 -0
- package/dist/timeline.d.ts.map +1 -0
- package/dist/timeline.js +334 -0
- package/dist/types/er.d.ts +105 -0
- package/dist/types/er.d.ts.map +1 -0
- package/dist/types/er.js +19 -0
- package/dist/types/gantt.d.ts +101 -0
- package/dist/types/gantt.d.ts.map +1 -0
- package/dist/types/gantt.js +18 -0
- package/dist/types/index.d.ts +32 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +35 -0
- package/dist/types/journey.d.ts +44 -0
- package/dist/types/journey.d.ts.map +1 -0
- package/dist/types/journey.js +14 -0
- package/dist/types/mindmap.d.ts +45 -0
- package/dist/types/mindmap.d.ts.map +1 -0
- package/dist/types/mindmap.js +13 -0
- package/dist/types/timeline.d.ts +49 -0
- package/dist/types/timeline.d.ts.map +1 -0
- package/dist/types/timeline.js +14 -0
- package/dist/vendored/grammars/er.jison +294 -0
- package/dist/vendored/grammars/gantt.jison +188 -0
- package/dist/vendored/grammars/journey.jison +69 -0
- package/dist/vendored/grammars/mindmap.jison +127 -0
- package/dist/vendored/grammars/timeline.jison +79 -0
- package/dist/vendored/parsers/er.js +891 -0
- package/dist/vendored/parsers/gantt.js +762 -0
- package/dist/vendored/parsers/journey.js +615 -0
- package/dist/vendored/parsers/mindmap.js +663 -0
- package/dist/vendored/parsers/timeline.js +616 -0
- package/dist/vendored/sync-info.json +14 -9
- package/package.json +2 -6
- package/dist/builder/class-builder.d.ts +0 -148
- package/dist/builder/class-builder.d.ts.map +0 -1
- package/dist/builder/class-builder.js +0 -318
- package/dist/builder/flowchart-builder.d.ts +0 -123
- package/dist/builder/flowchart-builder.d.ts.map +0 -1
- package/dist/builder/flowchart-builder.js +0 -257
- package/dist/builder/index.d.ts +0 -10
- package/dist/builder/index.d.ts.map +0 -1
- package/dist/builder/index.js +0 -9
- package/dist/builder/sequence-builder.d.ts +0 -174
- package/dist/builder/sequence-builder.d.ts.map +0 -1
- package/dist/builder/sequence-builder.js +0 -443
- package/dist/builder/state-builder.d.ts +0 -156
- package/dist/builder/state-builder.d.ts.map +0 -1
- package/dist/builder/state-builder.js +0 -314
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# mermaid-ast
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/mermaid-ast)
|
|
4
|
+
[](https://jsr.io/@emily/mermaid-ast)
|
|
4
5
|
[](https://randomlabs.ai)
|
|
5
6
|
|
|
6
7
|
Parse and render Mermaid diagrams to/from AST (Abstract Syntax Tree).
|
|
@@ -17,17 +18,17 @@ This library provides a way to programmatically work with Mermaid diagrams by pa
|
|
|
17
18
|
|
|
18
19
|
## Supported Diagram Types
|
|
19
20
|
|
|
20
|
-
| Diagram Type | Parse | Render |
|
|
21
|
-
|
|
22
|
-
| Flowchart (`flowchart`, `graph`) | ✅ | ✅ | ✅ |
|
|
23
|
-
| Sequence (`sequenceDiagram`) | ✅ | ✅ | ✅ |
|
|
24
|
-
| Class (`classDiagram`) | ✅ | ✅ | ✅ |
|
|
25
|
-
| State (`stateDiagram`) | ✅ | ✅ | ✅ |
|
|
26
|
-
| ER Diagram (`erDiagram`) |
|
|
27
|
-
| Gantt (`gantt`) |
|
|
28
|
-
|
|
|
29
|
-
|
|
|
30
|
-
| Timeline (`timeline`) |
|
|
21
|
+
| Diagram Type | Parse | Render | Wrapper Class |
|
|
22
|
+
|--------------|-------|--------|---------------|
|
|
23
|
+
| Flowchart (`flowchart`, `graph`) | ✅ | ✅ | ✅ `Flowchart` |
|
|
24
|
+
| Sequence (`sequenceDiagram`) | ✅ | ✅ | ✅ `Sequence` |
|
|
25
|
+
| Class (`classDiagram`) | ✅ | ✅ | ✅ `ClassDiagram` |
|
|
26
|
+
| State (`stateDiagram`) | ✅ | ✅ | ✅ `StateDiagram` |
|
|
27
|
+
| ER Diagram (`erDiagram`) | ✅ | ✅ | ✅ `ErDiagram` |
|
|
28
|
+
| Gantt (`gantt`) | ✅ | ✅ | ✅ `Gantt` |
|
|
29
|
+
| Mindmap (`mindmap`) | ✅ | ✅ | ✅ `Mindmap` |
|
|
30
|
+
| Journey (`journey`) | ✅ | ✅ | ✅ `Journey` |
|
|
31
|
+
| Timeline (`timeline`) | ✅ | ✅ | ✅ `Timeline` |
|
|
31
32
|
| Pie (`pie`) | ❌ | ❌ | ❌ |
|
|
32
33
|
| Quadrant (`quadrantChart`) | ❌ | ❌ | ❌ |
|
|
33
34
|
| Requirement (`requirementDiagram`) | ❌ | ❌ | ❌ |
|
|
@@ -147,108 +148,287 @@ detectDiagramType("classDiagram\n class Animal"); // "class"
|
|
|
147
148
|
detectDiagramType("unknown diagram"); // null
|
|
148
149
|
```
|
|
149
150
|
|
|
150
|
-
##
|
|
151
|
+
## Flowchart Wrapper Class (Recommended)
|
|
151
152
|
|
|
152
|
-
|
|
153
|
+
The `Flowchart` class provides a unified API for building, mutating, and querying flowchart diagrams. This is the recommended way to work with flowcharts programmatically.
|
|
153
154
|
|
|
154
|
-
###
|
|
155
|
+
### Creating Flowcharts
|
|
155
156
|
|
|
156
157
|
```typescript
|
|
157
|
-
import {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
.subgraph("sub1", "My Group", (s) => {
|
|
166
|
-
s.node("D", "Inner").link("D", "B");
|
|
167
|
-
})
|
|
168
|
-
.classDef("highlight", { fill: "#f9f" })
|
|
169
|
-
.class("A", "highlight")
|
|
170
|
-
.build();
|
|
158
|
+
import { Flowchart } from "mermaid-ast";
|
|
159
|
+
|
|
160
|
+
// Create empty flowchart
|
|
161
|
+
const f = Flowchart.create("LR");
|
|
162
|
+
|
|
163
|
+
// Parse existing diagram
|
|
164
|
+
const f = Flowchart.parse(`flowchart LR
|
|
165
|
+
A --> B --> C`);
|
|
171
166
|
|
|
172
|
-
|
|
167
|
+
// Wrap existing AST
|
|
168
|
+
const f = Flowchart.from(existingAst);
|
|
173
169
|
```
|
|
174
170
|
|
|
175
|
-
###
|
|
171
|
+
### Building Flowcharts
|
|
176
172
|
|
|
177
173
|
```typescript
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
.
|
|
182
|
-
.
|
|
183
|
-
.
|
|
184
|
-
.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
{ condition: "Success", build: (b) => b.message("A", "B", "OK") },
|
|
189
|
-
{ condition: "Failure", build: (b) => b.message("A", "B", "Error") },
|
|
190
|
-
])
|
|
191
|
-
.note("A", "Important!", { placement: "right_of" })
|
|
192
|
-
.build();
|
|
193
|
-
|
|
194
|
-
const text = render(ast);
|
|
174
|
+
const diagram = Flowchart.create("LR")
|
|
175
|
+
.addNode("A", "Start", { shape: "stadium" })
|
|
176
|
+
.addNode("B", "Process")
|
|
177
|
+
.addNode("C", "End", { shape: "circle" })
|
|
178
|
+
.addLink("A", "B", { text: "begin" })
|
|
179
|
+
.addLink("B", "C")
|
|
180
|
+
.createSubgraph("sub1", ["B"], "Processing")
|
|
181
|
+
.addClass("A", "highlight");
|
|
182
|
+
|
|
183
|
+
console.log(diagram.render());
|
|
195
184
|
```
|
|
196
185
|
|
|
197
|
-
###
|
|
186
|
+
### Mutating Flowcharts
|
|
198
187
|
|
|
199
188
|
```typescript
|
|
200
|
-
|
|
189
|
+
const diagram = Flowchart.parse(`flowchart LR
|
|
190
|
+
A --> B --> C --> D`);
|
|
201
191
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
.property("age: int", "-")
|
|
206
|
-
.method("speak()", "+");
|
|
207
|
-
})
|
|
208
|
-
.class("Dog")
|
|
209
|
-
.extends("Dog", "Animal")
|
|
210
|
-
.composition("Dog", "Tail")
|
|
211
|
-
.class("Tail")
|
|
212
|
-
.build();
|
|
192
|
+
// Change node properties
|
|
193
|
+
diagram.setNodeText("A", "New Start");
|
|
194
|
+
diagram.setNodeShape("A", "diamond");
|
|
213
195
|
|
|
214
|
-
|
|
196
|
+
// Modify links
|
|
197
|
+
diagram.flipLink(0); // Reverse direction
|
|
198
|
+
diagram.setLinkText(0, "label");
|
|
199
|
+
|
|
200
|
+
// Add/remove classes
|
|
201
|
+
diagram.addClass("B", "highlight");
|
|
202
|
+
diagram.removeClass("B", "highlight");
|
|
203
|
+
|
|
204
|
+
// Remove a node and reconnect neighbors
|
|
205
|
+
diagram.removeNode("B", { reconnect: true });
|
|
206
|
+
// Result: A --> C --> D
|
|
215
207
|
```
|
|
216
208
|
|
|
217
|
-
###
|
|
209
|
+
### Querying Flowcharts
|
|
218
210
|
|
|
219
211
|
```typescript
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
212
|
+
const diagram = Flowchart.parse(`flowchart TD
|
|
213
|
+
A[Start] --> B{Decision}
|
|
214
|
+
B -->|Yes| C[OK]
|
|
215
|
+
B -->|No| D[Cancel]`);
|
|
216
|
+
|
|
217
|
+
// Find nodes
|
|
218
|
+
diagram.findNodes({ class: "highlight" });
|
|
219
|
+
diagram.findNodes({ shape: "diamond" });
|
|
220
|
+
diagram.findNodes({ textContains: "Start" });
|
|
221
|
+
|
|
222
|
+
// Get links
|
|
223
|
+
diagram.getLinksFrom("B"); // Outgoing links
|
|
224
|
+
diagram.getLinksTo("B"); // Incoming links
|
|
225
|
+
|
|
226
|
+
// Graph traversal
|
|
227
|
+
diagram.getReachable("A"); // All nodes reachable from A
|
|
228
|
+
diagram.getAncestors("C"); // All nodes that can reach C
|
|
229
|
+
diagram.getPath("A", "C"); // Shortest path: ["A", "B", "C"]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Chain Operations (jj-style)
|
|
233
|
+
|
|
234
|
+
Inspired by [jj](https://github.com/martinvonz/jj), these operations let you manipulate chains of nodes:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
const diagram = Flowchart.create()
|
|
238
|
+
.addNode("X").addNode("A").addNode("B").addNode("C").addNode("Y")
|
|
239
|
+
.addLink("X", "A").addLink("A", "B").addLink("B", "C").addLink("C", "Y");
|
|
240
|
+
|
|
241
|
+
// Yank (remove) a chain and reconnect
|
|
242
|
+
diagram.yankChain(["A", "B", "C"]);
|
|
243
|
+
// Result: X --> Y
|
|
244
|
+
|
|
245
|
+
// Splice a chain between nodes
|
|
246
|
+
diagram.spliceChain(["A", "B"], "X", "Y");
|
|
247
|
+
// Result: X --> A --> B --> Y
|
|
248
|
+
|
|
249
|
+
// Reverse a chain's direction
|
|
250
|
+
diagram.reverseChain(["A", "B", "C"]);
|
|
251
|
+
// Changes A-->B-->C to A<--B<--C
|
|
252
|
+
|
|
253
|
+
// Extract a chain as a new Flowchart
|
|
254
|
+
const extracted = diagram.extractChain(["B", "C"]);
|
|
255
|
+
|
|
256
|
+
// Rebase nodes to a new parent
|
|
257
|
+
diagram.rebaseNodes(["A", "B"], "NewParent");
|
|
258
|
+
|
|
259
|
+
// Get a linear chain between two nodes
|
|
260
|
+
diagram.getChain("A", "D"); // ["A", "B", "C", "D"] if linear
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Subgraph Operations
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
const diagram = Flowchart.create()
|
|
267
|
+
.addNode("A").addNode("B").addNode("C")
|
|
268
|
+
.createSubgraph("sub1", ["A", "B"], "Group 1")
|
|
269
|
+
.createSubgraph("sub2", ["C"], "Group 2");
|
|
270
|
+
|
|
271
|
+
// Move nodes between subgraphs
|
|
272
|
+
diagram.moveToSubgraph(["B"], "sub2");
|
|
273
|
+
|
|
274
|
+
// Extract nodes to root level
|
|
275
|
+
diagram.extractFromSubgraph(["A"]);
|
|
276
|
+
|
|
277
|
+
// Merge subgraphs
|
|
278
|
+
diagram.mergeSubgraphs("sub1", "sub2");
|
|
279
|
+
|
|
280
|
+
// Dissolve a subgraph (keep nodes, remove grouping)
|
|
281
|
+
diagram.dissolveSubgraph("sub1");
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Other Wrapper Classes
|
|
234
285
|
|
|
235
|
-
|
|
286
|
+
Similar to `Flowchart`, we provide wrapper classes for other diagram types:
|
|
287
|
+
|
|
288
|
+
### ErDiagram
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
import { ErDiagram } from "mermaid-ast";
|
|
292
|
+
|
|
293
|
+
const diagram = ErDiagram.create()
|
|
294
|
+
.addEntity("Customer")
|
|
295
|
+
.addAttribute("Customer", "id", "int", ["PK"])
|
|
296
|
+
.addAttribute("Customer", "name", "string")
|
|
297
|
+
.addEntity("Order")
|
|
298
|
+
.addRelationship("Customer", "Order", "places", {
|
|
299
|
+
fromCardinality: "one_or_more",
|
|
300
|
+
toCardinality: "zero_or_more"
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
console.log(diagram.render());
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Gantt
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import { Gantt } from "mermaid-ast";
|
|
310
|
+
|
|
311
|
+
const diagram = Gantt.create("Project Timeline")
|
|
312
|
+
.setDateFormat("YYYY-MM-DD")
|
|
313
|
+
.addSection("Planning")
|
|
314
|
+
.addTask("Planning", "Research", { id: "a1", duration: "3d" })
|
|
315
|
+
.addTask("Planning", "Design", { id: "a2", duration: "5d", after: "a1" })
|
|
316
|
+
.addSection("Development")
|
|
317
|
+
.addTask("Development", "Implementation", { duration: "10d", after: "a2" });
|
|
318
|
+
|
|
319
|
+
console.log(diagram.render());
|
|
236
320
|
```
|
|
237
321
|
|
|
238
|
-
###
|
|
322
|
+
### Mindmap
|
|
239
323
|
|
|
240
|
-
|
|
324
|
+
```typescript
|
|
325
|
+
import { Mindmap } from "mermaid-ast";
|
|
326
|
+
|
|
327
|
+
const diagram = Mindmap.create("Project")
|
|
328
|
+
.addChild("root", "Frontend", { shape: "square" })
|
|
329
|
+
.addChild("root", "Backend", { shape: "square" })
|
|
330
|
+
.addChild("Frontend", "React")
|
|
331
|
+
.addChild("Frontend", "CSS")
|
|
332
|
+
.addChild("Backend", "API")
|
|
333
|
+
.addChild("Backend", "Database");
|
|
334
|
+
|
|
335
|
+
console.log(diagram.render());
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Journey
|
|
241
339
|
|
|
242
340
|
```typescript
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
341
|
+
import { Journey } from "mermaid-ast";
|
|
342
|
+
|
|
343
|
+
const diagram = Journey.create("User Onboarding")
|
|
344
|
+
.addSection("Discovery")
|
|
345
|
+
.addTask("Discovery", "Visit website", 5, ["User"])
|
|
346
|
+
.addTask("Discovery", "Read docs", 3, ["User"])
|
|
347
|
+
.addSection("Signup")
|
|
348
|
+
.addTask("Signup", "Create account", 4, ["User"])
|
|
349
|
+
.addTask("Signup", "Verify email", 2, ["User", "System"]);
|
|
350
|
+
|
|
351
|
+
console.log(diagram.render());
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Timeline
|
|
246
355
|
|
|
247
|
-
|
|
248
|
-
|
|
356
|
+
```typescript
|
|
357
|
+
import { Timeline } from "mermaid-ast";
|
|
358
|
+
|
|
359
|
+
const diagram = Timeline.create("Company History")
|
|
360
|
+
.addSection("2020s")
|
|
361
|
+
.addPeriod("2020s", "2020")
|
|
362
|
+
.addEvent("2020", "Company founded")
|
|
363
|
+
.addEvent("2020", "First product launch")
|
|
364
|
+
.addPeriod("2020s", "2023")
|
|
365
|
+
.addEvent("2023", "Series A funding");
|
|
366
|
+
|
|
367
|
+
console.log(diagram.render());
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### Sequence
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
import { Sequence } from "mermaid-ast";
|
|
374
|
+
|
|
375
|
+
const diagram = Sequence.create()
|
|
376
|
+
.addActor("alice", "Alice", "participant")
|
|
377
|
+
.addActor("bob", "Bob", "actor")
|
|
378
|
+
.addMessage("alice", "bob", "Hello Bob!", { type: "solid", arrow: "filled" })
|
|
379
|
+
.addMessage("bob", "alice", "Hi Alice!", { type: "dashed", arrow: "filled" })
|
|
380
|
+
.addLoop("Every minute", (loop) => {
|
|
381
|
+
loop.addMessage("bob", "alice", "Ping");
|
|
382
|
+
})
|
|
383
|
+
.addNote("alice", "Important note", { placement: "right" });
|
|
384
|
+
|
|
385
|
+
console.log(diagram.render());
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
### ClassDiagram
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
import { ClassDiagram } from "mermaid-ast";
|
|
392
|
+
|
|
393
|
+
const diagram = ClassDiagram.create()
|
|
394
|
+
.addClass("Animal")
|
|
395
|
+
.addAttribute("Animal", "name: string", "+")
|
|
396
|
+
.addAttribute("Animal", "age: int", "-")
|
|
397
|
+
.addMethod("Animal", "speak()", "+")
|
|
398
|
+
.addClass("Dog")
|
|
399
|
+
.addInheritance("Dog", "Animal")
|
|
400
|
+
.addMethod("Dog", "bark()", "+")
|
|
401
|
+
.addClass("Cat")
|
|
402
|
+
.addInheritance("Cat", "Animal")
|
|
403
|
+
.addMethod("Cat", "meow()", "+");
|
|
404
|
+
|
|
405
|
+
console.log(diagram.render());
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### StateDiagram
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
import { StateDiagram } from "mermaid-ast";
|
|
412
|
+
|
|
413
|
+
const diagram = StateDiagram.create()
|
|
414
|
+
.addState("Idle", { description: "Waiting for input" })
|
|
415
|
+
.addState("Running")
|
|
416
|
+
.addState("Done")
|
|
417
|
+
.addInitial("Idle")
|
|
418
|
+
.addTransition("Idle", "Running", { label: "start" })
|
|
419
|
+
.addTransition("Running", "Done", { label: "complete" })
|
|
420
|
+
.addFinal("Done")
|
|
421
|
+
.addComposite("Running", (composite) => {
|
|
422
|
+
composite
|
|
423
|
+
.addState("Step1")
|
|
424
|
+
.addState("Step2")
|
|
425
|
+
.addTransition("Step1", "Step2");
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
console.log(diagram.render());
|
|
249
429
|
```
|
|
250
430
|
|
|
251
|
-
|
|
431
|
+
## Render Options (Pretty-Print)
|
|
252
432
|
|
|
253
433
|
All render functions accept an optional `RenderOptions` object to customize output formatting:
|
|
254
434
|
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class Diagram Wrapper Class
|
|
3
|
+
*
|
|
4
|
+
* A unified API for building, mutating, and querying class diagrams.
|
|
5
|
+
* Provides a fluent interface that wraps the ClassDiagramAST.
|
|
6
|
+
*/
|
|
7
|
+
import { DiagramWrapper } from './diagram-wrapper.js';
|
|
8
|
+
import type { ClassDefinition, ClassDiagramAST, ClassDirection, ClassMember, ClassRelation, LineType, RelationType } from './types/class.js';
|
|
9
|
+
import type { RenderOptions } from './types/render-options.js';
|
|
10
|
+
/** Options for adding a class */
|
|
11
|
+
export interface AddClassOptions {
|
|
12
|
+
label?: string;
|
|
13
|
+
annotation?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Options for adding a member */
|
|
16
|
+
export interface AddMemberOptions {
|
|
17
|
+
visibility?: '+' | '-' | '#' | '~';
|
|
18
|
+
type?: 'method' | 'attribute';
|
|
19
|
+
}
|
|
20
|
+
/** Options for adding a relation */
|
|
21
|
+
export interface AddRelationOptions {
|
|
22
|
+
label?: string;
|
|
23
|
+
labelFrom?: string;
|
|
24
|
+
labelTo?: string;
|
|
25
|
+
lineType?: LineType;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A fluent wrapper for ClassDiagramAST that supports building, mutating, and querying.
|
|
29
|
+
*/
|
|
30
|
+
export declare class ClassDiagram extends DiagramWrapper<ClassDiagramAST> {
|
|
31
|
+
private constructor();
|
|
32
|
+
/** Create a new empty class diagram */
|
|
33
|
+
static create(direction?: ClassDirection): ClassDiagram;
|
|
34
|
+
/** Create from an existing AST */
|
|
35
|
+
static from(ast: ClassDiagramAST): ClassDiagram;
|
|
36
|
+
/** Parse Mermaid syntax into a ClassDiagram */
|
|
37
|
+
static parse(input: string): ClassDiagram;
|
|
38
|
+
render(options?: RenderOptions): string;
|
|
39
|
+
clone(): ClassDiagram;
|
|
40
|
+
get direction(): ClassDirection;
|
|
41
|
+
get classCount(): number;
|
|
42
|
+
get relationCount(): number;
|
|
43
|
+
get classes(): Map<string, {
|
|
44
|
+
id: string;
|
|
45
|
+
label?: string;
|
|
46
|
+
memberCount: number;
|
|
47
|
+
}>;
|
|
48
|
+
setDirection(direction: ClassDirection): this;
|
|
49
|
+
/** Add a class */
|
|
50
|
+
addClass(id: string, options?: AddClassOptions): this;
|
|
51
|
+
/** Remove a class and optionally its relations */
|
|
52
|
+
removeClass(id: string, options?: {
|
|
53
|
+
removeRelations?: boolean;
|
|
54
|
+
}): this;
|
|
55
|
+
/** Rename a class */
|
|
56
|
+
renameClass(id: string, newId: string): this;
|
|
57
|
+
/** Get a class by ID */
|
|
58
|
+
getClass(id: string): ClassDefinition | undefined;
|
|
59
|
+
/** Check if class exists */
|
|
60
|
+
hasClass(id: string): boolean;
|
|
61
|
+
/** Set class label */
|
|
62
|
+
setClassLabel(id: string, label: string): this;
|
|
63
|
+
/** Add annotation to class */
|
|
64
|
+
addAnnotation(id: string, annotation: string): this;
|
|
65
|
+
/** Remove annotation from class */
|
|
66
|
+
removeAnnotation(id: string, annotation: string): this;
|
|
67
|
+
/** Add a member (attribute or method) to a class */
|
|
68
|
+
addMember(classId: string, text: string, options?: AddMemberOptions): this;
|
|
69
|
+
/** Add an attribute to a class */
|
|
70
|
+
addAttribute(classId: string, text: string, visibility?: '+' | '-' | '#' | '~'): this;
|
|
71
|
+
/** Add a method to a class */
|
|
72
|
+
addMethod(classId: string, text: string, visibility?: '+' | '-' | '#' | '~'): this;
|
|
73
|
+
/** Get members of a class */
|
|
74
|
+
getMembers(classId: string): ClassMember[];
|
|
75
|
+
/** Remove a member from a class */
|
|
76
|
+
removeMember(classId: string, text: string): this;
|
|
77
|
+
/** Add a relation between classes */
|
|
78
|
+
addRelation(from: string, to: string, relationType: RelationType, options?: AddRelationOptions): this;
|
|
79
|
+
/** Add inheritance relation (A extends B) */
|
|
80
|
+
addInheritance(child: string, parent: string, options?: AddRelationOptions): this;
|
|
81
|
+
/** Add composition relation */
|
|
82
|
+
addComposition(whole: string, part: string, options?: AddRelationOptions): this;
|
|
83
|
+
/** Add aggregation relation */
|
|
84
|
+
addAggregation(whole: string, part: string, options?: AddRelationOptions): this;
|
|
85
|
+
/** Add dependency relation */
|
|
86
|
+
addDependency(from: string, to: string, options?: AddRelationOptions): this;
|
|
87
|
+
/** Add association (simple line) */
|
|
88
|
+
addAssociation(from: string, to: string, options?: AddRelationOptions): this;
|
|
89
|
+
/** Get all relations */
|
|
90
|
+
getRelations(): ClassRelation[];
|
|
91
|
+
/** Get relations for a class */
|
|
92
|
+
getRelationsFor(classId: string): ClassRelation[];
|
|
93
|
+
/** Remove a relation */
|
|
94
|
+
removeRelation(from: string, to: string): this;
|
|
95
|
+
/** Add a namespace */
|
|
96
|
+
addNamespace(name: string, classIds?: string[]): this;
|
|
97
|
+
/** Add class to namespace */
|
|
98
|
+
addToNamespace(namespace: string, classId: string): this;
|
|
99
|
+
/** Remove class from namespace */
|
|
100
|
+
removeFromNamespace(namespace: string, classId: string): this;
|
|
101
|
+
/** Get namespace for a class */
|
|
102
|
+
getNamespaceFor(classId: string): string | undefined;
|
|
103
|
+
/** Add a note */
|
|
104
|
+
addNote(text: string, forClass?: string): this;
|
|
105
|
+
/** Get all notes */
|
|
106
|
+
getNotes(): Array<{
|
|
107
|
+
text: string;
|
|
108
|
+
forClass?: string;
|
|
109
|
+
}>;
|
|
110
|
+
/** Define a class style */
|
|
111
|
+
defineStyle(name: string, styles: string[]): this;
|
|
112
|
+
/** Apply style to a class */
|
|
113
|
+
applyStyle(classId: string, styleName: string): this;
|
|
114
|
+
/** Find classes by criteria */
|
|
115
|
+
findClasses(query: {
|
|
116
|
+
hasAnnotation?: string;
|
|
117
|
+
hasStyle?: string;
|
|
118
|
+
inNamespace?: string;
|
|
119
|
+
}): ClassDefinition[];
|
|
120
|
+
/** Get all subclasses of a class (direct inheritance) */
|
|
121
|
+
getSubclasses(classId: string): string[];
|
|
122
|
+
/** Get parent class (direct inheritance) */
|
|
123
|
+
getParentClass(classId: string): string | undefined;
|
|
124
|
+
/** Get all ancestors (transitive inheritance) */
|
|
125
|
+
getAncestors(classId: string): string[];
|
|
126
|
+
/** Get all descendants (transitive inheritance) */
|
|
127
|
+
getDescendants(classId: string): string[];
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingUR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