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(text): Mermaid errors not shown when editing an existing diagram
Elian Doran8cc5efcfa119
diff --git a/apps/client/src/services/content_renderer_text.spec.ts b/apps/client/src/services/content_renderer_text.spec.tsindex 67ccae6192..72c4ed1e77 100644--- a/apps/client/src/services/content_renderer_text.spec.ts+++ b/apps/client/src/services/content_renderer_text.spec.ts@@ -493,21 +493,28 @@ describe("applyInlineMermaid", () => {error.mockRestore();});- it("keeps the previous render as placeholder instead of an error when a re-render fails", async () => {+ it("surfaces the error on a failed re-render even when a previous render exists", async () => {const error = vi.spyOn(console, "error").mockImplementation(() => {});const container = makeContainer(["graph V1"]);await applyInlineMermaid(container);+ // Break the source: the stale render must not mask the failure — otherwise+ // the diagram looks fine until a full refresh.const node = container.querySelector("div.mermaid-diagram") as HTMLElement;- const previousSvg = node.innerHTML;node.textContent = "graph BROKEN";mermaidRenderImpl = () => {throw new Error("still broken");};await applyInlineMermaid(container);- // A prior good render is showing, so keep it rather than flashing an error.+ expect(node.classList.contains("mermaid-error")).toBe(true);+ expect(node.textContent).toContain("still broken");++ // Recovery: fixing the source clears the error state and renders cleanly.+ node.textContent = "graph FIXED";+ mermaidRenderImpl = (_id, source) => ({ svg: `<svg>${source}</svg>` });+ await applyInlineMermaid(container);expect(node.classList.contains("mermaid-error")).toBe(false);- expect(node.innerHTML).toBe(previousSvg);+ expect(node.innerHTML).toBe("<svg>graph FIXED</svg>");error.mockRestore();});});diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.tsindex 3570e51981..f1b5d32081 100644--- a/apps/client/src/services/content_renderer_text.ts+++ b/apps/client/src/services/content_renderer_text.ts@@ -153,7 +153,7 @@ export async function applyInlineMermaid(container: HTMLDivElement) {// paint the previous SVG (by position) as a placeholder and queue a// re-render. This way the user keeps seeing the old diagram until mermaid// has finished producing the new one.- const pending: Array<{ visible: HTMLElement; source: string; hasPlaceholder: boolean }> = [];+ const pending: Array<{ visible: HTMLElement; source: string }> = [];const seenSources = new Set<string>();for (const [ index, node ] of nodes.entries()) {/* v8 ignore next -- defensive fallback: textContent on an HTMLElement is always a string */@@ -164,11 +164,12 @@ export async function applyInlineMermaid(container: HTMLDivElement) {if (cached) {node.innerHTML = cached;node.setAttribute("data-processed", "true");+ node.classList.remove("mermaid-error");continue;}+ pending.push({ visible: node, source });const placeholder = lastRendered[index];- pending.push({ visible: node, source, hasPlaceholder: Boolean(placeholder) });if (placeholder) {node.innerHTML = placeholder;}@@ -193,22 +194,21 @@ export async function applyInlineMermaid(container: HTMLDivElement) {// inside a collapsed offscreen container silently broke measurement-sensitive// diagrams (e.g. gantt). Each diagram renders independently so one failure// surfaces its own error instead of blanking the diagrams beside it.- for (const { visible, source, hasPlaceholder } of pending) {+ for (const { visible, source } of pending) {try {await loadElkIfNeeded(mermaid, source);const { svg } = await mermaid.render(`mermaid-inline-${mermaidRenderId++}`, source);const processed = postprocessMermaidSvg(svg);visible.innerHTML = processed;visible.setAttribute("data-processed", "true");+ visible.classList.remove("mermaid-error");cache.set(source, processed);} catch (e) {console.error(e);- // Keep a prior good render (shown as placeholder) rather than flashing- // an error mid-edit; otherwise surface the failure so it isn't silently- // swallowed (e.g. an invalid diagram in a chat reply).- if (!hasPlaceholder) {- showMermaidError(visible, e);- }+ // Surface the failure in place, replacing any placeholder from a prior+ // good render. A broken diagram must look broken — keeping the stale+ // render would hide the error until a full refresh.+ showMermaidError(visible, e);}}@@ -218,6 +218,7 @@ export async function applyInlineMermaid(container: HTMLDivElement) {/** Render a mermaid failure in place so it's visible instead of console-only. */function showMermaidError(node: HTMLElement, error: unknown) {const message = error instanceof Error ? error.message : String(error);+ node.removeAttribute("data-processed");node.classList.add("mermaid-error");node.textContent = t("content_renderer.mermaid_diagram_error", { error: message });}