mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 20:19:05 +01:00
feat(export/markdown): export in-line math properly
This commit is contained in:
parent
173fa36fca
commit
894cfe4f7a
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user