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(markdown): escape HTML chars in imported math, closes #10418
Elian Doran512b32066c67
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.tsindex b49c4b4fa6..7fde311f74 100644--- a/packages/commons/src/lib/markdown_renderer.ts+++ b/packages/commons/src/lib/markdown_renderer.ts@@ -332,12 +332,12 @@ function extractFormulas(text: string): { processedText: string; placeholderMap:let processedText = noCodeText.replace(/(?<![\\$])\$\$(?!\$)((?:(?!\n{2,})[^$])+?)\$\$(?!\$)/g, (_, formula: string) => {const key = `<!--FORMULA_BLOCK_${timestamp}_${id++}-->`;- formulaMap.set(key, `<span class="math-tex">\\[${formula}\\]</span>`);+ formulaMap.set(key, `<span class="math-tex">\\[${escapeMathFormula(formula)}\\]</span>`);return key;}).replace(/(?<![\\$])\$(?!\$)([^$\n]+?)\$(?!\$)/g, (_, formula: string) => {const key = `<!--FORMULA_INLINE_${timestamp}_${id++}-->`;- formulaMap.set(key, `<span class="math-tex">\\(${formula}\\)</span>`);+ formulaMap.set(key, `<span class="math-tex">\\(${escapeMathFormula(formula)}\\)</span>`);return key;});@@ -346,6 +346,22 @@ function extractFormulas(text: string): { processedText: string; placeholderMap:return { processedText, placeholderMap: formulaMap };}+/**+ * Escapes the HTML-significant characters (`&`, `<`, `>`) in a math formula body.+ *+ * The formula is restored into the HTML string *before* sanitization, so a raw `<`+ * (e.g. `k<K`) would be parsed as the start of an HTML tag and the sanitizer would+ * strip everything after it, truncating the equation (#10418). Escaping matches how+ * CKEditor serializes the equation (a text node, so quotes are intentionally left+ * untouched) and round-trips cleanly back into the editor.+ */+function escapeMathFormula(formula: string): string {+ return formula+ .replace(/&/g, "&")+ .replace(/</g, "<")+ .replace(/>/g, ">");+}+function restoreFromMap(text: string, map: Map<string, string>): string {if (map.size === 0) return text;const pattern = [ ...map.keys() ]diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.tsindex 96e7127097..7ecf46a5ef 100644--- a/packages/trilium-core/src/services/import/markdown.spec.ts+++ b/packages/trilium-core/src/services/import/markdown.spec.ts@@ -245,6 +245,26 @@ $$`;expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);});+ it("escapes HTML-significant characters in display math so they survive sanitization (#10418)", () => {+ // The `<` in `k<K` used to be treated as the start of an HTML tag by the+ // sanitizer, which stripped everything from `<K` onwards and truncated the+ // formula. Escaping `<`/`>`/`&` (matching how CKEditor serializes the math+ // text node) keeps the whole equation intact.+ const input = `$$+\\min_{0\\le k<K}\\|\\operatorname{grad}\\Phi(x_k)\\|^2 = O(1/K),+$$`;+ const expected = /*html*/`<span class="math-tex">\\[+\\min_{0\\le k<K}\\|\\operatorname{grad}\\Phi(x_k)\\|^2 = O(1/K),+\\]</span>`;+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ });++ it("escapes HTML-significant characters in inline math (#10418)", () => {+ const input = `Condition: $a<b$ and $c>d$ with $x \\& y$.`;+ const expected = /*html*/`<p>Condition: <span class="math-tex">\\(a<b\\)</span> and <span class="math-tex">\\(c>d\\)</span> with <span class="math-tex">\\(x \\& y\\)</span>.</p>`;+ expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);+ });+it("ignores math formulas inside code blocks and converts inline math expressions correctly", () => {const result = markdownService.renderToHtml(trimIndentation`\\`\`\`unknownlanguage