@senad-d/drawme 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 +254 -0
- package/assets/data/SHAPE-INDEX-NOTICE.md +17 -0
- package/assets/data/shape-index.json.gz +0 -0
- package/assets/references/diagram-types.md +205 -0
- package/assets/references/mermaid-authoring.md +56 -0
- package/assets/references/troubleshooting.md +63 -0
- package/assets/references/xml-authoring.md +205 -0
- package/package.json +67 -0
- package/src/dom.ts +51 -0
- package/src/drawio.ts +363 -0
- package/src/explain.ts +220 -0
- package/src/index.ts +267 -0
- package/src/png.ts +41 -0
- package/src/shapesearch.ts +217 -0
- package/src/validate.ts +376 -0
- package/src/workflow.ts +34 -0
package/src/validate.ts
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic structural linter for `.drawio` files — a pure-TypeScript port of
|
|
3
|
+
* the skill's `scripts/validate.py`. Catches the class of mistakes a vision
|
|
4
|
+
* self-check is slow and unreliable at: dangling edge endpoints, duplicate or
|
|
5
|
+
* reserved ids, broken parent references, and (as warnings) off-grid geometry,
|
|
6
|
+
* overlapping sibling nodes, and edge-routing defects. Runs without launching
|
|
7
|
+
* draw.io. Local-only, no network.
|
|
8
|
+
*/
|
|
9
|
+
import { readFile } from "node:fs/promises";
|
|
10
|
+
import { DOMParser } from "@xmldom/xmldom";
|
|
11
|
+
import { type DomEl, attr, childrenByTag, directText, elemChildren, firstByTag } from "./dom";
|
|
12
|
+
|
|
13
|
+
const RESERVED = new Set(["0", "1"]);
|
|
14
|
+
|
|
15
|
+
/** [x, y, width, height]. width/height may be NaN when the source omits them. */
|
|
16
|
+
type Rect = [number, number, number, number];
|
|
17
|
+
type ById = Map<string, DomEl>;
|
|
18
|
+
|
|
19
|
+
export interface ValidateResult {
|
|
20
|
+
errors: string[];
|
|
21
|
+
warnings: string[];
|
|
22
|
+
/** Readability score (lower is better); comparable only across variants of the same graph. */
|
|
23
|
+
score: { total: number; through: number; crossings: number; overlaps: number };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Python `float()` semantics: accepts nan/inf, strict-numeric otherwise; else undefined (≈ ValueError). */
|
|
27
|
+
function pyFloat(s: string): number | undefined {
|
|
28
|
+
const t = s.trim().toLowerCase();
|
|
29
|
+
if (t === "nan" || t === "+nan" || t === "-nan") return Number.NaN;
|
|
30
|
+
if (t === "inf" || t === "+inf" || t === "infinity" || t === "+infinity") return Infinity;
|
|
31
|
+
if (t === "-inf" || t === "-infinity") return -Infinity;
|
|
32
|
+
if (!/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(t)) return undefined;
|
|
33
|
+
const n = Number(t);
|
|
34
|
+
return Number.isNaN(n) ? undefined : n;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Python `repr()` of an optional attribute, for message parity with validate.py. */
|
|
38
|
+
function repr(s: string | null): string {
|
|
39
|
+
return s === null ? "None" : `'${s}'`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function rect(cell: DomEl): Rect | null {
|
|
43
|
+
const gy = firstByTag(cell, "mxGeometry");
|
|
44
|
+
if (!gy) return null;
|
|
45
|
+
const x = pyFloat(attr(gy, "x") ?? "0");
|
|
46
|
+
const y = pyFloat(attr(gy, "y") ?? "0");
|
|
47
|
+
const w = pyFloat(attr(gy, "width") ?? "nan");
|
|
48
|
+
const h = pyFloat(attr(gy, "height") ?? "nan");
|
|
49
|
+
if (x === undefined || y === undefined || w === undefined || h === undefined) return null;
|
|
50
|
+
return [x, y, w, h];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function hasNaN(r: Rect): boolean {
|
|
54
|
+
return r.some((v) => Number.isNaN(v));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** True for edge labels / relative-positioned child vertices (they legitimately omit w/h). */
|
|
58
|
+
function isEdgeLabel(cell: DomEl): boolean {
|
|
59
|
+
if ((attr(cell, "style") ?? "").includes("edgeLabel")) return true;
|
|
60
|
+
const gy = firstByTag(cell, "mxGeometry");
|
|
61
|
+
return gy !== null && gy.getAttribute("relative") === "1";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function overlap(a: Rect, b: Rect): boolean {
|
|
65
|
+
const [ax, ay, aw, ah] = a;
|
|
66
|
+
const [bx, by, bw, bh] = b;
|
|
67
|
+
return ax < bx + bw && bx < ax + aw && ay < by + bh && by < ay + ah;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function styleNum(style: string | null, key: string): number | undefined {
|
|
71
|
+
for (const part of (style ?? "").split(";")) {
|
|
72
|
+
if (part.startsWith(key + "=")) return pyFloat(part.split("=").slice(1).join("="));
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Absolute (x, y, w, h) of a vertex, summing parent-container offsets. */
|
|
78
|
+
function absRect(cell: DomEl, byId: ById): Rect | null {
|
|
79
|
+
const r = rect(cell);
|
|
80
|
+
if (r === null || hasNaN(r)) return null;
|
|
81
|
+
let [x, y] = r;
|
|
82
|
+
const [, , w, h] = r;
|
|
83
|
+
let parent = attr(cell, "parent");
|
|
84
|
+
const seen = new Set<string>();
|
|
85
|
+
while (parent && byId.has(parent) && !seen.has(parent)) {
|
|
86
|
+
seen.add(parent);
|
|
87
|
+
const p = byId.get(parent)!;
|
|
88
|
+
if (p.getAttribute("vertex") === "1") {
|
|
89
|
+
const pr = rect(p);
|
|
90
|
+
if (pr && !hasNaN(pr)) {
|
|
91
|
+
x += pr[0];
|
|
92
|
+
y += pr[1];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
parent = attr(p, "parent");
|
|
96
|
+
}
|
|
97
|
+
return [x, y, w, h];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
type Point = [number, number];
|
|
101
|
+
|
|
102
|
+
/** Absolute point where `edge` meets its source/target vertex (honours exit/entry). */
|
|
103
|
+
function endpoint(edge: DomEl, end: "source" | "target", byId: ById): Point | null {
|
|
104
|
+
const vid = attr(edge, end);
|
|
105
|
+
if (!vid || !byId.has(vid)) return null;
|
|
106
|
+
const box = absRect(byId.get(vid)!, byId);
|
|
107
|
+
if (box === null) return null;
|
|
108
|
+
const [x, y, w, h] = box;
|
|
109
|
+
const style = attr(edge, "style") ?? "";
|
|
110
|
+
const fx = styleNum(style, end === "source" ? "exitX" : "entryX");
|
|
111
|
+
const fy = styleNum(style, end === "source" ? "exitY" : "entryY");
|
|
112
|
+
return [x + (fx ?? 0.5) * w, y + (fy ?? 0.5) * h];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function edgeWaypoints(edge: DomEl): Point[] {
|
|
116
|
+
const gy = firstByTag(edge, "mxGeometry");
|
|
117
|
+
if (!gy) return [];
|
|
118
|
+
const arr = firstByTag(gy, "Array");
|
|
119
|
+
if (!arr) return [];
|
|
120
|
+
const pts: Point[] = [];
|
|
121
|
+
for (const pt of childrenByTag(arr, "mxPoint")) {
|
|
122
|
+
const px = attr(pt, "x");
|
|
123
|
+
const py = attr(pt, "y");
|
|
124
|
+
if (px !== null && py !== null) {
|
|
125
|
+
const nx = pyFloat(px);
|
|
126
|
+
const ny = pyFloat(py);
|
|
127
|
+
if (nx !== undefined && ny !== undefined) pts.push([nx, ny]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return pts;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Absolute polyline for a waypointed edge, or null when auto-routed / unresolved. */
|
|
134
|
+
function edgeRoute(edge: DomEl, byId: ById): Point[] | null {
|
|
135
|
+
const wp = edgeWaypoints(edge);
|
|
136
|
+
if (wp.length === 0) return null;
|
|
137
|
+
const s = endpoint(edge, "source", byId);
|
|
138
|
+
const t = endpoint(edge, "target", byId);
|
|
139
|
+
if (s === null || t === null) return null;
|
|
140
|
+
return [s, ...wp, t];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function orient(a: Point, b: Point, c: Point): number {
|
|
144
|
+
const v = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
145
|
+
if (Math.abs(v) < 1e-9) return 0;
|
|
146
|
+
return v > 0 ? 1 : -1;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function segmentsCross(p1: Point, p2: Point, p3: Point, p4: Point): boolean {
|
|
150
|
+
const o1 = orient(p1, p2, p3);
|
|
151
|
+
const o2 = orient(p1, p2, p4);
|
|
152
|
+
const o3 = orient(p3, p4, p1);
|
|
153
|
+
const o4 = orient(p3, p4, p2);
|
|
154
|
+
return o1 !== o2 && o3 !== o4 && ![o1, o2, o3, o4].includes(0);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function pointInRect(p: Point, box: Rect, eps = 1e-6): boolean {
|
|
158
|
+
const [x, y, w, h] = box;
|
|
159
|
+
return x + eps < p[0] && p[0] < x + w - eps && y + eps < p[1] && p[1] < y + h - eps;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function routeHitsRect(points: Point[], box: Rect): boolean {
|
|
163
|
+
const [x, y, w, h] = box;
|
|
164
|
+
const corners: Point[] = [
|
|
165
|
+
[x, y],
|
|
166
|
+
[x + w, y],
|
|
167
|
+
[x + w, y + h],
|
|
168
|
+
[x, y + h],
|
|
169
|
+
];
|
|
170
|
+
const borders: [Point, Point][] = corners.map((c, i) => [c, corners[(i + 1) % 4]]);
|
|
171
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
172
|
+
const a = points[i];
|
|
173
|
+
const b = points[i + 1];
|
|
174
|
+
if (pointInRect(a, box) || pointInRect(b, box)) return true;
|
|
175
|
+
for (const [c, d] of borders) if (segmentsCross(a, b, c, d)) return true;
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function routesCross(pa: Point[], pb: Point[]): boolean {
|
|
181
|
+
for (let i = 0; i < pa.length - 1; i++)
|
|
182
|
+
for (let j = 0; j < pb.length - 1; j++)
|
|
183
|
+
if (segmentsCross(pa[i], pa[i + 1], pb[j], pb[j + 1])) return true;
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
type RoutedEdge = [string | null, Point[], Set<string | null>];
|
|
188
|
+
type LeafBox = [string | null, Rect];
|
|
189
|
+
|
|
190
|
+
/** Edges with a resolvable waypointed route, tagged with their id and endpoint ids. */
|
|
191
|
+
function collectRoutedEdges(cells: DomEl[], ids: ById): RoutedEdge[] {
|
|
192
|
+
const routed: RoutedEdge[] = [];
|
|
193
|
+
for (const c of cells) {
|
|
194
|
+
if (c.getAttribute("edge") === "1") {
|
|
195
|
+
const pts = edgeRoute(c, ids);
|
|
196
|
+
if (pts) routed.push([attr(c, "id"), pts, new Set([attr(c, "source"), attr(c, "target")])]);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return routed;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Absolute boxes of leaf vertices (real nodes, not containers or edge labels). */
|
|
203
|
+
function collectLeafBoxes(cells: DomEl[], ids: ById, parents: Set<string | null>): LeafBox[] {
|
|
204
|
+
const leaves: LeafBox[] = [];
|
|
205
|
+
for (const c of cells) {
|
|
206
|
+
if (c.getAttribute("vertex") === "1" && !parents.has(attr(c, "id")) && !isEdgeLabel(c)) {
|
|
207
|
+
const box = absRect(c, ids);
|
|
208
|
+
if (box) leaves.push([attr(c, "id"), box]);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return leaves;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function routeThroughWarnings(routed: RoutedEdge[], leaves: LeafBox[]): string[] {
|
|
215
|
+
const warns: string[] = [];
|
|
216
|
+
for (const [eid, pts, ends] of routed) {
|
|
217
|
+
for (const [vid, box] of leaves) {
|
|
218
|
+
if (!ends.has(vid) && routeHitsRect(pts, box)) warns.push(`edge ${repr(eid)} routes through vertex ${repr(vid)}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return warns;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function routeCrossWarnings(routed: RoutedEdge[]): string[] {
|
|
225
|
+
const warns: string[] = [];
|
|
226
|
+
for (let i = 0; i < routed.length; i++) {
|
|
227
|
+
for (let j = i + 1; j < routed.length; j++) {
|
|
228
|
+
if (routesCross(routed[i][1], routed[j][1])) warns.push(`edges ${repr(routed[i][0])} and ${repr(routed[j][0])} cross`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return warns;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function geometryWarnings(cells: DomEl[], ids: ById, parents: Set<string | null>): string[] {
|
|
235
|
+
const routed = collectRoutedEdges(cells, ids);
|
|
236
|
+
const leaves = collectLeafBoxes(cells, ids, parents);
|
|
237
|
+
return [...routeThroughWarnings(routed, leaves), ...routeCrossWarnings(routed)];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Fold UserObject/object wrappers (links & metadata) into their inner mxCell so
|
|
242
|
+
* the wrapper id resolves for edges that reference it.
|
|
243
|
+
*/
|
|
244
|
+
function collectCells(root: DomEl | null): DomEl[] {
|
|
245
|
+
const cells: DomEl[] = [];
|
|
246
|
+
for (const child of root ? elemChildren(root) : []) {
|
|
247
|
+
if (child.tagName === "mxCell") cells.push(child);
|
|
248
|
+
else if (child.tagName === "UserObject" || child.tagName === "object") {
|
|
249
|
+
const inner = firstByTag(child, "mxCell");
|
|
250
|
+
if (inner) {
|
|
251
|
+
inner.setAttribute("id", attr(child, "id") ?? "");
|
|
252
|
+
cells.push(inner);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return cells;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Map every cell by id (blank key for id-less cells); report duplicate ids as errors. */
|
|
260
|
+
function buildIds(cells: DomEl[], errors: string[]): ById {
|
|
261
|
+
const ids: ById = new Map();
|
|
262
|
+
for (const c of cells) {
|
|
263
|
+
const cid = attr(c, "id");
|
|
264
|
+
if (cid !== null && ids.has(cid)) errors.push(`duplicate id ${repr(cid)}`);
|
|
265
|
+
ids.set(cid ?? "", c);
|
|
266
|
+
}
|
|
267
|
+
return ids;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Geometry checks for a single non-edge-label vertex. */
|
|
271
|
+
function checkVertexGeometry(c: DomEl, cid: string | null, errors: string[], warns: string[]): void {
|
|
272
|
+
const r = rect(c);
|
|
273
|
+
if (r === null || hasNaN(r)) {
|
|
274
|
+
errors.push(`vertex ${repr(cid)} has missing/invalid geometry`);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const [x, y, w, h] = r;
|
|
278
|
+
if (w <= 0 || h <= 0) warns.push(`vertex ${repr(cid)} non-positive size ${String(w)}x${String(h)}`);
|
|
279
|
+
if (x < 0 || y < 0) warns.push(`vertex ${repr(cid)} negative position (${String(x)},${String(y)})`);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Per-cell reference and geometry checks (parents, edge endpoints, reserved ids, geometry). */
|
|
283
|
+
function checkCell(c: DomEl, ids: ById, errors: string[], warns: string[]): void {
|
|
284
|
+
const cid = attr(c, "id");
|
|
285
|
+
const parent = attr(c, "parent");
|
|
286
|
+
const isV = c.getAttribute("vertex") === "1";
|
|
287
|
+
const isE = c.getAttribute("edge") === "1";
|
|
288
|
+
if (parent !== null && !ids.has(parent)) errors.push(`cell ${repr(cid)} parent ${repr(parent)} does not exist`);
|
|
289
|
+
for (const end of ["source", "target"] as const) {
|
|
290
|
+
const ref = attr(c, end);
|
|
291
|
+
if (ref && !ids.has(ref)) errors.push(`edge ${repr(cid)} ${end} ${repr(ref)} does not exist`);
|
|
292
|
+
}
|
|
293
|
+
if ((isV || isE) && cid !== null && RESERVED.has(cid)) errors.push(`cell ${repr(cid)} reuses reserved id 0/1`);
|
|
294
|
+
if (isV && !isEdgeLabel(c)) checkVertexGeometry(c, cid, errors, warns);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Sibling overlap: leaf vertices only (containers legitimately wrap children). */
|
|
298
|
+
function overlapWarnings(cells: DomEl[], parents: Set<string | null>): string[] {
|
|
299
|
+
const boxes: [string | null, string | null, Rect][] = [];
|
|
300
|
+
for (const c of cells) {
|
|
301
|
+
if (c.getAttribute("vertex") === "1" && !parents.has(attr(c, "id"))) {
|
|
302
|
+
const r = rect(c);
|
|
303
|
+
if (r && !hasNaN(r)) boxes.push([attr(c, "id"), attr(c, "parent"), r]);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const warns: string[] = [];
|
|
307
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
308
|
+
for (let j = i + 1; j < boxes.length; j++) {
|
|
309
|
+
const [ia, pa, ra] = boxes[i];
|
|
310
|
+
const [ib, pb, rb] = boxes[j];
|
|
311
|
+
if (pa === pb && overlap(ra, rb)) warns.push(`vertices ${repr(ia)} and ${repr(ib)} overlap`);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return warns;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function checkPage(diagram: DomEl): [string[], string[]] {
|
|
318
|
+
const name = attr(diagram, "name") ?? "?";
|
|
319
|
+
const model = firstByTag(diagram, "mxGraphModel");
|
|
320
|
+
if (!model) {
|
|
321
|
+
if (directText(diagram).trim()) return [[], [`page '${name}': compressed, skipped (cannot lint)`]];
|
|
322
|
+
return [[`page '${name}': no <mxGraphModel>`], []];
|
|
323
|
+
}
|
|
324
|
+
const cells = collectCells(firstByTag(model, "root"));
|
|
325
|
+
|
|
326
|
+
const errors: string[] = [];
|
|
327
|
+
const warns: string[] = [];
|
|
328
|
+
const ids = buildIds(cells, errors);
|
|
329
|
+
const parents = new Set<string | null>();
|
|
330
|
+
for (const c of cells) parents.add(attr(c, "parent"));
|
|
331
|
+
|
|
332
|
+
for (const c of cells) checkCell(c, ids, errors, warns);
|
|
333
|
+
warns.push(...overlapWarnings(cells, parents), ...geometryWarnings(cells, ids, parents));
|
|
334
|
+
return [errors, warns];
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** Lint a `.drawio` XML string. Throws only on unrecoverable parse failure. */
|
|
338
|
+
export function validateXml(xml: string): ValidateResult {
|
|
339
|
+
const doc = new DOMParser().parseFromString(xml, "text/xml");
|
|
340
|
+
const rootEl = doc.documentElement as unknown as DomEl | null;
|
|
341
|
+
if (!rootEl) throw new Error("no root element (malformed XML)");
|
|
342
|
+
const pageList = childrenByTag(rootEl, "diagram");
|
|
343
|
+
const pages = pageList.length > 0 ? pageList : [rootEl];
|
|
344
|
+
|
|
345
|
+
const errors: string[] = [];
|
|
346
|
+
const warnings: string[] = [];
|
|
347
|
+
for (const page of pages) {
|
|
348
|
+
const [e, w] = checkPage(page);
|
|
349
|
+
errors.push(...e);
|
|
350
|
+
warnings.push(...w);
|
|
351
|
+
}
|
|
352
|
+
const through = warnings.filter((w) => w.includes("routes through")).length;
|
|
353
|
+
const crossings = warnings.filter((w) => w.endsWith(" cross")).length;
|
|
354
|
+
const overlaps = warnings.filter((w) => w.endsWith(" overlap")).length;
|
|
355
|
+
return {
|
|
356
|
+
errors,
|
|
357
|
+
warnings,
|
|
358
|
+
score: { total: 20 * through + 10 * crossings + 5 * overlaps, through, crossings, overlaps },
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Read and lint a `.drawio` file. Read/parse failures are returned as errors, never thrown. */
|
|
363
|
+
export async function validateFile(path: string): Promise<ValidateResult> {
|
|
364
|
+
const empty = { total: 0, through: 0, crossings: 0, overlaps: 0 };
|
|
365
|
+
let xml: string;
|
|
366
|
+
try {
|
|
367
|
+
xml = await readFile(path, "utf8");
|
|
368
|
+
} catch (e) {
|
|
369
|
+
return { errors: [`cannot read ${path}: ${(e as Error).message}`], warnings: [], score: empty };
|
|
370
|
+
}
|
|
371
|
+
try {
|
|
372
|
+
return validateXml(xml);
|
|
373
|
+
} catch (e) {
|
|
374
|
+
return { errors: [`cannot parse ${path}: ${(e as Error).message}`], warnings: [], score: empty };
|
|
375
|
+
}
|
|
376
|
+
}
|
package/src/workflow.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the message the `/drawme` command injects to drive the diagram
|
|
3
|
+
* workflow. It steers the model through the skill's author→validate→preview→
|
|
4
|
+
* self-check→final loop while pointing it at the DrawMe tools and the bundled,
|
|
5
|
+
* network-free authoring references.
|
|
6
|
+
*/
|
|
7
|
+
export function drawmeWorkflow(description: string, refDir: string): string {
|
|
8
|
+
const quoted = description.replaceAll("\n", "\n> ");
|
|
9
|
+
return [
|
|
10
|
+
"# DrawMe — create a diagram",
|
|
11
|
+
"",
|
|
12
|
+
"The user asked for this diagram:",
|
|
13
|
+
"",
|
|
14
|
+
`> ${quoted}`,
|
|
15
|
+
"",
|
|
16
|
+
"Produce a `.drawio` file and export it, following the workflow below. For anything that touches the draw.io CLI, use the DrawMe tools (`drawio_check`, `drawio_validate`, `drawio_export`) — do NOT shell out to `drawio` yourself. Everything runs locally; there is no network access.",
|
|
17
|
+
"",
|
|
18
|
+
"1. **Check** — call `drawio_check` once (note the version: v30+ unlocks Mermaid conversion and the auto-layout pass). If the CLI is unavailable, still author the `.drawio` XML, tell the user how to install draw.io / open the file, and skip the export + self-check steps.",
|
|
19
|
+
"2. **Plan** — choose the diagram type, shapes, relationships, and layout direction (LR or TB). For a specific type (ERD, UML class, sequence, C4, architecture, ML/DL, flowchart, SysML, BPMN, network, swimlane), read `" +
|
|
20
|
+
refDir +
|
|
21
|
+
"/diagram-types.md`. If it is a standard type with no custom styling AND the CLI is v30+, prefer authoring concise **Mermaid** and converting it with `drawio_from_mermaid` (automatic layout, no hand-placed coordinates) — read `" +
|
|
22
|
+
refDir +
|
|
23
|
+
"/mermaid-authoring.md`.",
|
|
24
|
+
"3. **Author** — for hand-written `.drawio` XML, first read `" +
|
|
25
|
+
refDir +
|
|
26
|
+
"/xml-authoring.md` (skeleton, cell/edge forms, containers, palette, spacing). Never reuse ids `0`/`1`; every edge `mxCell` needs a `<mxGeometry relative=\"1\" as=\"geometry\"/>` child; keep the XML uncompressed. When you need a specific vendor / UML / BPMN / cloud shape, call `drawio_shapesearch` for the exact `style=` instead of guessing. For large graphs (>~15 nodes) on v30+, hand-write the nodes then run `drawio_layout` rather than hand-placing coordinates. Write the file to the user's working directory unless they gave a path.",
|
|
27
|
+
"4. **Validate** — call `drawio_validate` on the file and fix every reported error before exporting.",
|
|
28
|
+
"5. **Preview & self-check** — call `drawio_export` with `mode: \"preview\"` (clean, width-capped PNG). Read the PNG with your own vision and fix obvious problems (overlaps, clipped labels, missing/stacked/crossing edges, off-canvas shapes) by editing the XML and re-previewing. Cap at 2 self-check rounds. If an export fails, read `" +
|
|
29
|
+
refDir +
|
|
30
|
+
"/troubleshooting.md`.",
|
|
31
|
+
"6. **Review** — show the PNG to the user, apply targeted XML edits from their feedback, and re-preview until they approve.",
|
|
32
|
+
"7. **Final export** — call `drawio_export` with `mode: \"final\"` for each requested format (default PNG; SVG/PDF/JPG on request). Report the `.drawio` source path and every exported file path, and offer to open the source in the desktop app with `drawio_open` for fine-tuning.",
|
|
33
|
+
].join("\n");
|
|
34
|
+
}
|