Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): render escaped dollar as literal `$` without doubling backslashes (closes #10179)

Elian Dorane4ff9419ead4
diff --git a/packages/commons/src/lib/markdown_renderer.spec.ts b/packages/commons/src/lib/markdown_renderer.spec.ts
index d5248f236a..4681864729 100644
--- a/packages/commons/src/lib/markdown_renderer.spec.ts
+++ b/packages/commons/src/lib/markdown_renderer.spec.ts
@@ -357,6 +357,13 @@ describe("renderToHtml", () => {
expect(html).toContain('<code spellcheck="false">$x$</code>');
expect(html).toContain('<span class="math-tex">\\(y\\)</span>');
});
+
+ it("renders an escaped dollar as a single literal dollar without adding backslashes (#10179)", () => {
+ // Markdown `\$` is an escaped dollar; the backslash must be consumed, leaving
+ // just `$`. It must not gain extra backslashes (the reported `\\$` output).
+ expect(render("\\$")).toBe("<p>$</p>");
+ expect(render("price is \\$5 today")).toBe("<p>price is $5 today</p>");
+ });
});
describe("wiki links and transclusions", () => {
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.ts
index f8f7b90b25..0a5640ed28 100644
--- a/packages/commons/src/lib/markdown_renderer.ts
+++ b/packages/commons/src/lib/markdown_renderer.ts
@@ -372,9 +372,6 @@ export class CustomMarkdownRenderer extends Renderer {
* delegated to the caller so this works in both Node and the browser.
*/
export function renderToHtml(content: string, title: string, options: RenderToHtmlOptions): string {
- // Double-escape slashes in math expressions — otherwise the parser consumes them.
- content = content.replaceAll("\\$", "\\\\$");
-
const { processedText, placeholderMap: formulaMap } = extractFormulas(content);
const marked = new Marked({ async: false, gfm: true });
diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.ts
index b88ad832eb..6e65bf35f7 100644
--- a/packages/trilium-core/src/services/import/markdown.spec.ts
+++ b/packages/trilium-core/src/services/import/markdown.spec.ts
@@ -237,13 +237,18 @@ $$`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
- it("preserves escaped math expressions", () => {
- const scenarios = [
- "\\$\\$\sqrt{x^{2}+1}\\$\\$",
- "The equation is \\$e=mc^{2}\\$."
+ it("renders escaped math delimiters as literal dollars without rendering math (#10179)", () => {
+ // `\$` is a standard Markdown escape: the backslash is consumed and the dollar
+ // is kept literal (never treated as a formula delimiter). The escape must NOT
+ // leak extra backslashes into the output — the reported bug doubled them to `\\$`.
+ const scenarios: [input: string, expected: string][] = [
+ ["\\$\\$\sqrt{x^{2}+1}\\$\\$", `<p>$$\sqrt{x^{2}+1}$$</p>`],
+ ["The equation is \\$e=mc^{2}\\$.", `<p>The equation is $e=mc^{2}$.</p>`]
];
- for (const scenario of scenarios) {
- expect(markdownService.renderToHtml(scenario, "Title")).toStrictEqual(`<p>${scenario}</p>`);
+ for (const [input, expected] of scenarios) {
+ const html = markdownService.renderToHtml(input, "Title");
+ expect(html).toStrictEqual(expected);
+ expect(html).not.toContain("math-tex");
}
});