Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(import): convert legacy <en-todo> checkboxes to real todo-lists
Elian Doran8c2f5afce56f
diff --git a/packages/trilium-core/src/services/import/enex.spec.ts b/packages/trilium-core/src/services/import/enex.spec.tsindex 75afd000d7..7dcf0cfdf0 100644--- a/packages/trilium-core/src/services/import/enex.spec.ts+++ b/packages/trilium-core/src/services/import/enex.spec.ts@@ -179,4 +179,25 @@ describe("importEnex", () => {const dupIds = importedNote.getChildNotes().filter(n => n.title === "Dup").map(n => n.noteId);expect(linker.getRelations("internalLink").some(r => dupIds.includes(r.value))).toBe(false);});++ it("converts legacy <en-todo> checkboxes into a real todo-list, not literal ballot boxes", async () => {+ const { importedNote } = await testImport("Legacy checkboxes.enex");++ const note = importedNote.getChildNotes().find(n => n.title === "Ideas");+ if (!note) {+ throw new Error("'Ideas' note was not imported");+ }+ const content = decodeUtf8(note.getContent());++ // Rendered as an actual CKEditor todo-list, not the old literal ☐/☑ characters.+ expect(content).toContain(`<ul class="todo-list">`);+ expect(content).not.toContain("☐");+ expect(content).not.toContain("☑");++ expect(content).toContain(`<span class="todo-list__label__description">Write tests</span>`);+ expect(content).toContain(`<span class="todo-list__label__description">Ship <i>it</i></span>`);+ expect(content).toContain(`<span class="todo-list__label__description">Investigate bug</span>`);+ // The checked item ("Investigate bug") carries the checked state.+ expect(content).toContain(`checked="checked"`);+ });}, 60_000);diff --git a/packages/trilium-core/src/services/import/enex.ts b/packages/trilium-core/src/services/import/enex.tsindex 1027500785..21124e8dfa 100644--- a/packages/trilium-core/src/services/import/enex.ts+++ b/packages/trilium-core/src/services/import/enex.ts@@ -89,11 +89,6 @@ async function importEnex(taskContext: TaskContext<"importNotes">, file: File, pcontent = content.trim();- // Replace en-todo with unicode ballot box (old Evernote checkbox format). Done as a string replace- // before any HTML parsing so node-html-parser never has to deal with the self-closing <en-todo>.- content = content.replace(/<en-todo\s+checked="true"\s*\/>/g, "\u2611 ");- content = content.replace(/<en-todo(\s+checked="false")?\s*\/>/g, "\u2610 ");-// Replace OneNote converted checkboxes with unicode ballot box based// on known hash of checkboxes for regular, p1, and p2 checkboxescontent = content.replace(diff --git a/packages/trilium-core/src/services/import/enex_converter.spec.ts b/packages/trilium-core/src/services/import/enex_converter.spec.tsindex 6777c73608..0af79fcbee 100644--- a/packages/trilium-core/src/services/import/enex_converter.spec.ts+++ b/packages/trilium-core/src/services/import/enex_converter.spec.ts@@ -44,6 +44,30 @@ describe("convertEnexContent — checkboxes (--en-todo lists)", () => {});});+describe("convertEnexContent — legacy checkboxes (<en-todo>)", () => {+ it("converts a run of <en-todo> line-divs into a single todo-list (instead of literal ballot boxes)", () => {+ const input = `<div><en-todo checked="false"/>First</div><div><en-todo checked="true"/>Second</div>`;+ expect(convertEnexContent(input)).toBe(todoList(todoItem("First"), todoItem("Second", { checked: true })));+ });++ it("preserves inline formatting in the checkbox text", () => {+ const input = `<div><en-todo checked="false"/>Buy <strong>milk</strong></div>`;+ expect(convertEnexContent(input)).toBe(todoList(todoItem("Buy <strong>milk</strong>")));+ });++ it("starts a new list when a non-todo line interrupts the run", () => {+ const input = `<div><en-todo checked="false"/>A</div><div>Heading</div><div><en-todo checked="true"/>B</div>`;+ expect(convertEnexContent(input)).toBe(+ `${todoList(todoItem("A"))}<div>Heading</div>${todoList(todoItem("B", { checked: true }))}`+ );+ });++ it("falls back to a unicode ballot box for an inline <en-todo> that isn't a line's checkbox", () => {+ const input = `<div>Mixed <en-todo checked="true"/>content</div>`;+ expect(convertEnexContent(input)).toBe(`<div>Mixed ☑ content</div>`);+ });+});+describe("convertEnexContent — code blocks (--en-codeblock)", () => {it("converts a code block, mapping the syntax language to a CKEditor mime class and joining lines", () => {const input = `<div style="--en-codeblock:true; --en-syntaxLanguage:c; --en-lineWrapping:false;box-sizing: border-box;"><div>void main() {</div><div> printf("Hello world.\\n");</div><div>}</div></div>`;diff --git a/packages/trilium-core/src/services/import/enex_converter.ts b/packages/trilium-core/src/services/import/enex_converter.tsindex f86fad6958..9799eb8e39 100644--- a/packages/trilium-core/src/services/import/enex_converter.ts+++ b/packages/trilium-core/src/services/import/enex_converter.ts@@ -31,6 +31,7 @@ export function convertEnexContent(html: string, tasks: EnexTask[] = []): stringconvertCallouts(root);convertToggles(root);convertTodoLists(root);+ convertEnTodoLists(root);convertCodeBlocks(root);convertFormulaBlocks(root);convertMermaidBlocks(root);@@ -224,6 +225,86 @@ function isEnTodoUl(node: HTMLElement["childNodes"][number] | null): node is HTM}// #endregion+// #region Legacy checkboxes (<en-todo>)+/**+ * Older Evernote notes don't use the `--en-todo` list markup; each checkbox is an inline `<en-todo+ * checked="…"/>` element at the start of its own `<div>` line (`<div><en-todo checked="false"/>Task</div>`).+ * Rewrite each maximal run of adjacent such lines into one CKEditor `<ul class="todo-list">` so they import+ * as real to-do items rather than the literal ☐/☑ characters they used to become. A line that doesn't start+ * with a checkbox breaks the run; any leftover `<en-todo>` (e.g. mid-line) falls back to a unicode ballot box+ * so the information isn't lost.+ */+function convertEnTodoLists(root: HTMLElement) {+ convertEnTodoRuns(root);++ for (const enTodo of root.querySelectorAll("en-todo")) {+ enTodo.insertAdjacentHTML("beforebegin", enTodo.getAttribute("checked") === "true" ? "☑ " : "☐ ");+ enTodo.remove();+ }+}++/** Rewrites each run of adjacent `<en-todo>` line-divs within `container` into one todo-list, then recurses. */+function convertEnTodoRuns(container: HTMLElement) {+ for (const run of collectEnTodoRuns(container)) {+ const items = run.map((div) => buildEnTodoItem(div));+ run[0].insertAdjacentHTML("beforebegin", renderTodoList(items));+ for (const div of run) {+ div.remove();+ }+ }++ for (const child of container.childNodes) {+ if (child instanceof HTMLElement) {+ convertEnTodoRuns(child);+ }+ }+}++/** Groups a container's direct children into maximal runs of adjacent `<en-todo>` line-divs (ignoring whitespace). */+function collectEnTodoRuns(container: HTMLElement): HTMLElement[][] {+ const runs: HTMLElement[][] = [];+ let current: HTMLElement[] | null = null;++ for (const node of container.childNodes) {+ if (node instanceof HTMLElement && isEnTodoDiv(node)) {+ if (!current) {+ current = [];+ runs.push(current);+ }+ current.push(node);+ } else if (node instanceof HTMLElement || node.toString().trim() !== "") {+ // Any other element, or non-whitespace text, breaks the run; whitespace between lines does not.+ current = null;+ }+ }++ return runs;+}++/** A `<div>` whose first meaningful child is an `<en-todo>` checkbox — Evernote's legacy to-do line. */+function isEnTodoDiv(node: HTMLElement): boolean {+ if (!isTag(node, "div")) {+ return false;+ }+ for (const child of node.childNodes) {+ if (child instanceof HTMLElement) {+ return isTag(child, "en-todo");+ }+ if (child.toString().trim() !== "") {+ return false;+ }+ }+ return false;+}++function buildEnTodoItem(div: HTMLElement): string {+ const checkbox = div.childNodes.find((node): node is HTMLElement => node instanceof HTMLElement && isTag(node, "en-todo"));+ const checked = checkbox?.getAttribute("checked") === "true";+ const description = div.childNodes.filter((node) => node !== checkbox).map((node) => node.toString()).join("").trim();+ return renderTodoItem(checked, description);+}+// #endregion+// #region Code, formula and mermaid blocks/*** Evernote code blocks are `<div style="--en-codeblock:true; --en-syntaxLanguage:<lang>">` whose innerdiff --git a/packages/trilium-core/src/services/import/samples/Legacy checkboxes.enex b/packages/trilium-core/src/services/import/samples/Legacy checkboxes.enexnew file mode 100644index 0000000000..d5b98b7937--- /dev/null+++ b/packages/trilium-core/src/services/import/samples/Legacy checkboxes.enex@@ -0,0 +1,10 @@+<?xml version="1.0" encoding="UTF-8"?>+<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">+<en-export export-date="20260101T000000Z" application="Evernote" version="11">+ <note>+ <title>Ideas</title>+ <created>20200101T000000Z</created>+ <content><![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>+<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note><div><b>Tasks</b></div><div><en-todo checked="false"/>Write tests</div><div><en-todo checked="true"/>Investigate bug</div><div><en-todo checked="false"/>Ship <i>it</i></div></en-note>]]></content>+ </note>+</en-export>