Skip to content

Build your personal knowledge base with Trilium Notes

test(mermaid): fail when a registered diagram type has no highlighting

Elian Doran6a54fd36e17b
diff --git a/.claude/skills/developing-mermaid-highlighting/SKILL.md b/.claude/skills/developing-mermaid-highlighting/SKILL.md
index d377863fe1..5658fde000 100644
--- a/.claude/skills/developing-mermaid-highlighting/SKILL.md
+++ b/.claude/skills/developing-mermaid-highlighting/SKILL.md
@@ -81,7 +81,11 @@ reject them.
## Adding a diagram type
-Every type Mermaid 12.2.0 registers has a grammar. Swimlanes (`swimlane-beta`) is a flowchart to
+Every type Mermaid 12.2.0 registers has a grammar, and the client's `mermaid_highlighting.spec.ts`
+(next to `sample_diagrams.ts`) keeps it so: it fails when `mermaid.getRegisteredDiagramsMetadata()`
+lists an id missing from its `HIGHLIGHTED_AS` table, and when a sample of a listed type does not
+parse as that node with a parser in `DIAGRAM_PARSERS`. A Mermaid upgrade that adds a type therefore
+starts here; a type with no Trilium sample goes in `UNSAMPLED_CODE`. Swimlanes (`swimlane-beta`) is a flowchart to
Mermaid and to us; agent flow (`agentflow-beta`) is the flowchart grammar in its `agentflow`
dialect (`DIAGRAM_PARSERS` configures it), which adds its `flow`, `connector` and `global`
keywords without taking those names away from flowchart node ids. Use case follows Mermaid's
@@ -114,7 +118,9 @@ extension (Mermaid-Chart/vscode-mermaid-chart, `syntaxes/`) only lists keywords.
6. **Fixtures**: `test/<diagram>.txt`, named exactly like the directory. Start with Trilium's own
sample for the type (`apps/client/src/widgets/type_widgets/mermaid/sample_diagrams.ts`), then one
case per construct. Add a detection case to `test/mermaid.txt`.
-7. Regenerate, run the suite, measure coverage.
+7. **Map Mermaid's id** to the node in `HIGHLIGHTED_AS` (`mermaid_highlighting.spec.ts`), and run
+ it: `cd apps/client && pnpm exec vitest run src/widgets/type_widgets/mermaid`.
+8. Regenerate, run the suite, measure coverage.
## Lezer traps already hit
diff --git a/apps/client/src/widgets/type_widgets/mermaid/mermaid_highlighting.spec.ts b/apps/client/src/widgets/type_widgets/mermaid/mermaid_highlighting.spec.ts
new file mode 100644
index 0000000000..543a38f13a
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/mermaid/mermaid_highlighting.spec.ts
@@ -0,0 +1,87 @@
+import { DIAGRAM_PARSERS } from "@triliumnext/codemirror/src/languages/mermaid/index.js";
+import {
+ parser as dispatcher
+} from "@triliumnext/codemirror/src/languages/mermaid/parsers/mermaid/parser.js";
+import mermaid from "mermaid";
+import { describe, expect, it } from "vitest";
+
+import SAMPLE_DIAGRAMS from "./sample_diagrams";
+
+/**
+ * The node the dispatcher grammar of the Mermaid language reads each diagram type as, keyed by
+ * Mermaid's detector id. `DIAGRAM_PARSERS` mounts the diagram's own grammar on that node.
+ */
+const HIGHLIGHTED_AS: Record<string, string> = {
+ "agentflow": "AgentflowDiagram",
+ "architecture": "ArchitectureDiagram",
+ "block": "BlockDiagram",
+ "c4": "C4Diagram",
+ "classDiagram": "ClassDiagram",
+ "cynefin": "CynefinDiagram",
+ "er": "ERDiagram",
+ "eventmodeling": "EventModelingDiagram",
+ "flowchart-elk": "FlowchartDiagram",
+ "flowchart-v2": "FlowchartDiagram",
+ "gantt": "GanttDiagram",
+ "gitGraph": "GitDiagram",
+ "ishikawa": "IshikawaDiagram",
+ "journey": "JourneyDiagram",
+ "kanban": "KanbanDiagram",
+ "mindmap": "MindmapDiagram",
+ "packet": "PacketDiagram",
+ "pie": "PieDiagram",
+ "quadrantChart": "QuadrantDiagram",
+ "radar": "RadarDiagram",
+ "railroad": "RailroadDiagram",
+ "railroadAbnf": "RailroadAbnfDiagram",
+ "railroadEbnf": "RailroadEbnfDiagram",
+ "railroadPeg": "RailroadPegDiagram",
+ "requirement": "RequirementDiagram",
+ "sankey": "SankeyDiagram",
+ "sequence": "SequenceDiagram",
+ "stateDiagram": "StateDiagram",
+ "swimlane": "FlowchartDiagram",
+ "timeline": "TimelineDiagram",
+ "treemap": "TreemapDiagram",
+ "treeView": "TreeViewDiagram",
+ "usecase": "UsecaseDiagram",
+ "venn": "VennDiagram",
+ "wardley": "WardleyDiagram",
+ "xychart": "XYChartDiagram"
+};
+
+/**
+ * Detector ids that need no highlighting: Mermaid's internal ones, and `info`, which only prints
+ * Mermaid's version.
+ */
+const NOT_HIGHLIGHTED = new Set([ "error", "---", "info" ]);
+
+/** Code for the types `SAMPLE_DIAGRAMS` has no sample of. */
+const UNSAMPLED_CODE = [
+ "flowchart-elk TD\n A --> B",
+ "railroad-ebnf-beta\n a = \"x\";",
+ "railroad-abnf-beta\n a = \"x\";",
+ "railroad-peg-beta\n a <- \"x\";"
+];
+
+describe("Mermaid highlighting", () => {
+ it("has a grammar for every diagram type Mermaid registers", async () => {
+ // `mermaid.parse()` registers the built-in diagrams on first use.
+ await mermaid.parse("info");
+ const registered = mermaid.getRegisteredDiagramsMetadata()
+ .map(({ id }) => id)
+ .filter((id) => !NOT_HIGHLIGHTED.has(id));
+ expect(registered.filter((id) => !(id in HIGHLIGHTED_AS))).toEqual([]);
+
+ const checked = new Set<string>();
+ const samples = SAMPLE_DIAGRAMS.map(({ content }) => content);
+ for (const code of [ ...samples, ...UNSAMPLED_CODE ]) {
+ const id = mermaid.detectType(code);
+ const node = dispatcher.parse(code).topNode.lastChild?.name ?? "";
+ expect({ id, node }).toEqual({ id, node: HIGHLIGHTED_AS[id] });
+ expect(DIAGRAM_PARSERS[node], node).toBeDefined();
+ checked.add(id);
+ }
+ expect(registered.filter((id) => !checked.has(id))).toEqual([]);
+ });
+});