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/notion): merge fragmented bullet and numbered lists
Elian Doranef7a9030ef16
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.tsindex c353bfda82..40d49f7f4f 100644--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts@@ -169,6 +169,48 @@ describe("convertNotionHtml — table of contents", () => {});});+describe("convertNotionHtml — list fragmentation", () => {+ // Notion exports each list item as its own single-item <ul>/<ol> (wrapped in a display:contents <div>),+ // and fragments nested lists the same way; consecutive same-kind fragments must merge into one clean list.+ const dc = (inner: string) => `<div style="display:contents" dir="auto">${inner}</div>`;++ it("merges fragmented bullet items into a single list, including nested sublists", () => {+ const nested =+ dc(`<ul class="bulleted-list"><li style="list-style-type:circle">Nested 1</li></ul>`) ++ dc(`<ul class="bulleted-list"><li style="list-style-type:circle">Nested 2</li></ul>`);+ const input =+ dc(`<ul class="bulleted-list"><li style="list-style-type:disc">Item 1</li></ul>`) ++ dc(`<ul class="bulleted-list"><li style="list-style-type:disc">Item 2${nested}</li></ul>`);+ expect(convertNotionHtml(input)).toBe(+ `<ul><li style="list-style-type:disc">Item 1</li><li style="list-style-type:disc">Item 2<ul><li style="list-style-type:circle">Nested 1</li><li style="list-style-type:circle">Nested 2</li></ul></li></ul>`+ );+ });++ it("merges fragmented numbered items into a single list, dropping per-fragment start/type", () => {+ const nested =+ dc(`<ol type="a" class="numbered-list" start="1"><li>A</li></ol>`) ++ dc(`<ol type="a" class="numbered-list" start="2"><li>B</li></ol>`);+ const input =+ dc(`<ol type="1" class="numbered-list" start="1"><li>One</li></ol>`) ++ dc(`<ol type="1" class="numbered-list" start="2"><li>Two</li></ol>`) ++ dc(`<ol type="1" class="numbered-list" start="3"><li>Three${nested}</li></ol>`);+ expect(convertNotionHtml(input)).toBe(+ `<ol><li>One</li><li>Two</li><li>Three<ol><li>A</li><li>B</li></ol></li></ol>`+ );+ });++ it("does not merge lists separated by other content or of a different type", () => {+ const input =+ dc(`<ul class="bulleted-list"><li>A</li></ul>`) ++ dc(`<p>break</p>`) ++ dc(`<ul class="bulleted-list"><li>B</li></ul>`) ++ dc(`<ol class="numbered-list" start="1"><li>C</li></ol>`);+ expect(convertNotionHtml(input)).toBe(+ `<ul><li>A</li></ul><p>break</p><ul><li>B</li></ul><ol><li>C</li></ol>`+ );+ });+});+describe("convertNotionHtml — callouts", () => {const callout = (emoji: string, body: string) =>`<div style="display:contents" dir="ltr"><figure class="block-color-gray_background callout" style="white-space:pre-wrap;display:flex" id="386c5eca"><div style="font-size:1.5em"><span class="icon">${emoji}</span></div><div style="width:100%"><div style="display:contents" dir="auto">${body}</div></div></figure></div>`;diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.tsindex c01393489e..699657563f 100644--- a/packages/trilium-core/src/services/import/notion/converter.ts+++ b/packages/trilium-core/src/services/import/notion/converter.ts@@ -21,6 +21,7 @@ export function convertNotionHtml(html: string): string {convertToggleHeadings(root);dropTableOfContents(root);unwrapDisplayContents(root);+ mergeFragmentedLists(root);convertTables(root);convertImages(root);convertAttachments(root);@@ -290,6 +291,40 @@ function unwrapDisplayContents(root: HTMLElement) {}// #endregion+// #region List fragmentation+/**+ * Notion exports each list item as its own single-item `<ul>`/`<ol>` (so a three-item list is three lists),+ * and fragments nested lists the same way. Merge each fragment into its previous sibling of the same kind —+ * `bulleted-list` with `bulleted-list`, `numbered-list` with `numbered-list` — so a run of fragments becomes+ * one list; document order makes the first fragment accumulate the rest, and nested fragments merge once the+ * outer merge has reparented them. Any other element between two lists keeps them apart (so a paragraph splits+ * a run), and a list never merges across types. The surviving lists are then stripped of Notion's list class+ * and the `start`/`type`/`id` fragmentation artifacts, leaving clean `<ul>`/`<ol>`.+ */+function mergeFragmentedLists(root: HTMLElement) {+ for (const list of root.querySelectorAll("ul.bulleted-list, ol.numbered-list")) {+ const prev = list.previousElementSibling;+ if (prev && prev.tagName === list.tagName && isMergeableList(prev)) {+ for (const item of [...list.childNodes]) {+ prev.appendChild(item);+ }+ list.remove();+ }+ }+ for (const list of root.querySelectorAll("ul.bulleted-list, ol.numbered-list")) {+ for (const attr of ["class", "start", "type", "id"]) {+ list.removeAttribute(attr);+ }+ }+}++/** A Notion bulleted/numbered list — the kinds Notion fragments one item per list, to be merged back together. */+function isMergeableList(el: HTMLElement): boolean {+ return (isTag(el, "ul") && el.classList.contains("bulleted-list"))+ || (isTag(el, "ol") && el.classList.contains("numbered-list"));+}+// #endregion+// #region Tables/*** Notion tables are `<table class="simple-table">` with Notion-specific classes, ids and pixel widths on