Skip to content

Build your personal knowledge base with Trilium Notes

fix(import): render OneNote to-do tags as a Trilium task list

Elian Doran8ac55833df6d
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
index cc51c46be8..3beb960b3e 100644
--- a/apps/server/src/services/import/onenote/converter.spec.ts
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -103,6 +103,19 @@ const STYLES_SAMPLE = `<html lang="en-US">
</body>
</html>`;
+// Real OneNote source (debug-captured): to-do items are paragraphs tagged with data-tag="to-do" /
+// "to-do:completed", not list markup.
+const TAGS_SAMPLE = `<html lang="en-US">
+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
+ <div style="position:absolute;left:48px;top:115px;width:624px">
+ <p data-tag="to-do" style="margin-top:0pt;margin-bottom:0pt">To do</p>
+ <p data-tag="to-do" style="margin-top:0pt;margin-bottom:0pt">Another todo</p>
+ <p data-tag="to-do:completed" style="margin-top:0pt;margin-bottom:0pt">Completed todo</p>
+ <p data-tag="to-do" style="margin-top:0pt;margin-bottom:0pt">Another non-completed TODO</p>
+ </div>
+ </body>
+</html>`;
+
// Tests assert the end result (the HTML actually stored on the note, i.e. after sanitization).
describe("convertPageHtml", () => {
it("strips OneNote's block-level <br> spacing and empty list items, keeping real content", () => {
@@ -134,10 +147,22 @@ describe("convertPageHtml", () => {
expect(parse(out).querySelectorAll("br")).toHaveLength(1);
});
- it("converts OneNote to-do tags into task-list checkboxes", () => {
- const out = converter.convertPageHtml(`<body><p data-tag="to-do:completed">done</p><p data-tag="to-do">todo</p></body>`);
- expect(out).toContain("[x] done");
- expect(out).toContain("[ ] todo");
+ it("converts OneNote to-do tags into a Trilium task list", () => {
+ const out = converter.convertPageHtml(TAGS_SAMPLE);
+ const root = parse(out);
+
+ // Consecutive to-do paragraphs become one CKEditor todo-list, not literal "[ ]" text.
+ expect(root.querySelectorAll("ul.todo-list")).toHaveLength(1);
+ expect(root.querySelectorAll("li")).toHaveLength(4);
+ expect(root.querySelectorAll(`input[type="checkbox"]`)).toHaveLength(4);
+ expect(root.querySelectorAll("span.todo-list__label__description")).toHaveLength(4);
+
+ // Exactly the one completed item is checked.
+ expect(root.querySelectorAll("input[checked]")).toHaveLength(1);
+
+ expect(out).toContain("Completed todo");
+ expect(out).not.toContain("[ ]");
+ expect(out).not.toContain("[x]");
});
it("converts inline-style formatting (bold/italic/underline) to semantic tags", () => {
diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.ts
index 768da2297b..e3050794d0 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -41,15 +41,36 @@ export function convertPageHtml(rawHtml: string): string {
return sanitize.sanitizeHtml(scope.innerHTML);
}
-/** OneNote marks checkboxes with `data-tag="to-do"` / `"to-do:completed"`; render them as task items. */
+/**
+ * OneNote marks checkboxes as `<p data-tag="to-do">` / `"to-do:completed"` paragraphs. Group runs of
+ * consecutive to-do paragraphs into a single CKEditor task list (<ul class="todo-list">…), matching
+ * the structure Trilium's TodoList editor plugin produces — completed items use a checked checkbox.
+ */
function convertTodoTags(scope: HTMLElement) {
- for (const el of scope.querySelectorAll("[data-tag]")) {
+ const todos = scope.querySelectorAll("p[data-tag]").filter((el) => {
const tag = el.getAttribute("data-tag");
- if (tag === "to-do" || tag === "to-do:completed") {
- const checkbox = tag === "to-do:completed" ? "[x]" : "[ ]";
- el.set_content(`${checkbox} ${el.innerHTML}`);
+ return tag === "to-do" || tag === "to-do:completed";
+ });
+
+ // Collect adjacent to-do paragraphs so each run becomes one list.
+ const runs: HTMLElement[][] = [];
+ for (const p of todos) {
+ const currentRun = runs[runs.length - 1];
+ if (currentRun && currentRun[currentRun.length - 1].nextElementSibling === p) {
+ currentRun.push(p);
+ } else {
+ runs.push([p]);
}
}
+
+ for (const run of runs) {
+ const items = run.map((p) => {
+ const checked = p.getAttribute("data-tag") === "to-do:completed" ? ` checked="checked"` : "";
+ return `<li><label class="todo-list__label"><input type="checkbox"${checked}><span class="todo-list__label__description">${p.innerHTML}</span></label></li>`;
+ });
+ run[0].insertAdjacentHTML("beforebegin", `<ul class="todo-list">${items.join("")}</ul>`);
+ run.forEach((p) => p.remove());
+ }
}
/**