Skip to content

Build your personal knowledge base with Trilium Notes

feat(test): add test for mermaid linter

Elian Doran5d8ac3b9f729
diff --git a/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts b/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts
new file mode 100644
index 0000000000..1d0808f30e
--- /dev/null
+++ b/src/public/app/widgets/type_widgets/linters/mermaid.spec.ts
@@ -0,0 +1,42 @@
+import { describe, expect, it, vi } from "vitest";
+import { trimIndentation } from "../../../../../../spec/support/utils.js";
+import { validateMermaid } from "./mermaid.js";
+
+describe("Mermaid linter", () => {
+
+ (global as any).CodeMirror = {
+ Pos(line: number, col: number) {
+ return { line, col };
+ }
+ };
+
+ it("reports correctly bad diagram type", async () => {
+ const input = trimIndentation`\
+ stateDiagram-v23
+ [*] -> Still
+ `;
+
+ const result = await validateMermaid(input);
+ expect(result.length).toBe(1);
+ expect(result[0].message).toSatisfy((v: string) => v.includes("Expecting 'SPACE'"));
+ expect(result[0]).toMatchObject({
+ from: { line: 0, col: 0 },
+ to: { line: 0, col: 0 }
+ });
+ });
+
+ it("reports correctly basic arrow missing in diagram", async () => {
+ const input = trimIndentation`\
+ stateDiagram-v2
+ [*] -> Still
+ `;
+
+ const result = await validateMermaid(input);
+ expect(result.length).toBe(1);
+ expect(result[0].message).toSatisfy((v: string) => v.includes("Expecting 'SPACE'"));
+ expect(result[0]).toMatchObject({
+ from: { line: 1, col: 0 },
+ to: { line: 1, col: 3 }
+ });
+ });
+});
diff --git a/src/public/app/widgets/type_widgets/linters/mermaid.ts b/src/public/app/widgets/type_widgets/linters/mermaid.ts
index 85b11a352d..0df2ee8244 100644
--- a/src/public/app/widgets/type_widgets/linters/mermaid.ts
+++ b/src/public/app/widgets/type_widgets/linters/mermaid.ts
@@ -16,28 +16,38 @@ interface MermaidParseError extends Error {
}
export default function registerErrorReporter() {
- CodeMirror.registerHelper("lint", null, (async (text, options) => {
- if (!text.trim()) {
- return [];
- }
+ CodeMirror.registerHelper("lint", null, validateMermaid);
+}
+
+export async function validateMermaid(text: string) {
+ if (!text.trim()) {
+ return [];
+ }
- try {
- await mermaid.parse(text);
- } catch (e: unknown) {
- console.warn("Got validation error", JSON.stringify(e));
-
- const mermaidError = (e as MermaidParseError);
- const loc = mermaidError.hash.loc;
- return [
- {
- message: mermaidError.message,
- severity: "error",
- from: CodeMirror.Pos(loc.first_line - 1, loc.first_column - 1),
- to: CodeMirror.Pos(loc.last_line - 1, loc.last_column - 1)
- }
- ];
+ try {
+ await mermaid.parse(text);
+ } catch (e: unknown) {
+ console.warn("Got validation error", JSON.stringify(e));
+
+ const mermaidError = (e as MermaidParseError);
+ const loc = mermaidError.hash.loc;
+ let firstCol = loc.first_column;
+ let lastCol = loc.last_column;
+
+ if (firstCol !== 0 && lastCol !== 0) {
+ firstCol = lastCol + 1;
+ lastCol = firstCol + 1;
}
- return [];
- }));
+ return [
+ {
+ message: mermaidError.message,
+ severity: "error",
+ from: CodeMirror.Pos(loc.first_line - 1, firstCol),
+ to: CodeMirror.Pos(loc.last_line - 1, lastCol)
+ }
+ ];
+ }
+
+ return [];
}