Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): preserve Notion toggle open/closed state

Elian Doran11874899b6eb
diff --git a/packages/trilium-core/src/services/import/notion/converter.spec.ts b/packages/trilium-core/src/services/import/notion/converter.spec.ts
index d7c5bef6bd..6675a8efd5 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -60,7 +60,7 @@ describe("convertNotionHtml — to-do nesting and merging", () => {
const toggle = (summary: string, body: string) => `<ul class="toggle"><li><details open=""><summary>${summary}</summary>${body}</details></li></ul>`;
const input = toggle("The quick brownie", wrap(off("The quick brown", wrap(off("fox jumps")))));
expect(convertNotionHtml(input)).toBe(
- `<details class="trilium-collapsible"><summary>The quick brownie</summary>${list(item("The quick brown", { nested: list(item("fox jumps")) }))}</details>`
+ `<details open class="trilium-collapsible"><summary>The quick brownie</summary>${list(item("The quick brown", { nested: list(item("fox jumps")) }))}</details>`
);
});
});
@@ -87,26 +87,32 @@ describe("convertNotionHtml — display:contents wrappers", () => {
});
describe("convertNotionHtml — toggles", () => {
- it("unwraps a Notion toggle into a bare collapsible <details>", () => {
+ it("unwraps a Notion toggle into a bare collapsible <details>, preserving its open state", () => {
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>`
+ `<details open class="trilium-collapsible"><summary>Toggle title</summary><p>Content goes here.</p><p>And here.</p></details>`
+ );
+ });
+
+ it("preserves a collapsed toggle's state (no open attribute)", () => {
+ expect(convertNotionHtml(`<ul class="toggle"><li><details><summary>Closed</summary></details></li></ul>`)).toBe(
+ `<details class="trilium-collapsible"><summary>Closed</summary></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>`
+ `<details open 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>`
+ `<details open 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>`
+ `<details open class="trilium-collapsible"><summary>Outer</summary><details open 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.ts
index b442eecd65..c4c3c86238 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -24,7 +24,9 @@ export function convertNotionHtml(html: string): string {
* `<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).
+ * which keeps it nested. Notion's native `open` attribute is preserved so the published/share view
+ * reflects the exported expanded/collapsed state (the in-app editor always loads toggles collapsed, and
+ * the sanitizer is configured to keep `open` on `details`).
*/
function convertToggles(root: HTMLElement) {
for (const ul of root.querySelectorAll("ul.toggle")) {
@@ -32,7 +34,6 @@ function convertToggles(root: HTMLElement) {
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);
}
diff --git a/packages/trilium-core/src/services/sanitizer.spec.ts b/packages/trilium-core/src/services/sanitizer.spec.ts
index 230c953fc4..c4242a5ea9 100644
--- a/packages/trilium-core/src/services/sanitizer.spec.ts
+++ b/packages/trilium-core/src/services/sanitizer.spec.ts
@@ -58,6 +58,11 @@ describe("sanitize", () => {
expect(sanitizeHtml(dirty)).toBe(dirty);
});
+ it("keeps the open attribute on <details> (collapsible state from imports)", () => {
+ const dirty = `<details open class="trilium-collapsible"><summary>T</summary><p>body</p></details>`;
+ expect(sanitizeHtml(dirty)).toBe(dirty);
+ });
+
describe("bookmark anchors", () => {
it("preserves id attribute on empty <a> tags (CKEditor bookmarks)", () => {
const dirty = `<a id="my-bookmark"></a>`;
diff --git a/packages/trilium-core/src/services/sanitizer.ts b/packages/trilium-core/src/services/sanitizer.ts
index 8e72796973..fd068e1275 100644
--- a/packages/trilium-core/src/services/sanitizer.ts
+++ b/packages/trilium-core/src/services/sanitizer.ts
@@ -46,6 +46,9 @@ export function sanitizeHtml(dirtyHtml: string) {
a: ["id"],
h2: ["id"],
li: ["id"],
+ // Collapsible blocks: keep the native open/closed state (e.g. preserved from a Notion import)
+ // so the published/share view, which renders this HTML directly, reflects it.
+ details: ["open"],
input: ["type", "checked"],
img: ["width", "height"],
td: ["colspan", "rowspan"],