Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(export/markdown): export in-line math properly
Elian Doran894cfe4f7a95
diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.tsindex 2a4e5fbf17..2726481af3 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*/`<p>The equation is <span class="math-tex">\(e=mc^{2}\)</span>.</p>`;+ 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.tsindex fd125d0f66..cbc4809845 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.jsfunction cleanAttribute(attribute: string | null | undefined) {