Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): parse comments and directives in Mermaid mindmaps

Elian Doranbcc3675984d3
diff --git a/packages/codemirror/src/languages/mermaid/parsers/mermaid/tokens.ts b/packages/codemirror/src/languages/mermaid/parsers/mermaid/tokens.ts
index 13b3a314f9..fe5228a4f3 100644
--- a/packages/codemirror/src/languages/mermaid/parsers/mermaid/tokens.ts
+++ b/packages/codemirror/src/languages/mermaid/parsers/mermaid/tokens.ts
@@ -132,7 +132,7 @@ export const diagramText = new ExternalTokenizer((input) => {
* The length of the `%%{…}%%` directive at the stream position, which can span lines; -1 when it
* has no closing `}%%`.
*/
-function findDirectiveLength(input: InputStream) {
+export function findDirectiveLength(input: InputStream) {
for (let offset = 3; input.peek(offset) !== -1; offset++) {
const closes = input.peek(offset) === 125 && input.peek(offset + 1) === 37
&& input.peek(offset + 2) === 37;
@@ -142,7 +142,7 @@ function findDirectiveLength(input: InputStream) {
}
/** The length of the rest of the line at the stream position, newline excluded. */
-function lineLength(input: InputStream) {
+export function lineLength(input: InputStream) {
let length = 0;
while (input.peek(length) !== 10 && input.peek(length) !== -1) length++;
return length;
diff --git a/packages/codemirror/src/languages/mermaid/parsers/mindmap/highlight.ts b/packages/codemirror/src/languages/mermaid/parsers/mindmap/highlight.ts
index f931801993..0afb21e0b3 100644
--- a/packages/codemirror/src/languages/mermaid/parsers/mindmap/highlight.ts
+++ b/packages/codemirror/src/languages/mermaid/parsers/mindmap/highlight.ts
@@ -3,6 +3,8 @@ import { mindmapTags } from "../../tags/index.js";
export const mindmapHighlighting = styleTags({
DiagramName: mindmapTags.diagramName,
+ Directive: mindmapTags.directive,
+ LineComment: mindmapTags.lineComment,
LineText1: mindmapTags.lineText1,
LineText2: mindmapTags.lineText2,
LineText3: mindmapTags.lineText3,
diff --git a/packages/codemirror/src/languages/mermaid/parsers/mindmap/mindmap.grammar b/packages/codemirror/src/languages/mermaid/parsers/mindmap/mindmap.grammar
index 3c0dbfd8f0..6f4c40cd19 100644
--- a/packages/codemirror/src/languages/mermaid/parsers/mindmap/mindmap.grammar
+++ b/packages/codemirror/src/languages/mermaid/parsers/mindmap/mindmap.grammar
@@ -3,7 +3,7 @@
DiagramName Line*
}
-@skip { spaces | newlineEmpty }
+@skip { spaces | newlineEmpty | LineComment | Directive }
lineText {
LineText1 |
@@ -74,6 +74,7 @@ kw<term> { @specialize<word, term> }
@context trackIndent from "./tokens.js"
@external tokens indentation from "./tokens.js" { indent }
+@external tokens comments from "./tokens.js" { LineComment, Directive }
@external tokens lineTextType from "./tokens.js" {
LineText1,
LineText2,
diff --git a/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.terms.ts b/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.terms.ts
index 2a86594311..ef25829404 100644
--- a/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.terms.ts
+++ b/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.terms.ts
@@ -1,18 +1,20 @@
// AUTO-GENERATED by scripts/build_mermaid_grammars.mts from the grammar in this directory.
// This file was generated by lezer-generator. You probably shouldn't edit it.
export const
- indent = 16,
- LineText1 = 1,
- LineText2 = 2,
- LineText3 = 3,
- LineText4 = 4,
- LineText5 = 5,
- newline = 17,
- newlineEmpty = 18,
- MindmapDiagram = 6,
- DiagramName = 7,
- Line = 8,
- IconLine = 9,
- Icon = 10,
- ClassLine = 11,
- ShapedText = 12
+ indent = 18,
+ LineComment = 1,
+ Directive = 2,
+ LineText1 = 3,
+ LineText2 = 4,
+ LineText3 = 5,
+ LineText4 = 6,
+ LineText5 = 7,
+ newline = 19,
+ newlineEmpty = 20,
+ MindmapDiagram = 8,
+ DiagramName = 9,
+ Line = 10,
+ IconLine = 11,
+ Icon = 12,
+ ClassLine = 13,
+ ShapedText = 14
diff --git a/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.ts b/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.ts
index 0f6f103613..207d141165 100644
--- a/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.ts
+++ b/packages/codemirror/src/languages/mermaid/parsers/mindmap/parser.ts
@@ -1,23 +1,23 @@
// AUTO-GENERATED by scripts/build_mermaid_grammars.mts from the grammar in this directory.
// This file was generated by lezer-generator. You probably shouldn't edit it.
import {LRParser} from "@lezer/lr"
-import {indentation, lineTextType, newlines, trackIndent} from "./tokens.js"
+import {indentation, comments, lineTextType, newlines, trackIndent} from "./tokens.js"
import {mindmapHighlighting} from "./highlight.js"
-const spec_word = {__proto__:null,mindmap:44, icon:50}
+const spec_word = {__proto__:null,mindmap:48, icon:54}
export const parser = LRParser.deserialize({
version: 14,
- states: "&fOYQ[OOOOQW'#Ci'#CiQbQ[OOQgQ[OOOOQW'#Cc'#CcOOQW-E6g-E6gOlQ]O'#CdOOQW'#Cj'#CjQgQ[OOO!]Q^O,59OOOQW-E6h-E6hOOQW'#Cs'#CsO!vQ[O'#CeO!{Q^O'#CgO!{Q^O'#CyO!{Q^O'#C|O!{Q^O'#C}O!{Q^O'#DQO!{Q^O'#DRO!{Q^O'#DSOOQW'#Ch'#ChO#^Q[O1G.jOOQW1G.j1G.jO#hQ[O,59POOQW'#Cf'#CfOOQW,59R,59RO#mQ[O,59eO#rQ[O,59hO#wQ[O,59iO#|Q[O,59lO$RQ[O,59mO$WQ[O,59nOOQW7+$U7+$UO!{Q^O1G.kOOQW1G/P1G/POOQW1G/S1G/SOOQW1G/T1G/TOOQW1G/W1G/WOOQW1G/X1G/XOOQW1G/Y1G/YO$]Q[O7+$VOOQW<<Gq<<Gq",
- stateData: "$b~OdOSbOS~OaPOfSO~OaPO~OaUO~O`XO_WXaWX~Oj_OkbOn^Or`OsaOwcO~OPZOQZORZOSZOTZOh[Ol]O~PwOihO~OPZOQZORZOSZOTZO~O_WiaWi~PwOjqO~OorO~OksO~OstO~OruO~OjvO~OxwO~OkyO~O",
- goto: "#YwPPPPPPPx{!P!S!P!V!]!cPPPPPPPP!iPPPPP#UPP#U#UPP#U#U#URROTVRWRfXRg[QfXRpeQQORTQQWRRYWQeXQi]Qj^Qk_Ql`QmaQnbQocRxqTdXe",
- nodeNames: "⚠ LineText1 LineText2 LineText3 LineText4 LineText5 MindmapDiagram DiagramName Line IconLine Icon ClassLine ShapedText",
- maxTerm: 40,
+ states: "&fO`QjOOOOQb'#Ck'#CkQhQjOOQmQjOOOOQb'#Ce'#CeOOQb-E6i-E6iOrQkO'#CfOOQb'#Cl'#ClQmQjOOO!cQnO,59QOOQb-E6j-E6jOOQb'#Cu'#CuO!|QjO'#CgO#RQnO'#CiO#RQnO'#C{O#RQnO'#DOO#RQnO'#DPO#RQnO'#DSO#RQnO'#DTO#RQnO'#DUOOQb'#Cj'#CjO#dQjO1G.lOOQb1G.l1G.lO#nQjO,59ROOQb'#Ch'#ChOOQb,59T,59TO#sQjO,59gO#xQjO,59jO#}QjO,59kO$SQjO,59nO$XQjO,59oO$^QjO,59pOOQb7+$W7+$WO#RQnO1G.mOOQb1G/R1G/ROOQb1G/U1G/UOOQb1G/V1G/VOOQb1G/Y1G/YOOQb1G/Z1G/ZOOQb1G/[1G/[O$cQjO7+$XOOQb<<Gs<<Gs",
+ stateData: "$h~OfOSdOSPOSQOS~OcPOhSO~OcPO~OcUO~ObXOaYXcYX~Ol_OmbOp^Ot`OuaOycO~ORZOSZOTZOUZOVZOj[On]O~P}OkhO~ORZOSZOTZOUZOVZO~OaYicYi~P}OlqO~OqrO~OmsO~OutO~OtuO~OlvO~OzwO~OmyO~O",
+ goto: "#[yPPPPPPPPPz}!R!U!R!X!_!ePPPPPPPP!kPPPPP#WPP#W#WPP#W#W#WRROTVRWRfXRg[QfXRpeQQORTQQWRRYWQeXQi]Qj^Qk_Ql`QmaQnbQocRxqTdXe",
+ nodeNames: "⚠ LineComment Directive LineText1 LineText2 LineText3 LineText4 LineText5 MindmapDiagram DiagramName Line IconLine Icon ClassLine ShapedText",
+ maxTerm: 42,
context: trackIndent,
propSources: [mindmapHighlighting],
- skippedNodes: [0],
+ skippedNodes: [0,1,2],
repeatNodeCount: 2,
- tokenData: "$b~R]XYz[]zpqzxy!fyz!s![!]#Q!c!}#e!}#O#p#O#P!]#P#Q#u#T#o#e#o#p#z#q#r$V~!PSd~XYz[]zpqz#O#P!]~!`QYZz]^z~!kPj~xy!n~!sOr~~!xPk~yz!{~#QOs~~#TP![!]#W~#]Ph~![!]#`~#eOl~~#jQe~!c!}#e#T#o#e~#uOn~~#zOo~~#}P#o#p$Q~$VOw~~$YP#q#r$]~$bOx~",
- tokenizers: [indentation, lineTextType, 0, newlines],
- topRules: {"MindmapDiagram":[0,6]},
- specialized: [{term: 21, get: (value: keyof typeof spec_word) => spec_word[value] || -1}],
+ tokenData: "$b~R]XYz[]zpqzxy!fyz!s![!]#Q!c!}#e!}#O#p#O#P!]#P#Q#u#T#o#e#o#p#z#q#r$V~!PSf~XYz[]zpqz#O#P!]~!`QYZz]^z~!kPl~xy!n~!sOt~~!xPm~yz!{~#QOu~~#TP![!]#W~#]Pj~![!]#`~#eOn~~#jQg~!c!}#e#T#o#e~#uOp~~#zOq~~#}P#o#p$Q~$VOy~~$YP#q#r$]~$bOz~",
+ tokenizers: [indentation, comments, lineTextType, 0, newlines],
+ topRules: {"MindmapDiagram":[0,8]},
+ specialized: [{term: 23, get: (value: keyof typeof spec_word) => spec_word[value] || -1}],
tokenPrec: 0
})
diff --git a/packages/codemirror/src/languages/mermaid/parsers/mindmap/tokens.ts b/packages/codemirror/src/languages/mermaid/parsers/mindmap/tokens.ts
index d702fff7de..b0ef34d29d 100644
--- a/packages/codemirror/src/languages/mermaid/parsers/mindmap/tokens.ts
+++ b/packages/codemirror/src/languages/mermaid/parsers/mindmap/tokens.ts
@@ -1,5 +1,9 @@
import { ExternalTokenizer, ContextTracker } from "@lezer/lr";
+
+import { findDirectiveLength, lineLength } from "../mermaid/tokens.js";
import {
+ Directive,
+ LineComment,
newline as newlineToken,
newlineEmpty,
indent,
@@ -23,6 +27,7 @@ const newline = 10,
space = 32,
tab = 9,
hash = 35,
+ percent = 37,
colon = 58,
parenL = 40,
parenR = 41,
@@ -44,7 +49,8 @@ export const newlines = new ExternalTokenizer(
let empty =
input.next == newline ||
input.next == carriageReturn ||
- input.next == hash;
+ input.next == hash ||
+ startsComment(input);
input.acceptToken(empty ? newlineEmpty : newlineToken, -spaces);
}
},
@@ -90,7 +96,7 @@ const tabDepth = (depth: number) => {
};
// The indentation that starts a line; `trackIndent` measures its depth. A line that holds
-// nothing else, or starts with `#`, gets no `indent` token.
+// nothing else, or starts with `#` or `%%`, gets no `indent` token.
export const indentation = new ExternalTokenizer((input) => {
const prev = input.peek(-1);
if (prev !== newline && prev !== carriageReturn) return;
@@ -98,11 +104,39 @@ export const indentation = new ExternalTokenizer((input) => {
while (input.next === space || input.next === tab) {
input.advance();
}
- if (input.next !== newline && input.next !== carriageReturn && input.next !== hash) {
+ const blank = input.next === newline || input.next === carriageReturn || input.next === hash;
+ if (!blank && !startsComment(input)) {
input.acceptToken(indent);
}
});
+// A `%%` comment, on a line of its own or after a shape (`lineTextType` takes the `%%` in
+// `root %% text` as text), or a `%%{…}%%` directive, which Mermaid applies only on a line of
+// its own.
+export const comments = new ExternalTokenizer((input) => {
+ if (!startsComment(input)) return;
+
+ const directive = input.peek(2) === braceL && startsLine(input);
+ const directiveLength = directive ? findDirectiveLength(input) : -1;
+ if (directiveLength > 0) {
+ input.acceptToken(Directive, directiveLength);
+ } else {
+ input.acceptToken(LineComment, lineLength(input));
+ }
+});
+
+function startsComment(input: InputStream) {
+ return input.next === percent && input.peek(1) === percent;
+}
+
+function startsLine(input: InputStream) {
+ for (let offset = -1; ; offset--) {
+ const previous = input.peek(offset);
+ if (previous === newline || previous === -1) return true;
+ if (previous !== space && previous !== tab) return false;
+ }
+}
+
const indentTracker = {
lineType: LineText1
};
diff --git a/packages/codemirror/src/languages/mermaid/tags/index.ts b/packages/codemirror/src/languages/mermaid/tags/index.ts
index c5ff1b1cb1..455d4ec756 100644
--- a/packages/codemirror/src/languages/mermaid/tags/index.ts
+++ b/packages/codemirror/src/languages/mermaid/tags/index.ts
@@ -9,6 +9,8 @@ export const mermaidTags = {
export const mindmapTags = {
diagramName: Tag.define(mermaidTags.diagramName),
+ directive: Tag.define(mermaidTags.directive),
+ lineComment: Tag.define(t.lineComment),
lineText1: Tag.define(),
lineText2: Tag.define(),
lineText3: Tag.define(),
diff --git a/packages/codemirror/src/languages/mermaid/test/mindmap.txt b/packages/codemirror/src/languages/mermaid/test/mindmap.txt
index 9c86c2067b..f10d9c77b5 100644
--- a/packages/codemirror/src/languages/mermaid/test/mindmap.txt
+++ b/packages/codemirror/src/languages/mermaid/test/mindmap.txt
@@ -178,3 +178,73 @@ mindmap
==>
MindmapDiagram(DiagramName,Line,Line,Line)
+
+# Comment lines
+
+mindmap
+ root
+ %% a comment
+ child
+%% at column 0
+ other
+
+==>
+
+MindmapDiagram(DiagramName,Line(LineText3),LineComment,Line(LineText5),LineComment,Line(LineText5))
+
+# Directive below the root
+
+mindmap
+ root
+ %%{init: {"theme": "dark"}}%%
+ child
+
+==>
+
+MindmapDiagram(DiagramName,Line(LineText3),Directive,Line(LineText5))
+
+# Multi-line directive
+
+mindmap
+ root
+ %%{
+ init: {"theme": "dark"}
+ }%%
+ child
+
+==>
+
+MindmapDiagram(DiagramName,Line(LineText3),Directive,Line(LineText5))
+
+# Percent signs inside a node's text
+
+mindmap
+ root %% not a comment
+ child[50%% done]
+
+==>
+
+MindmapDiagram(DiagramName,Line(LineText3),Line(LineText5,ShapedText(LineText5)))
+
+# Comment after a shape
+
+mindmap
+ root
+ child[x] %% a comment
+ other[y] %%{init: {"theme": "dark"}}%%
+
+==>
+
+MindmapDiagram(DiagramName,Line(LineText3),Line(LineText5,ShapedText(LineText5)),LineComment,Line(LineText5,ShapedText(LineText5)),LineComment)
+
+# Comment after a line starting with a hash
+
+mindmap
+ root
+#x
+ %% a comment
+ child
+
+==>
+
+MindmapDiagram(DiagramName,Line(LineText3),Line,LineComment,Line(LineText5))