Skip to content

Build your personal knowledge base with Trilium Notes

fix(export/markdown): indent nested lists under ordered items 10 and up

Elian Doran0591905f0890
diff --git a/docs/Release Notes/Release Notes/v0.106.0.md b/docs/Release Notes/Release Notes/v0.106.0.md
index ced99b5ce2..92cb324d73 100644
--- a/docs/Release Notes/Release Notes/v0.106.0.md
+++ b/docs/Release Notes/Release Notes/v0.106.0.md
@@ -149,8 +149,8 @@ Add a `~template:newNoteDefaultParent` relation to a template note, pointing it
27. Improved the performance of batch deleting notes, including a progress report.
28. The sync button now shows a small bar to indicate the progress of the sync.
29. LLM:
- 1. Detect GitHub Copilot shims and report them properly.
- 2. More efficient Claude Code and GitHub Copilot which reduces the time and memory consumption between conversations.
+ 1. Detect GitHub Copilot shims and report them properly.
+ 2. More efficient Claude Code and GitHub Copilot which reduces the time and memory consumption between conversations.
30. Spreadsheets now print, export and share as they look in the editor, including links, rotated text and borders, by @adoriandoran
31. Icons are now vertically aligned correctly across the app, including icon packs, by @adoriandoran
32. Insert date/time now comes with multiple formats, accessible through the dropdown arrow and slash commands.
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.ts
index 5e5aa5de2c..ceb7c732d5 100644
--- a/packages/trilium-core/src/services/export/markdown.spec.ts
+++ b/packages/trilium-core/src/services/export/markdown.spec.ts
@@ -499,6 +499,17 @@ describe("Markdown export", () => {
expect(markdownExportService.toMarkdown(html)).toBe(expected);
});
+ it("indents a nested list to the content column of its item, however wide the number", () => {
+ const html = /*html*/`<ol start="9"><li>Ninth<ol><li>A</li></ol></li><li>Tenth<ol><li>B</li><li>C</li></ol></li></ol>`;
+ const expected = trimIndentation`\
+ 9. Ninth
+ 1. A
+ 10. Tenth
+ 1. B
+ 2. C`;
+ expect(markdownExportService.toMarkdown(html)).toBe(expected);
+ });
+
it("converts inline math expressions into proper Markdown syntax", () => {
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}}$`;
diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.ts
index 55cac236cb..57000e13f3 100644
--- a/packages/trilium-core/src/services/export/markdown.ts
+++ b/packages/trilium-core/src/services/export/markdown.ts
@@ -401,15 +401,15 @@ function buildListItemFilter(): Rule {
return {
filter: "li",
replacement(content, node, options) {
- content = content
- .trim()
- .replace(/\n/gm, '\n '); // indent
let prefix = `${options.bulletListMarker} `;
+ let indentWidth = 4;
const parent = node.parentNode as HTMLElement;
if (parent.nodeName === 'OL') {
const start = parent.getAttribute('start');
const index = Array.prototype.indexOf.call(parent.children, node);
prefix = `${start ? Number(start) + index : index + 1}. `;
+ // Nested blocks must reach the item's content column, which moves right from item 10.
+ indentWidth = prefix.length;
} else if (parent.classList.contains("todo-list")) {
const state = (node as HTMLElement).getAttribute("data-trilium-task-state");
const stateMarker = state
@@ -423,7 +423,10 @@ function buildListItemFilter(): Rule {
}
}
- const result = prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '');
+ content = content
+ .trim()
+ .replace(/\n/gm, `\n${" ".repeat(indentWidth)}`);
+ const result = prefix + content +(node.nextSibling && !/\n$/.test(content) ? '\n' : '');
return result;
}
};
diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.ts
index 0a9328a92d..efca48228b 100644
--- a/packages/trilium-core/src/services/import/markdown.spec.ts
+++ b/packages/trilium-core/src/services/import/markdown.spec.ts
@@ -442,6 +442,15 @@ $$`;
expect(markdownService.renderToHtml(markdownExportService.toMarkdown(original), "Title")).toStrictEqual(original);
});
+ it("round-trips a list nested under a multi-digit ordered item through the Markdown exporter", () => {
+ for (const position of [ 9, 10, 100 ]) {
+ const siblings = Array.from({ length: position - 1 }, (_, index) => `<li>Item ${index + 1}</li>`).join("");
+ const original = `<ol>${siblings}<li>Parent<ol><li>First</li><li>Second</li></ol></li></ol>`;
+ expect(markdownService.renderToHtml(markdownExportService.toMarkdown(original), "Title"), `item ${position}`)
+ .toStrictEqual(original);
+ }
+ });
+
it("imports todo list multistate markers as data-trilium-task-state and titles the <li> with the state's human name", () => {
// The `title` attribute mirrors what the CKEditor data downcast emits — it's
// the hover tooltip viewers of the shared page, the read-only preview and