Skip to content

Build your personal knowledge base with Trilium Notes

fix(mermaid): nbsp entity causing issues (closes #9413)

Elian Doran9fc750533c73
diff --git a/apps/client/src/services/mermaid.spec.ts b/apps/client/src/services/mermaid.spec.ts
index 423c8de5b2..61461065b7 100644
--- a/apps/client/src/services/mermaid.spec.ts
+++ b/apps/client/src/services/mermaid.spec.ts
@@ -32,4 +32,9 @@ describe("Mermaid", () => {
`;
expect(postprocessMermaidSvg(before)).toBe(after);
});
+
+ it("replaces   with numeric entity for valid XML", () => {
+ expect(postprocessMermaidSvg("<text>a&nbsp;b&nbsp;&nbsp;c</text>"))
+ .toBe("<text>a&#160;b&#160;&#160;c</text>");
+ });
});
diff --git a/apps/client/src/services/mermaid.ts b/apps/client/src/services/mermaid.ts
index 7074017222..35d2555f8d 100644
--- a/apps/client/src/services/mermaid.ts
+++ b/apps/client/src/services/mermaid.ts
@@ -49,11 +49,15 @@ export async function loadElkIfNeeded(mermaid: Mermaid, mermaidContent: string)
* Processes the output of a Mermaid SVG render before it should be delivered to the user.
*
* <p>
- * Currently this fixes <br> to <br/> which would otherwise cause an invalid XML.
+ * Currently this fixes <br> to <br/> and replaces named HTML entities like &nbsp; with their
+ * numeric equivalents, both of which would otherwise cause invalid XML when the SVG is saved
+ * as an attachment.
*
* @param svg the Mermaid SVG to process.
* @returns the processed SVG.
*/
export function postprocessMermaidSvg(svg: string) {
- return svg.replaceAll(/<br\s*>/ig, "<br/>");
+ return svg
+ .replaceAll(/<br\s*>/ig, "<br/>")
+ .replaceAll(/&nbsp;/g, "&#160;");
}