Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): shield blockquoted code fences from formula extraction (closes #10268)

Elian Doranf50f3a3123f9
diff --git a/packages/commons/src/lib/markdown_renderer.spec.ts b/packages/commons/src/lib/markdown_renderer.spec.ts
index c4a0dddf5b..84d94771b6 100644
--- a/packages/commons/src/lib/markdown_renderer.spec.ts
+++ b/packages/commons/src/lib/markdown_renderer.spec.ts
@@ -91,6 +91,26 @@ describe("extractCodeBlocks", () => {
const hasFencedBlock = values.some((v) => v.includes("print('hello')"));
expect(hasFencedBlock).toBe(true);
});
+
+ it("should extract a fenced code block nested in a blockquote, prefixes and all (#10268)", () => {
+ const input = [ "> ```", "> echo ${VAR} ${VAR2}", "> ```" ].join("\n");
+ const { processedText, placeholderMap } = extractCodeBlocks(input);
+
+ expect(placeholderMap.size).toBe(1);
+ expect(processedText).not.toContain("```");
+ // The whole block, including the `> ` prefixes, is captured verbatim so the
+ // blockquote still renders after restoration.
+ expect([...placeholderMap.values()][0]).toBe(input);
+ });
+
+ it("should extract a fenced code block in a doubly-nested blockquote", () => {
+ const input = [ "> > ```", "> > $ $", "> > ```" ].join("\n");
+ const { processedText, placeholderMap } = extractCodeBlocks(input);
+
+ expect(placeholderMap.size).toBe(1);
+ expect(processedText).not.toContain("```");
+ expect([...placeholderMap.values()][0]).toBe(input);
+ });
});
describe("renderToHtml", () => {
@@ -401,6 +421,16 @@ describe("renderToHtml", () => {
expect(render("$$$x$$")).toBe("<p>$$$x$$</p>");
expect(render("$$e=mc^2$")).not.toContain("math-tex");
});
+
+ it("does not treat dollars in a blockquoted code block as formulas (#10268)", () => {
+ // The fence lines carry a `> ` prefix, so the code block must still be shielded
+ // from formula extraction — otherwise `${VAR} ${VAR2}` is mangled into a math span.
+ const html = render([ "> ```", "> echo ${VAR} ${VAR2}", "> ```" ].join("\n"));
+ expect(html).toContain("echo ${VAR} ${VAR2}");
+ expect(html).not.toContain("math-tex");
+ expect(html).not.toContain("FORMULA");
+ expect(html).toContain("<blockquote>");
+ });
});
describe("wiki links and transclusions", () => {
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.ts
index f5d0675402..2e4d65694c 100644
--- a/packages/commons/src/lib/markdown_renderer.ts
+++ b/packages/commons/src/lib/markdown_renderer.ts
@@ -249,8 +249,13 @@ export function extractCodeBlocks(text: string): { processedText: string; placeh
let id = 0;
const timestamp = Date.now();
+ // `(?:>[ \t]*)*` allows blockquote prefixes on the fence lines so that fenced
+ // code blocks nested in a blockquote (`> ``` `) are still shielded. Otherwise their
+ // contents leak into formula extraction, which mangles `$…$` runs like `${VAR}` (#10268).
+ // The whole match is restored verbatim before marked parses, so the prefixes are
+ // preserved and the blockquote still renders correctly.
text = text
- .replace(/^[ \t]*```[^\n]*\n[\s\S]*?^[ \t]*```[ \t]*$/gm, (m) => {
+ .replace(/^[ \t]*(?:>[ \t]*)*```[^\n]*\n[\s\S]*?^[ \t]*(?:>[ \t]*)*```[ \t]*$/gm, (m) => {
const key = `<!--CODE_BLOCK_${timestamp}_${id++}-->`;
codeMap.set(key, m);
return key;