Skip to content

Build your personal knowledge base with Trilium Notes

fix(export/markdown): double slashes breaking math expressions (closes #1649)

Elian Doran3a1f1ceedb0e
diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts
index 524e4eb693..74c6a96001 100644
--- a/src/services/export/markdown.spec.ts
+++ b/src/services/export/markdown.spec.ts
@@ -278,14 +278,14 @@ describe("Markdown export", () => {
});
it("converts inline math expressions into proper Markdown syntax", () => {
- const html = /*html*/`<p>The equation is&nbsp;<span class="math-tex">\\(e=mc^{2}\\)</span>.</p>`;
- const expected = `The equation is\u00a0$e=mc^{2}$.`;
+ const html = /*html*/String.raw`<span class="math-tex">\(H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}\)</span></span>`;
+ const expected = String.raw`$H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}$`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
it("converts display math expressions into proper Markdown syntax", () => {
- const html = /*html*/`<span class="math-tex">\\[\sqrt{x^{2}+1}\\]</span>`;
- const expected = `$$\sqrt{x^{2}+1}$$`;
+ const html = /*html*/String.raw`<span class="math-tex">\[H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}\]</span></span>`;
+ const expected = String.raw`$$H(X, Y) = \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 \frac{1}{p(x_i, y_j)} = - \sum_{i=1}^{M} \sum_{j=1}^{L} p(x_i, y_j) \log_2 p(x_i, y_j) \frac{\text{bits}}{\text{symbol}}$$`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
diff --git a/src/services/export/markdown.ts b/src/services/export/markdown.ts
index 2b066caa79..e524a62749 100644
--- a/src/services/export/markdown.ts
+++ b/src/services/export/markdown.ts
@@ -208,19 +208,28 @@ function buildFigureFilter(): Rule {
}
function buildMathFilter(): Rule {
+ const MATH_INLINE_PREFIX = "\\(";
+ const MATH_INLINE_SUFFIX = "\\)";
+
+ const MATH_DISPLAY_PREFIX = "\\[";
+ const MATH_DISPLAY_SUFFIX = "\\]";
+
return {
filter(node) {
return node.nodeName === "SPAN" && node.classList.contains("math-tex");
},
- replacement(content) {
+ replacement(_, node) {
+ // We have to use the raw HTML text, otherwise the content is escaped too much.
+ const content = (node as HTMLElement).innerText;
+
// Inline math
- if (content.startsWith("\\\\(") && content.endsWith("\\\\)")) {
- return `$${content.substring(3, content.length - 3)}$`;
+ if (content.startsWith(MATH_INLINE_PREFIX) && content.endsWith(MATH_INLINE_SUFFIX)) {
+ return `$${content.substring(MATH_INLINE_PREFIX.length, content.length - MATH_INLINE_SUFFIX.length)}$`;
}
// Display math
- if (content.startsWith(String.raw`\\\[`) && content.endsWith(String.raw`\\\]`)) {
- return `$$${content.substring(4, content.length - 4)}$$`;
+ if (content.startsWith(MATH_DISPLAY_PREFIX) && content.endsWith(MATH_DISPLAY_SUFFIX)) {
+ return `$$${content.substring(MATH_DISPLAY_PREFIX.length, content.length - MATH_DISPLAY_SUFFIX.length)}$$`;
}
// Unknown.