Files
vscode-cowriting-plugin/src/mermaidDiff.ts
T
2026-06-11 16:29:36 -07:00

50 lines
1.7 KiB
TypeScript

/**
* mermaidDiff — F7.1 (#22) dispatcher. Detects a mermaid diagram's type and
* routes a baseline→current diff to the matching pure differ, which re-emits the
* CURRENT diagram source augmented with mermaid styling directives (INV-29). Any
* unsupported type or parser surprise degrades to the v1 whole-block badge
* (INV-30) — this function never throws. vscode-free, DOM-free, deterministic.
*/
import { diffFlowchart } from "./mermaidFlowchartDiff";
import { diffSequence } from "./mermaidSequenceDiff";
/** Theme-neutral colors baked into emitted mermaid source (source can't read CSS vars). */
export const CW_COLORS = {
added: "#2ea043",
changed: "#d29922",
removed: "#808080",
} as const;
export type DiagramType = "flowchart" | "sequence" | "other";
export interface MermaidDiffAugmented {
kind: "augmented";
source: string;
}
export interface MermaidDiffFallback {
kind: "fallback";
}
export type MermaidDiffResult = MermaidDiffAugmented | MermaidDiffFallback;
export function detectDiagramType(source: string): DiagramType {
for (const line of source.split(/\r?\n/)) {
const t = line.trim();
if (t === "" || t.startsWith("%%")) continue;
if (/^(flowchart|graph)\b/.test(t)) return "flowchart";
if (/^sequenceDiagram\b/.test(t)) return "sequence";
return "other";
}
return "other";
}
export function diffMermaid(beforeSrc: string, currentSrc: string): MermaidDiffResult {
const type = detectDiagramType(currentSrc);
try {
if (type === "flowchart") return { kind: "augmented", source: diffFlowchart(beforeSrc, currentSrc) };
if (type === "sequence") return { kind: "augmented", source: diffSequence(beforeSrc, currentSrc) };
} catch {
return { kind: "fallback" };
}
return { kind: "fallback" };
}