Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(codemirror): leave Mermaid accessibility lines inside labels to the diagram
Elian Doran3867b37d04ab
diff --git a/.claude/skills/developing-mermaid-highlighting/SKILL.md b/.claude/skills/developing-mermaid-highlighting/SKILL.mdindex 2242acb009..ed1c5c876f 100644--- a/.claude/skills/developing-mermaid-highlighting/SKILL.md+++ b/.claude/skills/developing-mermaid-highlighting/SKILL.md@@ -236,6 +236,10 @@ root; `\n`, `\r` and `\t` in an input are unescaped.## Known limitations+- An `accTitle:` or `accDescr:` line inside multi-line content is that content, and+ `textAroundAccessibility()` finds it by the diagram grammar's tree: a token begun on an earlier+ line, or a body in `CONTENT_BODIES`. State notes, class notes, git commit messages and pie labels+ over several lines are not tokens in their grammars, so a statement there still highlights as one.- `mindmap root` (root on the `mindmap` line, valid Mermaid) parses as empty lines.- A bare word under a kanban column parses as another column, not a card.- Dotted edge labels (`A -. text .-> B`) parse as two links.diff --git a/packages/codemirror/src/languages/mermaid/index.ts b/packages/codemirror/src/languages/mermaid/index.tsindex eda307184d..c792f0b916 100644--- a/packages/codemirror/src/languages/mermaid/index.ts+++ b/packages/codemirror/src/languages/mermaid/index.ts@@ -90,11 +90,11 @@ export const DIAGRAM_PARSERS: Record<string, LRParser> = {export const mermaidLanguage = LRLanguage.define({name: "mermaid",parser: mermaidParser.configure({- wrap: parseMixed((node) => {+ wrap: parseMixed((node, input) => {if (node.name === "FrontMatterContent") return { parser: yamlLanguage.parser };const parser = nestedParsers[node.name];if (!parser) return null;- const overlay = textAroundAccessibility(node.node);+ const overlay = textAroundAccessibility(node.node, input, parser);return overlay ? { parser, overlay } : { parser };})}),@@ -113,6 +113,8 @@ const nestedParsers = Object.fromEntries(Object.entries(DIAGRAM_PARSERS).map(([})]));+const CONTENT_BODIES = new Set([ "ClassBody", "StateBody" ]);+export function mermaid() {return new LanguageSupport(mermaidLanguage);}@@ -135,16 +137,44 @@ function metadataYaml(node: SyntaxNodeRef, input: Input) {/*** The ranges of a diagram outside its `AccTitle` and `AccDescr` nodes, for the diagram's parser to* cover; `null` when it has none. Those nodes are the only named children of a diagram, and- * diagram text always comes before each one.+ * diagram text always comes before each one. A statement on a line inside multi-line content is+ * that content, as in Mermaid's lexers, so its range stays with the diagram; the diagram's parser+ * finds the content on the text with every statement blanked, which the diagram grammars do not+ * know.*/-function textAroundAccessibility(diagram: SyntaxNode) {+function textAroundAccessibility(diagram: SyntaxNode, input: Input, parser: LRParser) {+ const statements: SyntaxNode[] = [];+ for (let child = diagram.firstChild; child; child = child.nextSibling) statements.push(child);+ if (statements.length === 0) return null;++ let text = input.read(diagram.from, diagram.to);+ for (const { from, to } of statements) {+ const start = from - diagram.from;+ const end = to - diagram.from;+ const spaces = text.slice(start, end).replace(/[^\n]/g, " ");+ text = text.slice(0, start) + spaces + text.slice(end);+ }+ const blanked = parser.parse(text);const ranges: { from: number; to: number }[] = [];let from = diagram.from;- for (let child = diagram.firstChild; child; child = child.nextSibling) {- ranges.push({ from, to: child.from });- from = child.to;+ for (const statement of statements) {+ const start = statement.from - diagram.from;+ const lineStart = text.lastIndexOf("\n", start) + 1;+ if (isMultilineContent(blanked.resolveInner(start, 1), lineStart)) continue;+ ranges.push({ from, to: statement.from });+ from = statement.to;}if (from === diagram.from) return null;if (diagram.to > from) ranges.push({ from, to: diagram.to });return ranges;}++/**+ * Whether `node`, which starts before `lineStart` when it continues an earlier line, is content:+ * a token such as a multi-line label or string, or a body whose lines Mermaid reads as class+ * members or states. Other bodies, such as a C4 boundary, hold statements.+ */+function isMultilineContent(node: SyntaxNode, lineStart: number) {+ if (node.from >= lineStart || node.type.isError || node.type.isTop) return false;+ return !node.firstChild || CONTENT_BODIES.has(node.name);+}diff --git a/packages/codemirror/src/languages/mermaid/mermaid.spec.ts b/packages/codemirror/src/languages/mermaid/mermaid.spec.tsindex 02d73d4992..7d0b2550b4 100644--- a/packages/codemirror/src/languages/mermaid/mermaid.spec.ts+++ b/packages/codemirror/src/languages/mermaid/mermaid.spec.ts@@ -157,6 +157,31 @@ describe("accessibility lines", () => {.toBe(mermaidLanguage.parser.parse(blanked).toString());expect(mounted?.tree.toString()).not.toContain("⚠");});++ it.each([+ "flowchart TD\n A[\"hello\naccDescr: world\nend\"] --> B\n accTitle: real",+ "classDiagram\n class A {\n accDescr: world\n }\n accTitle: real",+ "stateDiagram-v2\n state S {\n accDescr: world\n A --> B\n }\n accTitle: real"+ ])("leaves the lines inside a string or body to the diagram in %j", (doc) => {+ const highlighted = styles(doc);+ expect(highlighted.filter((style) => style.includes("accDescr")))+ .not.toContainEqual(expect.stringMatching(/^accDescr tok-keyword$/));+ expect(highlighted).toContain("accTitle tok-keyword");+ expect(highlighted).toContain("real tok-string");+ });++ it("keeps the statements in a body that holds statements, as in a C4 boundary", () => {+ const doc = "C4Context\n Boundary(b, \"B\") {\n Person(a, \"A\")\n"+ + " accDescr: world\n }";+ expect(styles(doc)).toContain("accDescr tok-keyword");+ });++ it("mounts the diagram plainly when every statement is content", () => {+ const doc = "classDiagram\n class A {\n accDescr: world\n }";+ // A plain mount, unlike an overlay, shows the diagram's own tree in place of the node.+ expect(mermaidLanguage.parser.parse(doc).toString()).toBe("MermaidDiagram(ClassDiagram("+ + "DiagramName,ClassDef(Class,ClassName,ClassBody(\"{\",Member,\"}\"))))");+ });});describe("shape metadata", () => {