Skip to content

Build your personal knowledge base with Trilium Notes

fix(export/markdown): math expressions not working due to string escaping

Elian Doran7293f59a80e7
diff --git a/docs/User Guide/User Guide/Advanced Usage/Note ID.md b/docs/User Guide/User Guide/Advanced Usage/Note ID.md
index 5c0c8949b6..f8f0dcd6c8 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Note ID.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Note ID.md
@@ -11,6 +11,6 @@ When notes are exported, their note ID is kept in the metadata of the export. Ho
Since the Note ID is a fixed-width randomly generated number, due to the [pigeonhole principle](https://en.wikipedia.org/wiki/Pigeonhole_principle), there is a possibility that a newly created note will have the same ID as an existing note.
-Since the note ID is alphanumeric and the length is 12 we have \\(62^{12}\\) unique IDs. However since we are generating them randomly, we can use a collision calculator such as the one for [Nano ID](https://alex7kom.github.io/nano-nanoid-cc/?alphabet=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&size=12&speed=1000&speedUnit=hour) to determine that we'd need to create 1000 notes per hour every hour for 9 centuries in order to have at least 1% probability of a note collision.
+Since the note ID is alphanumeric and the length is 12 we have $62^{12}$ unique IDs. However since we are generating them randomly, we can use a collision calculator such as the one for [Nano ID](https://alex7kom.github.io/nano-nanoid-cc/?alphabet=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&size=12&speed=1000&speedUnit=hour) to determine that we'd need to create 1000 notes per hour every hour for 9 centuries in order to have at least 1% probability of a note collision.
As such, Trilium does not take any explicit action against potential note collisions, similar to other software that makes uses of unique hashes such as [Git](https://stackoverflow.com/questions/10434326/hash-collision-in-git). If one would theoretically occur, what would most likely happen is that the existing note will be replaced by the new one.
\ No newline at end of file
diff --git a/src/services/export/markdown.spec.ts b/src/services/export/markdown.spec.ts
index 24aa77da20..077d91b412 100644
--- a/src/services/export/markdown.spec.ts
+++ b/src/services/export/markdown.spec.ts
@@ -280,13 +280,13 @@ 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 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}$.`;
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 html = /*html*/`<span class="math-tex">\\[\sqrt{x^{2}+1}\\]</span>`;
const expected = `$$\sqrt{x^{2}+1}$$`;
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
diff --git a/src/services/export/markdown.ts b/src/services/export/markdown.ts
index 731024435c..2fc4fe468b 100644
--- a/src/services/export/markdown.ts
+++ b/src/services/export/markdown.ts
@@ -215,13 +215,13 @@ function buildMathFilter(): Rule {
},
replacement(content) {
// Inline math
- if (content.startsWith("(") && content.endsWith(")")) {
- return `$${content.substring(1, content.length - 1)}$`;
+ if (content.startsWith("\\\\(") && content.endsWith("\\\\)")) {
+ return `$${content.substring(3, content.length - 3)}$`;
}
// Display math
- if (content.startsWith("\\[") && content.endsWith("\\]")) {
- return `$$${content.substring(2, content.length - 2)}$$`;
+ if (content.startsWith(String.raw`\\\[`) && content.endsWith(String.raw`\\\]`)) {
+ return `$$${content.substring(4, content.length - 4)}$$`;
}
// Unknown.