zombie-mermaid 2.2.6 → 3.0.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/dist/cli.js CHANGED
@@ -1,28 +1,26 @@
1
1
  #!/usr/bin/env node
2
- import { createRequire as ce } from "node:module";
3
- import { extname as le, format as de, parse as J } from "node:path";
4
- import { DEFAULTS as U, THEMES as G, applyClickStatement as ue, detectDiagramType as me, escapeXml as he, extractInitConfig as fe, isDirection as pe, isMonospaceFont as ge, isWideChar as ve, normalizeBrTags as k, parseStyleProps as Z, setMonospaceMetrics as we, splitStatements as be, splitStatementsByLine as ye, toDirection as B, tryApplyClassAssignment as Se, tryApplyClassDef as Ee, tryApplyStyleStatement as xe, withDirectionOverride as Q } from "@zombie-mermaid/core";
5
- import { access as ke, readFile as Me, writeFile as Ce } from "node:fs/promises";
6
- import { diagramColorsToAsciiTheme as Te, renderMermaidASCII as F } from "@zombie-mermaid/ascii-renderer";
7
- import { matchExpandedBlock as ee, parseClassDiagram as Le, parseErDiagram as Ae, parseExpandedMeta as te, parseSequenceDiagram as Ie, parseXYChart as Re, resolveShapeName as $e } from "@zombie-mermaid/mermaid-parser";
8
- import { layoutClassDiagramSync as ze, layoutErDiagramSync as De, layoutGraphSync as Be, layoutSequenceDiagram as Pe, layoutXYChart as _e, renderClassSvg as Ne, renderErSvg as Oe, renderSequenceSvg as Ge, renderSvg as Fe, renderXYChartSvg as Xe, resolveCssColors as We, resolveFontSizes as Ue } from "@zombie-mermaid/svg-renderer";
9
- import { decodeXML as Ye } from "entities";
10
- import { createServer as qe } from "node:http";
11
- import { StdioServerTransport as He } from "@modelcontextprotocol/sdk/server/stdio.js";
12
- import { createMcpServer as Ve } from "@zombie-mermaid/mcp";
13
- var je = 3e3;
14
- var ne = {
2
+ import { createRequire as U } from "node:module";
3
+ import { extname as V, format as H, parse as G } from "node:path";
4
+ import { THEMES as B, escapeXml as q, isDirection as j, isWideChar as K } from "@zombie-mermaid/core";
5
+ import { access as J, readFile as Z, writeFile as Q } from "node:fs/promises";
6
+ import { diagramColorsToAsciiTheme as ee, renderMermaidASCII as O } from "@zombie-mermaid/ascii-renderer";
7
+ import { renderMermaidSVG as z } from "@zombie-mermaid/svg-renderer";
8
+ import { createServer as te } from "node:http";
9
+ import { StdioServerTransport as ne } from "@modelcontextprotocol/sdk/server/stdio.js";
10
+ import { createMcpServer as re } from "@zombie-mermaid/mcp";
11
+ var oe = 3e3;
12
+ var W = {
15
13
  ".svg": "svg",
16
14
  ".txt": "ascii",
17
15
  ".html": "html",
18
16
  ".htm": "html",
19
17
  ".png": "png"
20
18
  };
21
- function Ke(e) {
19
+ function ie(e) {
22
20
  if (!(e === void 0 || e === "-"))
23
- return ne[le(e).toLowerCase()];
21
+ return W[V(e).toLowerCase()];
24
22
  }
25
- var C = {
23
+ var v = {
26
24
  svg: {
27
25
  flag: "--svg",
28
26
  ext: ".svg",
@@ -44,724 +42,200 @@ var C = {
44
42
  display: "PNG",
45
43
  label: "the PNG rasterization"
46
44
  }
47
- }, A = Object.keys(C);
48
- function Je(e, t) {
49
- const n = J(e);
50
- return de({
51
- dir: n.dir,
52
- name: n.name,
53
- ext: t
45
+ }, A = Object.keys(v);
46
+ function ae(e, n) {
47
+ const t = G(e);
48
+ return H({
49
+ dir: t.dir,
50
+ name: t.name,
51
+ ext: n
54
52
  });
55
53
  }
56
- function Ze(e) {
57
- const [t, ...n] = e;
58
- if (t === void 0) return { command: "help" };
59
- if (t === "--help" || t === "-h") return { command: "help" };
60
- if (t === "--version" || t === "-v") return { command: "version" };
61
- if (t === "themes") return { command: "themes" };
62
- if (t === "render") return tt(n);
63
- if (t === "web") return nt(n);
64
- if (t === "mcp") return { command: "mcp" };
65
- throw new Error(`Unknown command: ${t}`);
54
+ function se(e) {
55
+ const [n, ...t] = e;
56
+ if (n === void 0) return { command: "help" };
57
+ if (n === "--help" || n === "-h") return { command: "help" };
58
+ if (n === "--version" || n === "-v") return { command: "version" };
59
+ if (n === "themes") return { command: "themes" };
60
+ if (n === "render") return le(t);
61
+ if (n === "web") return me(t);
62
+ if (n === "mcp") return { command: "mcp" };
63
+ throw new Error(`Unknown command: ${n}`);
66
64
  }
67
- function z(e, t, n) {
68
- const r = e[t + 1];
69
- if (t + 1 >= e.length || r === void 0) throw new Error(`${n} requires a numeric value`);
70
- if (!/^\d+$/.test(r)) throw new Error(`${n} requires a non-negative integer, got: "${r}"`);
65
+ function P(e, n, t) {
66
+ const r = e[n + 1];
67
+ if (n + 1 >= e.length || r === void 0) throw new Error(`${t} requires a numeric value`);
68
+ if (!/^\d+$/.test(r)) throw new Error(`${t} requires a non-negative integer, got: "${r}"`);
71
69
  return Number(r);
72
70
  }
73
- function Qe(e, t, n) {
74
- const r = e[t + 1];
75
- if (t + 1 >= e.length || r === void 0) throw new Error(`${n} requires a value ("auto" or a positive integer)`);
71
+ function de(e, n, t) {
72
+ const r = e[n + 1];
73
+ if (n + 1 >= e.length || r === void 0) throw new Error(`${t} requires a value ("auto" or a positive integer)`);
76
74
  if (r === "auto") return "auto";
77
- const o = Number(r);
78
- if (!/^\d+$/.test(r) || o === 0 || !Number.isSafeInteger(o)) throw new Error(`${n} requires "auto" or a positive integer, got: "${r}"`);
79
- return o;
75
+ const s = Number(r);
76
+ if (!/^\d+$/.test(r) || s === 0 || !Number.isSafeInteger(s)) throw new Error(`${t} requires "auto" or a positive integer, got: "${r}"`);
77
+ return s;
80
78
  }
81
- function et(e, t, n) {
82
- const r = e[t + 1];
83
- if (t + 1 >= e.length || r === void 0) throw new Error(`${n} requires a value (one of TD, TB, BT, LR, RL)`);
84
- const o = r.toUpperCase();
85
- if (!pe(o)) throw new Error(`${n} requires one of TD, TB, BT, LR, RL, got: "${r}"`);
86
- return o;
79
+ function ce(e, n, t) {
80
+ const r = e[n + 1];
81
+ if (n + 1 >= e.length || r === void 0) throw new Error(`${t} requires a value (one of TD, TB, BT, LR, RL)`);
82
+ const s = r.toUpperCase();
83
+ if (!j(s)) throw new Error(`${t} requires one of TD, TB, BT, LR, RL, got: "${r}"`);
84
+ return s;
87
85
  }
88
- function tt(e) {
89
- let t, n = !1, r = !1, o = !1, l = !1, u = !1, a, v = !1, h, p, m, g, w = !1, d = !1, c, f, s = 0;
90
- for (; s < e.length; ) {
91
- const i = e[s];
92
- if (i === void 0) throw new Error(`parseArgs: index ${s} out of range while parsing arguments`);
93
- if (i === "--ascii")
94
- n = !0, s++;
95
- else if (i === "--svg")
96
- r = !0, s++;
97
- else if (i === "--resolve-colors")
98
- o = !0, s++;
99
- else if (i === "--html")
100
- l = !0, s++;
101
- else if (i === "--png")
102
- u = !0, s++;
103
- else if (i === "-o" || i === "--output") {
104
- if (s + 1 >= e.length) throw new Error("-o requires a file path (or - for stdout)");
105
- a = e[s + 1], s += 2;
106
- } else if (i === "-f" || i === "--force")
107
- v = !0, s++;
108
- else if (i === "--theme") {
109
- if (s + 1 >= e.length) throw new Error("--theme requires a theme name");
110
- h = e[s + 1], s += 2;
111
- } else if (i === "-x" || i === "--paddingX")
112
- p = z(e, s, i), s += 2;
113
- else if (i === "-y" || i === "--paddingY")
114
- m = z(e, s, i), s += 2;
115
- else if (i === "-p" || i === "--borderPadding")
116
- g = z(e, s, i), s += 2;
117
- else if (i === "--coords")
118
- w = !0, s++;
119
- else if (i === "--hyperlinks")
120
- d = !0, s++;
121
- else if (i === "-w" || i === "--max-width")
122
- c = Qe(e, s, i), s += 2;
123
- else if (i === "--direction")
124
- f = et(e, s, i), s += 2;
125
- else if (!i.startsWith("-") || i === "-") {
126
- if (i === "-") throw new Error("Unexpected argument: - (use -o - to write output to stdout; omit the input file to read stdin)");
127
- if (t !== void 0) throw new Error(`Unexpected argument: ${i} (input file already set to "${t}")`);
128
- t = i, s++;
129
- } else throw new Error(`Unknown flag: ${i}`);
130
- }
131
- const y = {
86
+ function le(e) {
87
+ let n, t = !1, r = !1, s = !1, m = !1, c = !1, d, E = !1, S, k, g, C, T = !1, b = !1, a, u, i = 0;
88
+ for (; i < e.length; ) {
89
+ const o = e[i];
90
+ if (o === void 0) throw new Error(`parseArgs: index ${i} out of range while parsing arguments`);
91
+ if (o === "--ascii")
92
+ t = !0, i++;
93
+ else if (o === "--svg")
94
+ r = !0, i++;
95
+ else if (o === "--resolve-colors")
96
+ s = !0, i++;
97
+ else if (o === "--html")
98
+ m = !0, i++;
99
+ else if (o === "--png")
100
+ c = !0, i++;
101
+ else if (o === "-o" || o === "--output") {
102
+ if (i + 1 >= e.length) throw new Error("-o requires a file path (or - for stdout)");
103
+ d = e[i + 1], i += 2;
104
+ } else if (o === "-f" || o === "--force")
105
+ E = !0, i++;
106
+ else if (o === "--theme") {
107
+ if (i + 1 >= e.length) throw new Error("--theme requires a theme name");
108
+ S = e[i + 1], i += 2;
109
+ } else if (o === "-x" || o === "--paddingX")
110
+ k = P(e, i, o), i += 2;
111
+ else if (o === "-y" || o === "--paddingY")
112
+ g = P(e, i, o), i += 2;
113
+ else if (o === "-p" || o === "--borderPadding")
114
+ C = P(e, i, o), i += 2;
115
+ else if (o === "--coords")
116
+ T = !0, i++;
117
+ else if (o === "--hyperlinks")
118
+ b = !0, i++;
119
+ else if (o === "-w" || o === "--max-width")
120
+ a = de(e, i, o), i += 2;
121
+ else if (o === "--direction")
122
+ u = ce(e, i, o), i += 2;
123
+ else if (!o.startsWith("-") || o === "-") {
124
+ if (o === "-") throw new Error("Unexpected argument: - (use -o - to write output to stdout; omit the input file to read stdin)");
125
+ if (n !== void 0) throw new Error(`Unexpected argument: ${o} (input file already set to "${n}")`);
126
+ n = o, i++;
127
+ } else throw new Error(`Unknown flag: ${o}`);
128
+ }
129
+ const h = {
132
130
  svg: r,
133
- html: l,
134
- png: u
135
- }, b = A.filter((i) => y[i]);
136
- if (b.length > 1) {
137
- const [i, E] = b;
138
- throw new Error(`${C[i].flag} and ${C[E].flag} cannot both be set — they would write different content to the same output path. Run the command twice with different -o paths for each.`);
139
- }
140
- const S = Ke(a);
141
- if (S !== void 0 && S !== "ascii") {
142
- const i = A.find((E) => E !== S && y[E]);
143
- if (i !== void 0) {
144
- const E = C[S], T = C[i];
145
- throw new Error(`-o ${a} has ${E.article} ${E.ext} extension, but ${T.flag} output would be written to it. Use a ${T.ext} path (or -o - for stdout) for ${T.label}.`);
131
+ html: m,
132
+ png: c
133
+ }, w = A.filter((o) => h[o]);
134
+ if (w.length > 1) {
135
+ const [o, l] = w;
136
+ throw new Error(`${v[o].flag} and ${v[l].flag} cannot both be set — they would write different content to the same output path. Run the command twice with different -o paths for each.`);
137
+ }
138
+ const p = ie(d);
139
+ if (p !== void 0 && p !== "ascii") {
140
+ const o = A.find((l) => l !== p && h[l]);
141
+ if (o !== void 0) {
142
+ const l = v[p], x = v[o];
143
+ throw new Error(`-o ${d} has ${l.article} ${l.ext} extension, but ${x.flag} output would be written to it. Use a ${x.ext} path (or -o - for stdout) for ${x.label}.`);
146
144
  }
147
- S === "svg" ? r = !0 : S === "html" ? l = !0 : u = !0;
148
- } else if (S === "ascii") {
149
- const i = A.find((E) => y[E]);
150
- if (i !== void 0) {
151
- const E = C[i];
152
- throw new Error(`-o ${a} has a .txt extension, but ${E.flag} output would be written to it. Use a ${E.ext} path (or -o - for stdout) instead.`);
145
+ p === "svg" ? r = !0 : p === "html" ? m = !0 : c = !0;
146
+ } else if (p === "ascii") {
147
+ const o = A.find((l) => h[l]);
148
+ if (o !== void 0) {
149
+ const l = v[o];
150
+ throw new Error(`-o ${d} has a .txt extension, but ${l.flag} output would be written to it. Use a ${l.ext} path (or -o - for stdout) instead.`);
153
151
  }
154
- n = !0;
152
+ t = !0;
155
153
  }
156
- if (!n && !r && !l && !u) {
157
- if (a !== void 0 && a !== "-") {
158
- const i = Object.keys(ne).join(", ");
159
- throw new Error(`Cannot infer an output format from "${a}" — pass --ascii, --svg, --html, or --png, or use a recognised extension (${i})`);
154
+ if (!t && !r && !m && !c) {
155
+ if (d !== void 0 && d !== "-") {
156
+ const o = Object.keys(W).join(", ");
157
+ throw new Error(`Cannot infer an output format from "${d}" — pass --ascii, --svg, --html, or --png, or use a recognised extension (${o})`);
160
158
  }
161
159
  throw new Error("Specify --ascii, --svg, --html, and/or --png (or -o <path> with a .svg/.txt/.html/.png extension)");
162
160
  }
163
- const M = {
161
+ const y = {
164
162
  svg: r,
165
- html: l,
166
- png: u
167
- }, x = A.find((i) => M[i]);
168
- if (a === "-" && n && x !== void 0) {
169
- const i = C[x].display;
170
- throw new Error(`-o - would send both ASCII and ${i} to stdout; drop --ascii, or write the ${i} to a file path`);
171
- }
172
- if (x !== void 0 && a === void 0) {
173
- const { flag: i, ext: E } = C[x];
174
- if (t === void 0) throw new Error(`${i} needs -o <path> (or -o - for stdout) when reading from stdin — there is no input file name to derive an output name from`);
175
- a = Je(t, E);
176
- }
177
- if (c !== void 0 && !n) throw new Error("-w/--max-width requires --ascii");
178
- if (d && !n) throw new Error("--hyperlinks requires --ascii");
179
- if (o && !r) throw new Error("--resolve-colors requires --svg");
163
+ html: m,
164
+ png: c
165
+ }, f = A.find((o) => y[o]);
166
+ if (d === "-" && t && f !== void 0) {
167
+ const o = v[f].display;
168
+ throw new Error(`-o - would send both ASCII and ${o} to stdout; drop --ascii, or write the ${o} to a file path`);
169
+ }
170
+ if (f !== void 0 && d === void 0) {
171
+ const { flag: o, ext: l } = v[f];
172
+ if (n === void 0) throw new Error(`${o} needs -o <path> (or -o - for stdout) when reading from stdin — there is no input file name to derive an output name from`);
173
+ d = ae(n, l);
174
+ }
175
+ if (a !== void 0 && !t) throw new Error("-w/--max-width requires --ascii");
176
+ if (b && !t) throw new Error("--hyperlinks requires --ascii");
177
+ if (s && !r) throw new Error("--resolve-colors requires --svg");
180
178
  return {
181
179
  command: "render",
182
- input: t,
183
- ascii: n,
180
+ input: n,
181
+ ascii: t,
184
182
  svg: r,
185
- resolveColors: o,
186
- html: l,
187
- png: u,
188
- output: a,
189
- force: v,
190
- theme: h,
191
- paddingX: p,
192
- paddingY: m,
193
- borderPadding: g,
194
- coords: w,
195
- hyperlinks: d,
196
- maxWidth: c,
197
- direction: f
183
+ resolveColors: s,
184
+ html: m,
185
+ png: c,
186
+ output: d,
187
+ force: E,
188
+ theme: S,
189
+ paddingX: k,
190
+ paddingY: g,
191
+ borderPadding: C,
192
+ coords: T,
193
+ hyperlinks: b,
194
+ maxWidth: a,
195
+ direction: u
198
196
  };
199
197
  }
200
- function nt(e) {
201
- let t = je, n = 0;
202
- for (; n < e.length; ) {
203
- const r = e[n];
204
- if (r === void 0) throw new Error(`parseArgs: index ${n} out of range while parsing arguments`);
198
+ function me(e) {
199
+ let n = oe, t = 0;
200
+ for (; t < e.length; ) {
201
+ const r = e[t];
202
+ if (r === void 0) throw new Error(`parseArgs: index ${t} out of range while parsing arguments`);
205
203
  if (r === "--port")
206
- t = z(e, n, r), n += 2;
204
+ n = P(e, t, r), t += 2;
207
205
  else throw new Error(`Unknown flag: ${r}`);
208
206
  }
209
207
  return {
210
208
  command: "web",
211
- port: t
209
+ port: n
212
210
  };
213
211
  }
214
- var rt = /\p{Mn}|\p{Me}/u;
215
- function ot(e) {
216
- return rt.test(e);
212
+ var ue = /\p{Mn}|\p{Me}/u;
213
+ function pe(e) {
214
+ return ue.test(e);
217
215
  }
218
- var it = new Intl.Segmenter(void 0, { granularity: "grapheme" }), at = "️";
219
- function st(e) {
220
- const t = [];
221
- for (const { segment: n } of it.segment(e)) t.push(n);
222
- return t;
216
+ var he = new Intl.Segmenter(void 0, { granularity: "grapheme" }), fe = "️";
217
+ function ge(e) {
218
+ const n = [];
219
+ for (const { segment: t } of he.segment(e)) n.push(t);
220
+ return n;
223
221
  }
224
- function ct(e) {
225
- if (e.includes(at)) return 2;
226
- let t = !1, n = !1;
222
+ function ve(e) {
223
+ if (e.includes(fe)) return 2;
224
+ let n = !1, t = !1;
227
225
  for (const r of e)
228
- ve(r) && (t = !0), ot(r) || (n = !0);
229
- return t ? 2 : n ? 1 : 0;
230
- }
231
- function lt(e) {
232
- let t = 0;
233
- for (const n of st(e)) t += ct(n);
234
- return t;
235
- }
236
- var dt = /\x1b\]8;[^\x07\x1b]*(?:\x1b\\|\x07)/g;
237
- function ut(e) {
238
- return e.replace(dt, "");
239
- }
240
- function Y(e) {
241
- const t = e.trim();
242
- return t.length >= 2 && (t[0] === '"' && t[t.length - 1] === '"' || t[0] === "'" && t[t.length - 1] === "'") ? t.slice(1, -1) : t;
243
- }
244
- var mt = '"graph <dir>"/"flowchart <dir>" (dir: TD, TB, LR, BT, RL), "stateDiagram-v2", "sequenceDiagram", "classDiagram", "erDiagram", "xychart-beta"';
245
- function ht(e) {
246
- const t = e.trim().toLowerCase();
247
- if (/^sequence/.test(t)) return "sequenceDiagram";
248
- if (/^class/.test(t)) return "classDiagram";
249
- if (/^er/.test(t)) return "erDiagram";
250
- if (/^xychart/.test(t)) return "xychart-beta";
251
- if (/^state/.test(t)) return "stateDiagram-v2";
252
- }
253
- var ft = /^(?:[\w-]+@)?<?(?:-{2,}|={2,}|-\.+-?|~{3,})/;
254
- function pt(e) {
255
- const t = [];
256
- for (const n of e) n.forEach((r, o) => {
257
- if (o === 0 && t.length > 0 && ft.test(r.text)) {
258
- const l = t[t.length - 1];
259
- t[t.length - 1] = {
260
- text: `${l.text} ${r.text}`,
261
- line: l.line
262
- };
263
- } else t.push(r);
264
- });
265
- return t;
266
- }
267
- function gt(e) {
268
- const t = fe(e.split(`
269
- `).map((l) => l.trim())), n = pt(ye(e));
270
- if (n.length === 0) throw new Error("Empty mermaid diagram");
271
- const r = n[0], o = /^stateDiagram(-v2)?\s*$/i.test(r.text) ? wt(n) : vt(n);
272
- return o.initConfig = t, o;
273
- }
274
- function vt(e) {
275
- const t = e[0];
276
- if (t === void 0)
277
- throw new Error("parseFlowchart called with no lines");
278
- const n = t.text, r = n.match(/^(?:graph|flowchart)\s+(TD|TB|LR|BT|RL)\s*$/i);
279
- if (!r) {
280
- const u = n.match(/^(?:graph|flowchart)\b\s*(.*)$/i);
281
- if (u) {
282
- const h = u[1].trim();
283
- throw new Error(h.length > 0 ? `Line ${t.line}: Invalid direction "${h}" in header "${n}". Expected one of: TD, TB, LR, BT, RL.` : `Line ${t.line}: Missing direction in header "${n}". Expected e.g. "graph TD" or "flowchart LR" — one of: TD, TB, LR, BT, RL.`);
284
- }
285
- const a = ht(n), v = a && a.toLowerCase() !== n.trim().toLowerCase() ? ` Did you mean "${a}"?` : "";
286
- throw new Error(`Line ${t.line}: Invalid mermaid header: "${n}".${v} Supported headers: ${mt}.`);
287
- }
288
- const o = {
289
- direction: B(r[1]),
290
- nodes: /* @__PURE__ */ new Map(),
291
- edges: [],
292
- subgraphs: [],
293
- classDefs: /* @__PURE__ */ new Map(),
294
- classAssignments: /* @__PURE__ */ new Map(),
295
- nodeStyles: /* @__PURE__ */ new Map(),
296
- linkStyles: /* @__PURE__ */ new Map(),
297
- interactions: /* @__PURE__ */ new Map()
298
- }, l = [];
299
- for (let u = 1; u < e.length; u++) {
300
- const a = e[u].text;
301
- if (Ee(a, o) || Se(a, o) || xe(a, o)) continue;
302
- if (/^click\s+/i.test(a)) {
303
- Tt(a, o);
304
- continue;
305
- }
306
- if (re(a, o)) continue;
307
- const v = a.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);
308
- if (v) {
309
- const m = v[1].trim(), g = Z(v[2]);
310
- if (m === "default") o.linkStyles.set("default", {
311
- ...o.linkStyles.get("default"),
312
- ...g
313
- });
314
- else {
315
- const w = m.split(",").map((d) => parseInt(d.trim(), 10));
316
- for (const d of w) isNaN(d) || o.linkStyles.set(d, {
317
- ...o.linkStyles.get(d),
318
- ...g
319
- });
320
- }
321
- continue;
322
- }
323
- const h = a.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);
324
- if (h && l.length > 0) {
325
- l[l.length - 1].direction = B(h[1]);
326
- continue;
327
- }
328
- const p = a.match(/^subgraph\s+(.+)$/);
329
- if (p) {
330
- const m = p[1].trim(), g = m.match(/^([^\s[]+)\s*\[(.+)\]$/);
331
- let w, d;
332
- g ? (w = g[1], d = k(Y(g[2]))) : (d = k(Y(m)), w = d.replace(/\s+/g, "_").replace(/[^\p{L}\p{N}_-]/gu, ""));
333
- const c = {
334
- id: w,
335
- label: d,
336
- nodeIds: [],
337
- children: []
338
- };
339
- l.push(c);
340
- continue;
341
- }
342
- if (a === "end") {
343
- const m = l.pop();
344
- m && (l.length > 0 ? l[l.length - 1].children.push(m) : o.subgraphs.push(m));
345
- continue;
346
- }
347
- $t(a, o, l);
348
- }
349
- return o;
226
+ K(r) && (n = !0), pe(r) || (t = !0);
227
+ return n ? 2 : t ? 1 : 0;
350
228
  }
351
- function wt(e) {
352
- const t = {
353
- direction: "TD",
354
- nodes: /* @__PURE__ */ new Map(),
355
- edges: [],
356
- subgraphs: [],
357
- classDefs: /* @__PURE__ */ new Map(),
358
- classAssignments: /* @__PURE__ */ new Map(),
359
- nodeStyles: /* @__PURE__ */ new Map(),
360
- linkStyles: /* @__PURE__ */ new Map(),
361
- interactions: /* @__PURE__ */ new Map()
362
- }, n = [], r = /* @__PURE__ */ new Set();
363
- let o = 0, l = 0;
364
- for (let u = 1; u < e.length; u++) {
365
- const a = e[u].text, v = a.match(/^direction\s+(TD|TB|LR|BT|RL)\s*$/i);
366
- if (v) {
367
- n.length > 0 ? n[n.length - 1].direction = B(v[1]) : t.direction = B(v[1]);
368
- continue;
369
- }
370
- const h = a.match(/^linkStyle\s+(default|[\d,\s]+)\s+(.+)$/);
371
- if (h) {
372
- const d = h[1].trim(), c = Z(h[2]);
373
- if (d === "default") t.linkStyles.set("default", {
374
- ...t.linkStyles.get("default"),
375
- ...c
376
- });
377
- else {
378
- const f = d.split(",").map((s) => parseInt(s.trim(), 10));
379
- for (const s of f) isNaN(s) || t.linkStyles.set(s, {
380
- ...t.linkStyles.get(s),
381
- ...c
382
- });
383
- }
384
- continue;
385
- }
386
- if (re(a, t)) continue;
387
- const p = a.match(/^state\s+(?:"([^"]+)"\s+as\s+)?([\w\p{L}]+)\s*\{$/u);
388
- if (p) {
389
- const d = p[1] ?? p[2], c = p[2], f = {
390
- id: c,
391
- label: d,
392
- nodeIds: [],
393
- children: []
394
- };
395
- n.push(f), r.add(c), t.nodes.delete(c);
396
- continue;
397
- }
398
- if (a === "}") {
399
- const d = n.pop();
400
- d && (n.length > 0 ? n[n.length - 1].children.push(d) : t.subgraphs.push(d));
401
- continue;
402
- }
403
- const m = a.match(/^state\s+"([^"]+)"\s+as\s+([\w\p{L}]+)\s*$/u);
404
- if (m) {
405
- const d = k(m[1]), c = m[2];
406
- L(t, n, {
407
- id: c,
408
- label: d,
409
- shape: "rounded"
410
- });
411
- continue;
412
- }
413
- const g = a.match(/^(\[\*\]|[\w\p{L}-]+)\s*(?:([\w-]+)@)?-->\s*(\[\*\]|[\w\p{L}-]+)(?:\s*:\s*(.+))?$/u);
414
- if (g) {
415
- let d = g[1];
416
- const c = g[2];
417
- let f = g[3];
418
- const s = g[4]?.trim(), y = s ? k(s) : void 0;
419
- d === "[*]" ? (o++, d = `_start${o > 1 ? o : ""}`, L(t, n, {
420
- id: d,
421
- label: "",
422
- shape: "state-start"
423
- })) : r.has(d) || q(t, n, d), f === "[*]" ? (l++, f = `_end${l > 1 ? l : ""}`, L(t, n, {
424
- id: f,
425
- label: "",
426
- shape: "state-end"
427
- })) : r.has(f) || q(t, n, f), t.edges.push({
428
- source: d,
429
- target: f,
430
- label: y,
431
- style: "solid",
432
- hasArrowStart: !1,
433
- hasArrowEnd: !0,
434
- ...c !== void 0 ? { id: c } : {}
435
- });
436
- continue;
437
- }
438
- const w = a.match(/^([\w\p{L}-]+)\s*:\s*(.+)$/u);
439
- if (w) {
440
- const d = w[1];
441
- L(t, n, {
442
- id: d,
443
- label: k(w[2].trim()),
444
- shape: "rounded"
445
- });
446
- continue;
447
- }
448
- }
449
- return t;
450
- }
451
- function L(e, t, n) {
452
- if (e.nodes.has(n.id) || e.nodes.set(n.id, n), t.length > 0) {
453
- const r = t[t.length - 1];
454
- r.nodeIds.includes(n.id) || r.nodeIds.push(n.id);
455
- }
456
- }
457
- function q(e, t, n) {
458
- if (!e.nodes.has(n)) L(e, t, {
459
- id: n,
460
- label: n,
461
- shape: "rounded"
462
- });
463
- else if (t.length > 0) {
464
- const r = t[t.length - 1];
465
- r.nodeIds.includes(n) || r.nodeIds.push(n);
466
- }
229
+ function we(e) {
230
+ let n = 0;
231
+ for (const t of ge(e)) n += ve(t);
232
+ return n;
467
233
  }
468
- var bt = /^(<|o|x)?(-{2,}|={2,}|-\.+-|~{3,})(>|o|x)?(?:\|([^|]*)\|)?/, yt = /* @__PURE__ */ new Set(["--", "=="]);
469
- function I(e) {
470
- if (e === "o") return "circle";
471
- if (e === "x") return "cross";
234
+ var be = /\x1b\]8;[^\x07\x1b]*(?:\x1b\\|\x07)/g;
235
+ function ye(e) {
236
+ return e.replace(be, "");
472
237
  }
473
- var St = /^(<|o|x)?(--|-\.|==)\s+(.+?)\s+(-{2,}[>ox]|={2,}[>ox]|\.+-[>ox]|-{3,}|={3,}|-\.+-)/, Et = [
474
- {
475
- regex: /^([\w\p{L}-]+)\(\(\(((?:"[^"]*"|(?!\)\)\)).)+)\)\)\)/u,
476
- shape: "doublecircle"
477
- },
478
- {
479
- regex: /^([\w\p{L}-]+)\(\[((?:"[^"]*"|(?!\]\)).)+)\]\)/u,
480
- shape: "stadium"
481
- },
482
- {
483
- regex: /^([\w\p{L}-]+)\(\(((?:"[^"]*"|(?!\)\)).)+)\)\)/u,
484
- shape: "circle"
485
- },
486
- {
487
- regex: /^([\w\p{L}-]+)\[\[((?:"[^"]*"|(?!\]\]).)+)\]\]/u,
488
- shape: "subroutine"
489
- },
490
- {
491
- regex: /^([\w\p{L}-]+)\[\(((?:"[^"]*"|(?!\)\]).)+)\)\]/u,
492
- shape: "cylinder"
493
- },
494
- {
495
- regex: /^([\w\p{L}-]+)\[\/((?:"[^"]*"|(?![\\/]\]).)+)([\\/])\]/u,
496
- shape: (e) => e === "\\" ? "trapezoid" : "parallelogram"
497
- },
498
- {
499
- regex: /^([\w\p{L}-]+)\[\\((?:"[^"]*"|(?![\\/]\]).)+)([\\/])\]/u,
500
- shape: (e) => e === "/" ? "trapezoid-alt" : "parallelogram-alt"
501
- },
502
- {
503
- regex: /^([\w\p{L}-]+)>((?:"[^"]*"|(?!\]).)+)\]/u,
504
- shape: "asymmetric"
505
- },
506
- {
507
- regex: /^([\w\p{L}-]+)\{\{((?:"[^"]*"|(?!\}\}).)+)\}\}/u,
508
- shape: "hexagon"
509
- },
510
- {
511
- regex: /^([\w\p{L}-]+)\[((?:"[^"]*"|(?!\]).)+)\]/u,
512
- shape: "rectangle"
513
- },
514
- {
515
- regex: /^([\w\p{L}-]+)\(((?:"[^"]*"|(?!\)).)+)\)/u,
516
- shape: "rounded"
517
- },
518
- {
519
- regex: /^([\w\p{L}-]+)\{((?:"[^"]*"|(?!\}).)+)\}/u,
520
- shape: "diamond"
521
- }
522
- ], xt = /^([\w\p{L}]+(?:-[\w\p{L}]+)*)/u, kt = /^([\w\p{L}-]+)(?=@\{)/u;
523
- function Mt(e) {
524
- if (e.shape) return $e(e.shape) ?? "rectangle";
525
- if (e.icon !== void 0 || e.img !== void 0) switch (e.form?.toLowerCase()) {
526
- case "circle":
527
- return "circle";
528
- case "rounded":
529
- return "rounded";
530
- default:
531
- return "rectangle";
532
- }
533
- return "rectangle";
534
- }
535
- function Ct(e, t) {
536
- return t.label !== void 0 && t.label.length > 0 ? t.label : t.icon ? t.icon : t.img ? t.img : e;
537
- }
538
- function Tt(e, t) {
539
- ue(e, t.interactions);
540
- }
541
- var Lt = /^([\w-]+)(?=@\{)/;
542
- function re(e, t) {
543
- const n = e.match(Lt);
544
- if (!n || !t.edges.some((o) => o.id === n[1])) return !1;
545
- const r = ee(e.slice(n[1].length));
546
- return r ? (At(t, n[1], te(r.body)), !0) : !1;
547
- }
548
- function At(e, t, n) {
549
- for (const r of e.edges)
550
- r.id === t && (n.animate !== void 0 && (r.animate = n.animate.toLowerCase() !== "false"), n.animation !== void 0 && (r.animate = !0));
551
- }
552
- var It = /^:::([\w][\w-]*)/, Rt = /^([\w\p{L}-]+):::([\w][\w-]*)/u;
553
- function $t(e, t, n) {
554
- let r = e.trim();
555
- const o = H(r, t, n);
556
- if (!o || o.ids.length === 0) return;
557
- r = o.remaining.trim();
558
- let l = o.ids;
559
- for (; r.length > 0; ) {
560
- let u, a, v, h, p, m, g;
561
- const w = r.match(/^([\w-]+)@(?=[-=<~ox])/);
562
- w && (g = w[1], r = r.slice(w[0].length));
563
- const d = r.match(bt), c = d?.[2], f = d?.[1], s = d?.[3];
564
- if (d && c !== void 0 && !(f === void 0 && s === void 0 && yt.has(c))) {
565
- u = f !== void 0, p = I(f);
566
- const b = d[4]?.trim();
567
- h = b ? k(b) : void 0, r = r.slice(d[0].length).trim(), a = Dt(c), v = s !== void 0, m = I(s);
568
- } else {
569
- const b = r.match(St);
570
- if (!b) break;
571
- u = !!b[1], p = I(b[1]);
572
- const S = b[3].trim();
573
- h = S ? k(S) : void 0;
574
- const M = b[2], x = b[4];
575
- r = r.slice(b[0].length).trim(), a = Bt(M, x), m = I(x.slice(-1)), v = x.endsWith(">") || m !== void 0;
576
- }
577
- const y = H(r, t, n);
578
- if (!y || y.ids.length === 0) break;
579
- r = y.remaining.trim();
580
- for (const b of l) for (const S of y.ids) t.edges.push({
581
- source: b,
582
- target: S,
583
- label: h,
584
- style: a,
585
- hasArrowStart: u,
586
- hasArrowEnd: v,
587
- ...p !== void 0 ? { startMarker: p } : {},
588
- ...m !== void 0 ? { endMarker: m } : {},
589
- ...g !== void 0 ? { id: g } : {}
590
- });
591
- l = y.ids;
592
- }
593
- }
594
- function H(e, t, n) {
595
- const r = V(e, t, n);
596
- if (!r) return null;
597
- const o = [r.id];
598
- let l = r.remaining.trim();
599
- for (; l.startsWith("&"); ) {
600
- l = l.slice(1).trim();
601
- const u = V(l, t, n);
602
- if (!u) break;
603
- o.push(u.id), l = u.remaining.trim();
604
- }
605
- return {
606
- ids: o,
607
- remaining: l
608
- };
609
- }
610
- function V(e, t, n) {
611
- let r = null, o = e, l;
612
- const u = o.match(Rt);
613
- u && (l = u[2], o = u[1] + o.slice(u[0].length));
614
- const a = o.match(kt);
615
- if (a) {
616
- const h = a[1], p = ee(o.slice(h.length));
617
- if (p) {
618
- const m = te(p.body);
619
- P(t, n, {
620
- id: h,
621
- label: k(Ct(h, m)),
622
- shape: Mt(m)
623
- }), r = h, o = o.slice(h.length + p.length);
624
- }
625
- }
626
- if (r === null) for (const { regex: h, shape: p } of Et) {
627
- const m = o.match(h);
628
- if (m) {
629
- r = m[1];
630
- const g = k(m[2]), w = typeof p == "function" ? p(m[3] ?? "") : p;
631
- P(t, n, {
632
- id: r,
633
- label: g,
634
- shape: w
635
- }), o = o.slice(m[0].length);
636
- break;
637
- }
638
- }
639
- if (r === null) {
640
- const h = o.match(xt);
641
- h && (r = h[1], t.nodes.has(r) || P(t, n, {
642
- id: r,
643
- label: r,
644
- shape: "rectangle"
645
- }), o = o.slice(h[0].length));
646
- }
647
- if (r === null) return null;
648
- l && t.classAssignments.set(r, l);
649
- const v = o.match(It);
650
- return v && (t.classAssignments.set(r, v[1]), o = o.slice(v[0].length)), {
651
- id: r,
652
- remaining: o
653
- };
654
- }
655
- function P(e, t, n) {
656
- e.nodes.has(n.id) || e.nodes.set(n.id, n), zt(t, n.id);
657
- }
658
- function zt(e, t) {
659
- if (e.length > 0) {
660
- const n = e[e.length - 1];
661
- n.nodeIds.includes(t) || n.nodeIds.push(t);
662
- }
663
- }
664
- function Dt(e) {
665
- return e.startsWith("~") ? "invisible" : e.includes(".") ? "dotted" : e.startsWith("=") ? "thick" : "solid";
666
- }
667
- function Bt(e, t) {
668
- return e.includes(".") || t.includes(".") ? "dotted" : e.startsWith("=") || t.startsWith("=") ? "thick" : "solid";
669
- }
670
- var Pt = {
671
- type: "xychart",
672
- parse: Re,
673
- layoutForSvg: _e,
674
- renderSvg(e, t, n) {
675
- const r = n.interactivity !== void 0 ? n.interactivity === "full" : n.interactive ?? !1;
676
- return Xe(e, t.colors, t.font, t.transparent, r, t.embedSource, t.title, t.decorative, t.emit);
677
- }
678
- }, _t = {
679
- type: "sequence",
680
- parse: Ie,
681
- layoutForSvg: Pe,
682
- renderSvg(e, t) {
683
- return Ge(e, t.colors, t.font, t.transparent, t.fontSizes, t.embedSource, t.title, t.decorative, t.emit);
684
- }
685
- };
686
- function oe(e) {
687
- return (e.interactivity ?? "static") !== "none";
688
- }
689
- function Nt(e) {
690
- return (e.interactivity ?? "static") === "full";
691
- }
692
- var Ot = {
693
- type: "class",
694
- parse: Le,
695
- layoutForSvg: ze,
696
- renderSvg(e, t, n) {
697
- return Ne(e, t.colors, t.font, t.transparent, t.fontSizes, t.embedSource, t.title, t.decorative, oe(n), t.emit);
698
- }
699
- }, Gt = {
700
- type: "er",
701
- parse: Ae,
702
- layoutForSvg(e, t) {
703
- return De(Q(e, t.direction), t);
704
- },
705
- renderSvg(e, t) {
706
- return Oe(e, t.colors, t.font, t.transparent, t.fontSizes, t.embedSource, t.title, t.decorative, t.emit);
707
- }
708
- }, Ft = {
709
- type: "flowchart",
710
- parse: (e, t) => gt(t),
711
- layoutForSvg(e, t) {
712
- const n = Q(e, t.direction);
713
- return {
714
- graph: Be(n, t),
715
- curve: t.curve ?? e.initConfig?.curve ?? "linear"
716
- };
717
- },
718
- renderSvg(e, t, n) {
719
- return Fe(e.graph, t.colors, t.font, t.transparent, t.fontSizes, e.curve, t.embedSource, Nt(n), oe(n), t.title, t.decorative, t.emit);
720
- }
721
- }, Xt = {
722
- xychart: Pt,
723
- er: Gt,
724
- sequence: _t,
725
- class: Ot,
726
- flowchart: Ft
727
- };
728
- function ie(e) {
729
- return {
730
- bg: e.bg ?? U.bg,
731
- fg: e.fg ?? U.fg,
732
- line: e.line,
733
- accent: e.accent,
734
- muted: e.muted,
735
- surface: e.surface,
736
- border: e.border
737
- };
738
- }
739
- function Wt(e) {
740
- return {
741
- nonce: e.nonce,
742
- styleAttribute: e.styleAttribute
743
- };
744
- }
745
- function D(e, t = {}) {
746
- const n = Ut(e, t);
747
- return t.resolveColors ? We(n, ie(t)) : n;
748
- }
749
- function Ut(e, t) {
750
- const n = Ye(e), r = ie(t), o = t.font ?? "Inter";
751
- we(ge(o));
752
- const l = t.transparent ?? !1, u = Ue(t.fontSizes), a = me(n), v = t.embedSource ? e : void 0, h = t.title, p = t.decorative, m = Wt(t), g = be(n), w = Xt[a], d = w.parse(g, n), c = w.layoutForSvg(d, t), f = {
753
- colors: r,
754
- font: o,
755
- transparent: l,
756
- fontSizes: u,
757
- embedSource: v,
758
- title: h,
759
- decorative: p,
760
- emit: m
761
- };
762
- return w.renderSvg(c, f, t);
763
- }
764
- var Yt = `// @ts-check
238
+ var xe = `// @ts-check
765
239
  /// <reference lib="dom" />
766
240
  // ============================================================================
767
241
  // Client script for the CLI's self-contained HTML viewer (\`render --html\`).
@@ -1053,7 +527,7 @@ var Yt = `// @ts-check
1053
527
  fit()
1054
528
  stage.focus({ preventScroll: true })
1055
529
  })()
1056
- `, qt = `
530
+ `, Ee = `
1057
531
  :root {
1058
532
  color-scheme: light dark;
1059
533
  --bg: #f4f4f5;
@@ -1160,16 +634,16 @@ var Yt = `// @ts-check
1160
634
  #diagram { position: static; transform: none !important; }
1161
635
  }
1162
636
  `.trim();
1163
- function Ht(e) {
1164
- const t = e.title?.trim() || "Mermaid diagram";
637
+ function Se(e) {
638
+ const n = e.title?.trim() || "Mermaid diagram";
1165
639
  return `<!doctype html>
1166
640
  <html lang="en">
1167
641
  <head>
1168
642
  <meta charset="utf-8" />
1169
643
  <meta name="viewport" content="width=device-width, initial-scale=1" />
1170
- <title>${he(t)}</title>
644
+ <title>${q(n)}</title>
1171
645
  <style>
1172
- ${qt}
646
+ ${Ee}
1173
647
  </style>
1174
648
  </head>
1175
649
  <body>
@@ -1187,206 +661,206 @@ ${e.svg}
1187
661
  <button id="theme" type="button" aria-pressed="false" aria-label="Switch to dark mode (T)">Dark</button>
1188
662
  </div>
1189
663
  <script>
1190
- ${Yt}
664
+ ${xe}
1191
665
  <\/script>
1192
666
  </body>
1193
667
  </html>
1194
668
  `;
1195
669
  }
1196
- async function Vt(e) {
1197
- let t;
670
+ async function ke(e) {
671
+ let n;
1198
672
  try {
1199
- t = await import("@resvg/resvg-js");
1200
- } catch (n) {
1201
- const r = n instanceof Error ? n.message : String(n);
673
+ n = await import("@resvg/resvg-js");
674
+ } catch (t) {
675
+ const r = t instanceof Error ? t.message : String(t);
1202
676
  throw new Error(`--png requires the optional dependency "@resvg/resvg-js", which is not installed (it may have failed to build for this platform, or the install ran with --no-optional). Install it with \`pnpm add @resvg/resvg-js\` (or the npm/yarn equivalent) and try again.
1203
677
  ${r}`);
1204
678
  }
1205
- return (await t.renderAsync(e)).asPng();
679
+ return (await n.renderAsync(e)).asPng();
1206
680
  }
1207
- var jt = 80, _ = 1, N = 1, O = 0, Kt = /\x1b\[[0-9;]*m/g;
1208
- function j(e) {
681
+ var Ce = 80, $ = 1, L = 1, R = 0, Te = /\x1b\[[0-9;]*m/g;
682
+ function D(e) {
1209
683
  return e.split(`
1210
- `).reduce((t, n) => Math.max(t, lt(ut(n.replace(Kt, "")))), 0);
684
+ `).reduce((n, t) => Math.max(n, we(ye(t.replace(Te, "")))), 0);
1211
685
  }
1212
- function Jt(e) {
1213
- return e !== "auto" ? e : process.stdout.columns ?? jt;
686
+ function Ae(e) {
687
+ return e !== "auto" ? e : process.stdout.columns ?? Ce;
1214
688
  }
1215
- async function Zt(e, t, n, r) {
1216
- const o = t ?? process.stdout, l = r ?? process.stderr;
1217
- let u;
1218
- if (e.input !== void 0) u = await Me(e.input, "utf-8");
1219
- else if (n !== void 0) u = n;
689
+ async function Ie(e, n, t, r) {
690
+ const s = n ?? process.stdout, m = r ?? process.stderr;
691
+ let c;
692
+ if (e.input !== void 0) c = await Z(e.input, "utf-8");
693
+ else if (t !== void 0) c = t;
1220
694
  else {
1221
695
  if (process.stdin.isTTY) throw new Error("No input file specified and stdin is a terminal. Pipe a diagram or pass a file path.");
1222
- u = await tn();
696
+ c = await Pe();
1223
697
  }
1224
- if (u = u.trim(), u.length === 0) throw new Error("Empty input — provide a Mermaid diagram via file or stdin");
1225
- let a;
1226
- if (e.theme !== void 0 && (a = G[e.theme], a === void 0)) {
1227
- const c = Object.keys(G).join(", ");
1228
- throw new Error(`Unknown theme: "${e.theme}". Available themes: ${c}`);
698
+ if (c = c.trim(), c.length === 0) throw new Error("Empty input — provide a Mermaid diagram via file or stdin");
699
+ let d;
700
+ if (e.theme !== void 0 && (d = B[e.theme], d === void 0)) {
701
+ const a = Object.keys(B).join(", ");
702
+ throw new Error(`Unknown theme: "${e.theme}". Available themes: ${a}`);
1229
703
  }
1230
- const v = e.svg && e.output === "-", h = e.html && e.output === "-", p = e.png && e.output === "-", m = e.ascii && !e.svg && !e.html && !e.png && e.output !== void 0 && e.output !== "-" ? e.output : void 0, g = e.svg && !v ? e.output : void 0, w = e.html && !h ? e.output : void 0, d = e.png && !p ? e.output : void 0;
1231
- for (const c of [
1232
- m,
704
+ const E = e.svg && e.output === "-", S = e.html && e.output === "-", k = e.png && e.output === "-", g = e.ascii && !e.svg && !e.html && !e.png && e.output !== void 0 && e.output !== "-" ? e.output : void 0, C = e.svg && !E ? e.output : void 0, T = e.html && !S ? e.output : void 0, b = e.png && !k ? e.output : void 0;
705
+ for (const a of [
1233
706
  g,
1234
- w,
1235
- d
1236
- ]) c !== void 0 && await Qt(c, e.force);
707
+ C,
708
+ T,
709
+ b
710
+ ]) a !== void 0 && await Me(a, e.force);
1237
711
  if (e.ascii) {
1238
- const c = a && m === void 0 ? {
712
+ const a = d && g === void 0 ? {
1239
713
  colorMode: "auto",
1240
- theme: Te(a)
714
+ theme: ee(d)
1241
715
  } : { colorMode: "none" };
1242
- e.paddingX !== void 0 && (c.paddingX = e.paddingX), e.paddingY !== void 0 && (c.paddingY = e.paddingY), e.borderPadding !== void 0 && (c.boxBorderPadding = e.borderPadding), e.coords && (c.showCoords = !0), e.direction !== void 0 && (c.direction = e.direction), e.hyperlinks && m === void 0 && (c.hyperlinks = !0);
1243
- let f = F(u, c);
716
+ e.paddingX !== void 0 && (a.paddingX = e.paddingX), e.paddingY !== void 0 && (a.paddingY = e.paddingY), e.borderPadding !== void 0 && (a.boxBorderPadding = e.borderPadding), e.coords && (a.showCoords = !0), e.direction !== void 0 && (a.direction = e.direction), e.hyperlinks && g === void 0 && (a.hyperlinks = !0);
717
+ let u = O(c, a);
1244
718
  if (e.maxWidth !== void 0) {
1245
- const s = Jt(e.maxWidth);
1246
- let y = j(f), b = !1;
1247
- if (y > s) {
1248
- const S = c.paddingX ?? 5, M = c.paddingY ?? 5, x = c.boxBorderPadding ?? 1, i = Math.min(S, _), E = Math.min(M, N), T = Math.min(x, O);
1249
- if (i < S || E < M || T < x) {
1250
- const X = F(u, {
1251
- ...c,
1252
- paddingX: i,
1253
- paddingY: E,
1254
- boxBorderPadding: T
1255
- }), W = j(X);
1256
- W < y && (f = X, y = W, b = !0);
719
+ const i = Ae(e.maxWidth);
720
+ let h = D(u), w = !1;
721
+ if (h > i) {
722
+ const p = a.paddingX ?? 5, y = a.paddingY ?? 5, f = a.boxBorderPadding ?? 1, o = Math.min(p, $), l = Math.min(y, L), x = Math.min(f, R);
723
+ if (o < p || l < y || x < f) {
724
+ const _ = O(c, {
725
+ ...a,
726
+ paddingX: o,
727
+ paddingY: l,
728
+ boxBorderPadding: x
729
+ }), N = D(_);
730
+ N < h && (u = _, h = N, w = !0);
1257
731
  }
1258
732
  }
1259
- if (y > s) {
1260
- const S = e.maxWidth === "auto" ? "detected terminal width" : "--max-width", M = b ? `Compact spacing (-x ${_} -y ${N} -p ${O}) was already applied automatically. ` : "", x = b ? "Try a narrower direction (LR vs TD), shorter labels, or widen your terminal." : "Try -x/-y/-p for tighter spacing, a narrower direction (LR vs TD), or widen your terminal.";
1261
- l.write(`Warning: ASCII output is ${y} columns wide, exceeding ${S} of ${s}. ${M}zombie-mermaid does not reflow diagram structure to fit (no label wrapping or direction-flip cascade — see https://github.com/dfadler/zombie-mermaid/issues/335). ${x}
733
+ if (h > i) {
734
+ const p = e.maxWidth === "auto" ? "detected terminal width" : "--max-width", y = w ? `Compact spacing (-x ${$} -y ${L} -p ${R}) was already applied automatically. ` : "", f = w ? "Try a narrower direction (LR vs TD), shorter labels, or widen your terminal." : "Try -x/-y/-p for tighter spacing, a narrower direction (LR vs TD), or widen your terminal.";
735
+ m.write(`Warning: ASCII output is ${h} columns wide, exceeding ${p} of ${i}. ${y}zombie-mermaid does not reflow diagram structure to fit (no label wrapping or direction-flip cascade — see https://github.com/dfadler/zombie-mermaid/issues/335). ${f}
1262
736
  `);
1263
- } else b && l.write(`Note: ASCII output exceeded ${s} columns at the requested spacing; applied compact spacing automatically (-x ${_} -y ${N} -p ${O}) to fit.
737
+ } else w && m.write(`Note: ASCII output exceeded ${i} columns at the requested spacing; applied compact spacing automatically (-x ${$} -y ${L} -p ${R}) to fit.
1264
738
  `);
1265
739
  }
1266
- await $(R(m === void 0, m), f + `
1267
- `, o, e.force);
740
+ await M(I(g === void 0, g), u + `
741
+ `, s, e.force);
1268
742
  }
1269
743
  if (e.svg) {
1270
- const c = {
1271
- ...a,
744
+ const a = {
745
+ ...d,
1272
746
  resolveColors: e.resolveColors
1273
747
  };
1274
- e.direction !== void 0 && (c.direction = e.direction);
1275
- const f = D(u, c);
1276
- await $(R(v, g), f, o, e.force);
748
+ e.direction !== void 0 && (a.direction = e.direction);
749
+ const u = z(c, a);
750
+ await M(I(E, C), u, s, e.force);
1277
751
  }
1278
752
  if (e.html) {
1279
- const c = D(u, a ?? {}), f = e.input ? J(e.input).name : void 0, s = Ht({
1280
- svg: c,
1281
- title: f
753
+ const a = z(c, d ?? {}), u = e.input ? G(e.input).name : void 0, i = Se({
754
+ svg: a,
755
+ title: u
1282
756
  });
1283
- await $(R(h, w), s, o, e.force);
757
+ await M(I(S, T), i, s, e.force);
1284
758
  }
1285
759
  if (e.png) {
1286
- const c = {
1287
- ...a,
760
+ const a = {
761
+ ...d,
1288
762
  resolveColors: !0
1289
763
  };
1290
- e.direction !== void 0 && (c.direction = e.direction);
1291
- const f = D(u, c), s = await Vt(f);
1292
- await $(R(p, d), s, o, e.force);
764
+ e.direction !== void 0 && (a.direction = e.direction);
765
+ const u = z(c, a), i = await ke(u);
766
+ await M(I(k, b), i, s, e.force);
1293
767
  }
1294
768
  }
1295
- function R(e, t) {
1296
- return e ? { kind: "stdout" } : t !== void 0 ? {
769
+ function I(e, n) {
770
+ return e ? { kind: "stdout" } : n !== void 0 ? {
1297
771
  kind: "file",
1298
- path: t
772
+ path: n
1299
773
  } : { kind: "skip" };
1300
774
  }
1301
- async function $(e, t, n, r) {
775
+ async function M(e, n, t, r) {
1302
776
  switch (e.kind) {
1303
777
  case "stdout":
1304
- n.write(t);
778
+ t.write(n);
1305
779
  break;
1306
780
  case "file":
1307
- await en(e.path, t, r);
781
+ await ze(e.path, n, r);
1308
782
  }
1309
783
  }
1310
- function ae(e) {
784
+ function X(e) {
1311
785
  return /* @__PURE__ */ new Error(`Refusing to overwrite existing file "${e}" — pass --force to replace it`);
1312
786
  }
1313
- async function Qt(e, t) {
1314
- if (t) return;
1315
- let n = !0;
787
+ async function Me(e, n) {
788
+ if (n) return;
789
+ let t = !0;
1316
790
  try {
1317
- await ke(e);
791
+ await J(e);
1318
792
  } catch {
1319
- n = !1;
793
+ t = !1;
1320
794
  }
1321
- if (n) throw ae(e);
795
+ if (t) throw X(e);
1322
796
  }
1323
- async function en(e, t, n) {
797
+ async function ze(e, n, t) {
1324
798
  try {
1325
- await Ce(e, t, {
799
+ await Q(e, n, {
1326
800
  encoding: "utf-8",
1327
- flag: n ? "w" : "wx"
801
+ flag: t ? "w" : "wx"
1328
802
  });
1329
803
  } catch (r) {
1330
- throw !n && r instanceof Error && "code" in r && r.code === "EEXIST" ? ae(e) : r;
804
+ throw !t && r instanceof Error && "code" in r && r.code === "EEXIST" ? X(e) : r;
1331
805
  }
1332
806
  }
1333
- async function tn() {
1334
- const e = [], t = process.stdin;
1335
- return t.setEncoding("utf-8"), new Promise((n, r) => {
1336
- t.on("data", (o) => e.push(o)), t.on("end", () => n(e.join(""))), t.on("error", r);
807
+ async function Pe() {
808
+ const e = [], n = process.stdin;
809
+ return n.setEncoding("utf-8"), new Promise((t, r) => {
810
+ n.on("data", (s) => e.push(s)), n.on("end", () => t(e.join(""))), n.on("error", r);
1337
811
  });
1338
812
  }
1339
- var nn = 1e6;
1340
- function rn(e) {
1341
- return new Promise((t, n) => {
813
+ var $e = 1e6;
814
+ function Le(e) {
815
+ return new Promise((n, t) => {
1342
816
  const r = [];
1343
- let o = 0;
1344
- e.on("data", (l) => {
1345
- if (o += l.length, o > nn) {
1346
- n(/* @__PURE__ */ new Error("Request body too large")), e.destroy();
817
+ let s = 0;
818
+ e.on("data", (m) => {
819
+ if (s += m.length, s > $e) {
820
+ t(/* @__PURE__ */ new Error("Request body too large")), e.destroy();
1347
821
  return;
1348
822
  }
1349
- r.push(l);
1350
- }), e.on("end", () => t(Buffer.concat(r).toString("utf-8"))), e.on("error", n);
823
+ r.push(m);
824
+ }), e.on("end", () => n(Buffer.concat(r).toString("utf-8"))), e.on("error", t);
1351
825
  });
1352
826
  }
1353
- function se(e) {
827
+ function Y(e) {
1354
828
  return typeof e == "object" && e !== null;
1355
829
  }
1356
- function on(e) {
1357
- let t;
830
+ function Re(e) {
831
+ let n;
1358
832
  try {
1359
- t = JSON.parse(e);
833
+ n = JSON.parse(e);
1360
834
  } catch {
1361
835
  throw new Error("Invalid JSON in request body");
1362
836
  }
1363
- if (!se(t)) throw new Error("Request body must be a JSON object");
1364
- const n = t.source;
1365
- if (typeof n != "string" || n.trim().length === 0) throw new Error('Provide Mermaid source as a non-empty "source" string');
837
+ if (!Y(n)) throw new Error("Request body must be a JSON object");
838
+ const t = n.source;
839
+ if (typeof t != "string" || t.trim().length === 0) throw new Error('Provide Mermaid source as a non-empty "source" string');
1366
840
  return {
1367
- source: n,
1368
- format: t.format === "svg" ? "svg" : "ascii"
841
+ source: t,
842
+ format: n.format === "svg" ? "svg" : "ascii"
1369
843
  };
1370
844
  }
1371
- function K(e, t, n) {
1372
- const r = JSON.stringify(n);
1373
- e.writeHead(t, {
845
+ function F(e, n, t) {
846
+ const r = JSON.stringify(t);
847
+ e.writeHead(n, {
1374
848
  "Content-Type": "application/json; charset=utf-8",
1375
849
  "Content-Length": Buffer.byteLength(r)
1376
850
  }), e.end(r);
1377
851
  }
1378
- async function an(e, t) {
852
+ async function Be(e, n) {
1379
853
  try {
1380
- const { source: n, format: r } = on(await rn(e));
1381
- K(t, 200, {
1382
- output: r === "svg" ? D(n) : F(n),
854
+ const { source: t, format: r } = Re(await Le(e));
855
+ F(n, 200, {
856
+ output: r === "svg" ? z(t) : O(t),
1383
857
  format: r
1384
858
  });
1385
- } catch (n) {
1386
- K(t, 400, { error: n instanceof Error ? n.message : String(n) });
859
+ } catch (t) {
860
+ F(n, 400, { error: t instanceof Error ? t.message : String(t) });
1387
861
  }
1388
862
  }
1389
- var sn = `<!doctype html>
863
+ var Oe = `<!doctype html>
1390
864
  <html lang="en">
1391
865
  <head>
1392
866
  <meta charset="utf-8" />
@@ -1452,84 +926,84 @@ var sn = `<!doctype html>
1452
926
  </body>
1453
927
  </html>
1454
928
  `;
1455
- function cn() {
1456
- return qe((e, t) => {
1457
- const n = (e.url ?? "/").split("?")[0];
1458
- if (e.method === "GET" && (n === "/" || n === "")) {
1459
- t.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }), t.end(sn);
929
+ function _e() {
930
+ return te((e, n) => {
931
+ const t = (e.url ?? "/").split("?")[0];
932
+ if (e.method === "GET" && (t === "/" || t === "")) {
933
+ n.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }), n.end(Oe);
1460
934
  return;
1461
935
  }
1462
- if (e.method === "POST" && n === "/render") {
1463
- an(e, t);
936
+ if (e.method === "POST" && t === "/render") {
937
+ Be(e, n);
1464
938
  return;
1465
939
  }
1466
- t.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }), t.end("Not found");
940
+ n.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }), n.end("Not found");
1467
941
  });
1468
942
  }
1469
- function ln(e) {
1470
- return new Promise((t, n) => {
1471
- const r = cn();
1472
- r.once("error", n), r.listen(e, "127.0.0.1", () => {
1473
- r.removeListener("error", n), t(r);
943
+ function Ne(e) {
944
+ return new Promise((n, t) => {
945
+ const r = _e();
946
+ r.once("error", t), r.listen(e, "127.0.0.1", () => {
947
+ r.removeListener("error", t), n(r);
1474
948
  });
1475
949
  });
1476
950
  }
1477
- async function dn(e, t = console.log) {
1478
- const n = await ln(e.port), r = n.address();
1479
- return t(`zombie-mermaid web running at http://localhost:${se(r) && typeof r.port == "number" ? r.port : e.port}`), n;
951
+ async function De(e, n = console.log) {
952
+ const t = await Ne(e.port), r = t.address();
953
+ return n(`zombie-mermaid web running at http://localhost:${Y(r) && typeof r.port == "number" ? r.port : e.port}`), t;
1480
954
  }
1481
- async function un(e = (t) => console.error(t)) {
1482
- const t = Ve(), n = new He();
1483
- return await t.connect(n), e("zombie-mermaid MCP server running on stdio (experimental — feedback welcome: https://github.com/dfadler/zombie-mermaid/issues)"), t;
955
+ async function Fe(e = (n) => console.error(n)) {
956
+ const n = re(), t = new ne();
957
+ return await n.connect(t), e("zombie-mermaid MCP server running on stdio (experimental — feedback welcome: https://github.com/dfadler/zombie-mermaid/issues)"), n;
1484
958
  }
1485
- function mn() {
1486
- const e = ce(import.meta.url)("../package.json");
959
+ function Ge() {
960
+ const e = U(import.meta.url)("../package.json");
1487
961
  return typeof e == "object" && e !== null && "version" in e && typeof e.version == "string" ? e.version : "unknown";
1488
962
  }
1489
- var hn = mn();
1490
- async function fn() {
963
+ var We = Ge();
964
+ async function Xe() {
1491
965
  const e = process.argv.slice(2);
1492
- let t;
966
+ let n;
1493
967
  try {
1494
- t = Ze(e);
1495
- } catch (n) {
1496
- console.error(`Error: ${n instanceof Error ? n.message : String(n)}`), process.exit(1);
968
+ n = se(e);
969
+ } catch (t) {
970
+ console.error(`Error: ${t instanceof Error ? t.message : String(t)}`), process.exit(1);
1497
971
  }
1498
- switch (t.command) {
972
+ switch (n.command) {
1499
973
  case "help":
1500
- pn();
974
+ Ye();
1501
975
  break;
1502
976
  case "version":
1503
- console.log(`zombie-mermaid ${hn}`);
977
+ console.log(`zombie-mermaid ${We}`);
1504
978
  break;
1505
979
  case "themes":
1506
980
  console.log(`Available themes:
1507
981
  `);
1508
- for (const n of Object.keys(G)) console.log(` ${n}`);
982
+ for (const t of Object.keys(B)) console.log(` ${t}`);
1509
983
  break;
1510
984
  case "render":
1511
985
  try {
1512
- await Zt(t);
1513
- } catch (n) {
1514
- console.error(`Error: ${n instanceof Error ? n.message : String(n)}`), process.exit(1);
986
+ await Ie(n);
987
+ } catch (t) {
988
+ console.error(`Error: ${t instanceof Error ? t.message : String(t)}`), process.exit(1);
1515
989
  }
1516
990
  break;
1517
991
  case "web":
1518
992
  try {
1519
- await dn(t);
1520
- } catch (n) {
1521
- console.error(`Error: ${n instanceof Error ? n.message : String(n)}`), process.exit(1);
993
+ await De(n);
994
+ } catch (t) {
995
+ console.error(`Error: ${t instanceof Error ? t.message : String(t)}`), process.exit(1);
1522
996
  }
1523
997
  break;
1524
998
  case "mcp":
1525
999
  try {
1526
- await un();
1527
- } catch (n) {
1528
- console.error(`Error: ${n instanceof Error ? n.message : String(n)}`), process.exit(1);
1000
+ await Fe();
1001
+ } catch (t) {
1002
+ console.error(`Error: ${t instanceof Error ? t.message : String(t)}`), process.exit(1);
1529
1003
  }
1530
1004
  }
1531
1005
  }
1532
- function pn() {
1006
+ function Ye() {
1533
1007
  console.log(`
1534
1008
  zombie-mermaid — render Mermaid diagrams from the command line
1535
1009
 
@@ -1603,7 +1077,7 @@ Options:
1603
1077
  -v, --version Show version
1604
1078
  `.trim());
1605
1079
  }
1606
- fn();
1080
+ Xe();
1607
1081
  export {
1608
- fn as main
1082
+ Xe as main
1609
1083
  };