feat(import/markdown): import in-display math properly

This commit is contained in:
Elian Doran 2025-04-05 09:57:44 +03:00
parent fc4eb13e8d
commit 07b5cd3b05
No known key found for this signature in database
2 changed files with 19 additions and 1 deletions

View File

@ -163,4 +163,16 @@ second line 2</code></pre><ul><li>Hello</li><li>world</li></ul><ol><li>Hello</li
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
it("converts inline math expressions into Mathtex format", () => {
const input = `The equation is\u00a0$e=mc^{2}$.`;
const expected = /*html*/`<p>The equation is&nbsp;<span class="math-tex">\(e=mc^{2}\)</span>.</p>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
it("converts display math expressions into Mathtex format", () => {
const input = `$$\sqrt{x^{2}+1}$$`;
const expected = /*html*/`<p><span class="math-tex">\\[\sqrt{x^{2}+1}\\]</span></p>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
});

View File

@ -12,7 +12,13 @@ class CustomMarkdownRenderer extends Renderer {
}
paragraph(data: Tokens.Paragraph): string {
return super.paragraph(data).trimEnd();
let text = super.paragraph(data).trimEnd();
// Display math
text = text.replaceAll(/\$\$(.+)\$\$/g,
`<span class="math-tex">\\\[$1\\\]</span>`);
return text;
}
code({ text, lang }: Tokens.Code): string {