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 Notion toggles to collapsible <details>
Elian Doraneb8df56607af
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.tsindex 0b0bd6196b..73b5b0058b 100644--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts@@ -77,3 +77,28 @@ describe("convertNotionHtml — display:contents wrappers", () => {expect(convertNotionHtml(input)).toBe(input);});});++describe("convertNotionHtml — toggles", () => {+ it("unwraps a Notion toggle into a bare collapsible <details>", () => {+ const input = `<div style="display:contents" dir="auto"><ul class="toggle"><li><details open=""><summary>Toggle title</summary><div style="display:contents" dir="auto"><p>Content goes here.</p></div><div style="display:contents" dir="auto"><p>And here.</p></div></details></li></ul></div>`;+ expect(convertNotionHtml(input)).toBe(+ `<details class="trilium-collapsible"><summary>Toggle title</summary><p>Content goes here.</p><p>And here.</p></details>`+ );+ });++ it("handles a toggle with no body and an empty summary", () => {+ expect(convertNotionHtml(`<ul class="toggle"><li><details open=""><summary>Title only</summary></details></li></ul>`)).toBe(+ `<details class="trilium-collapsible"><summary>Title only</summary></details>`+ );+ expect(convertNotionHtml(`<ul class="toggle"><li><details open=""><summary></summary></details></li></ul>`)).toBe(+ `<details class="trilium-collapsible"><summary></summary></details>`+ );+ });++ it("keeps a nested toggle nested rather than flattening it", () => {+ const input = `<ul class="toggle"><li><details open=""><summary>Outer</summary><div style="display:contents"><ul class="toggle"><li><details open=""><summary>Inner</summary></details></li></ul></div></details></li></ul>`;+ expect(convertNotionHtml(input)).toBe(+ `<details class="trilium-collapsible"><summary>Outer</summary><details class="trilium-collapsible"><summary>Inner</summary></details></details>`+ );+ });+});diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.tsindex 08c6fb7b7f..cd5b7a5b1f 100644--- a/packages/trilium-core/src/services/import/notion/converter.ts+++ b/packages/trilium-core/src/services/import/notion/converter.ts@@ -13,10 +13,31 @@ import { HTMLElement, parse } from "node-html-parser";export function convertNotionHtml(html: string): string {const root = parse(html);convertTodoLists(root);+ convertToggles(root);unwrapDisplayContents(root);return root.toString();}+/**+ * Trilium has no list-based toggle; its collapsible block is a bare `<details>`+ * (data view `<details class="trilium-collapsible">`). Notion exports each toggle as+ * `<ul class="toggle"><li><details>…</details></li></ul>`, so drop the list wrapper, hoisting the+ * `<details>` in its place and tagging it with Trilium's collapsible class. Only the toggle's own+ * top-level `<details>` is hoisted; a nested toggle is handled when the loop reaches its own `ul.toggle`,+ * which keeps it nested. The native `open` attribute is dropped (the sanitizer strips it anyway).+ */+function convertToggles(root: HTMLElement) {+ for (const ul of root.querySelectorAll("ul.toggle")) {+ const detailsList = directChildren(ul, "li").flatMap((li) => directChildren(li, "details"));+ for (const details of detailsList) {+ const existing = details.getAttribute("class");+ details.setAttribute("class", existing ? `${existing} trilium-collapsible` : "trilium-collapsible");+ details.removeAttribute("open");+ }+ ul.replaceWith(...detailsList);+ }+}+/*** Notion wraps almost every block in a `<div style="display:contents">`, a layout no-op that survives* sanitization and would otherwise leave the imported note littered with meaningless block wrappers.