From 4bb767f8eec65b99066d0351824c050a5bc4b4c6 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 5 Apr 2025 10:46:33 +0300 Subject: [PATCH] fix(import/markdown): preserve escaped math expressions --- src/services/import/markdown.spec.ts | 10 ++++++++++ src/services/import/markdown.ts | 7 +++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/services/import/markdown.spec.ts b/src/services/import/markdown.spec.ts index d2d8ea694..e25a8bc09 100644 --- a/src/services/import/markdown.spec.ts +++ b/src/services/import/markdown.spec.ts @@ -175,4 +175,14 @@ second line 2
  1. Hello
  2. { + const scenarios = [ + "\\$\\$\sqrt{x^{2}+1}\\$\\$", + "The equation is \\$e=mc^{2}\\$." + ]; + for (const scenario of scenarios) { + expect(markdownService.renderToHtml(scenario, "Title")).toStrictEqual(`

    ${scenario}

    `); + } + }); + }); diff --git a/src/services/import/markdown.ts b/src/services/import/markdown.ts index 60f770bbd..209d4afe5 100644 --- a/src/services/import/markdown.ts +++ b/src/services/import/markdown.ts @@ -16,11 +16,11 @@ class CustomMarkdownRenderer extends Renderer { if (text.includes("$")) { // Display math - text = text.replaceAll(/\$\$(.+)\$\$/g, + text = text.replaceAll(/(?\\\[$1\\\]`); // Inline math - text = text.replaceAll(/\$(.+)\$/g, + text = text.replaceAll(/(?\\\($1\\\)`); } @@ -87,6 +87,9 @@ import { ADMONITION_TYPE_MAPPINGS } from "../export/markdown.js"; import utils from "../utils.js"; function renderToHtml(content: string, title: string) { + // Double-escape slashes in math expression because they are otherwise consumed by the parser somewhere. + content = content.replaceAll("\\$", "\\\\$"); + let html = parse(content, { async: false, renderer: renderer