diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts index 2a4e5fbf1..2726481af 100644 --- a/src/services/export/markdown.spec.ts +++ b/src/services/export/markdown.spec.ts @@ -279,4 +279,10 @@ describe("Markdown export", () => { expect(markdownExportService.toMarkdown(html)).toBe(expected); }); + it("converts inline math expressions to proper Markdown syntax", () => { + const html = /*html*/`
The equation is \(e=mc^{2}\).
`; + const expected = `The equation is\u00a0$e=mc^{2}$.`; + expect(markdownExportService.toMarkdown(html)).toBe(expected); + }); + }); diff --git a/src/services/export/markdown.ts b/src/services/export/markdown.ts index fd125d0f6..cbc480984 100644 --- a/src/services/export/markdown.ts +++ b/src/services/export/markdown.ts @@ -46,6 +46,7 @@ function toMarkdown(content: string) { instance.addRule("admonition", buildAdmonitionFilter()); instance.addRule("inlineLink", buildInlineLinkFilter()); instance.addRule("figure", buildFigureFilter()); + instance.addRule("math", buildMathFilter()); instance.use(gfm); instance.keep([ "kbd" ]); } @@ -207,6 +208,23 @@ function buildFigureFilter(): Rule { } } +function buildMathFilter(): Rule { + return { + filter(node) { + return node.nodeName === "SPAN" && node.classList.contains("math-tex"); + }, + replacement(content) { + // Inline math + if (content.startsWith("(") && content.endsWith(")")) { + return `$${content.substring(1, content.length - 1)}$`; + } + + // Unknown. + return content; + } + } +} + // Taken from upstream since it's not exposed. // https://github.com/mixmark-io/turndown/blob/master/src/commonmark-rules.js function cleanAttribute(attribute: string | null | undefined) {