Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/notion): convert toggle headings to plain headings

Elian Doran85ce3d4aa2f3
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 ab6bcc5a70..cc3dec90bb 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -124,6 +124,42 @@ describe("convertNotionHtml — toggles", () => {
});
});
+describe("convertNotionHtml — toggle headings", () => {
+ // A toggle heading exports as a bare <details> (no ul.toggle wrapper) whose <summary> font-size encodes
+ // the level; Notion nests its body in a `.indented` div and wraps blocks in display:contents.
+ const toggleHeading = (size: string, title: string, body: string) =>
+ `<div style="display:contents" dir="auto"><details open=""><summary style="font-weight:600;font-size:${size};line-height:1.3;margin:0">${title}</summary><div class="indented"><div style="display:contents" dir="auto">${body}</div></div></details></div>`;
+
+ it("flattens a toggle heading into a plain heading, hoisting its body out of the `.indented` wrapper", () => {
+ expect(convertNotionHtml(toggleHeading("1.875em", "Section A", "<p>Content goes here.</p>"))).toBe(
+ `<h2>Section A</h2><p>Content goes here.</p>`
+ );
+ });
+
+ it("maps each Notion toggle-heading size to the matching (shifted) heading level", () => {
+ const input =
+ toggleHeading("1.875em", "L1", "<p>a</p>") +
+ toggleHeading("1.5em", "L2", "<p>b</p>") +
+ toggleHeading("1.25em", "L3", "<p>c</p>") +
+ toggleHeading("1.125em", "L4", "<p>d</p>");
+ expect(convertNotionHtml(input)).toBe(
+ `<h2>L1</h2><p>a</p><h3>L2</h3><p>b</p><h4>L3</h4><p>c</p><h5>L4</h5><p>d</p>`
+ );
+ });
+
+ it("preserves inline formatting in the heading title and handles an empty body", () => {
+ expect(convertNotionHtml(toggleHeading("1.5em", "A <strong>bold</strong> title", ""))).toBe(
+ `<h3>A <strong>bold</strong> title</h3>`
+ );
+ });
+
+ it("leaves a regular list toggle as a collapsible (its summary has no heading font-size)", () => {
+ expect(convertNotionHtml(`<ul class="toggle"><li><details open=""><summary>Plain</summary><div style="display:contents"><p>x</p></div></details></li></ul>`)).toBe(
+ `<details open class="trilium-collapsible"><summary>Plain</summary><p>x</p></details>`
+ );
+ });
+});
+
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.ts
index cae398d504..035af042d8 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -18,6 +18,7 @@ export function convertNotionHtml(html: string): string {
stripDatePrefixes(root);
convertTodoLists(root);
convertToggles(root);
+ convertToggleHeadings(root);
unwrapDisplayContents(root);
convertTables(root);
convertImages(root);
@@ -212,6 +213,51 @@ function convertToggles(root: HTMLElement) {
}
// #endregion
+// #region Toggle headings
+/**
+ * A Notion *toggle heading* is a collapsible whose title is a heading. Unlike a list toggle it exports as a
+ * bare `<details>` (no `ul.toggle` wrapper) whose `<summary>` carries the heading's font-size — the only
+ * signal of its level. Trilium's collapsible can't hold a heading in its summary, so flatten each toggle
+ * heading into a plain heading: emit an `<hN>` from the summary, then hoist the toggle's body in its place
+ * (dropping the `.indented` wrapper Notion nests it in). The level is shifted down one to match how Notion
+ * exports ordinary headings — its content "Heading 1" is `<h2>` because the page title takes `<h1>` — so a
+ * toggle heading lands at the same level as the equivalent plain heading.
+ */
+function convertToggleHeadings(root: HTMLElement) {
+ for (const details of root.querySelectorAll("details")) {
+ const summary = directChild(details, (node) => isTag(node, "summary"));
+ const tag = summary ? toggleHeadingTag(summary) : undefined;
+ if (!summary || !tag) {
+ continue;
+ }
+
+ // Unwrap the `.indented` body wrapper so the toggle's content lands at the heading's level, not nested.
+ for (const child of [...details.childNodes]) {
+ if (child instanceof HTMLElement && child.classList.contains("indented")) {
+ child.replaceWith(...child.childNodes);
+ }
+ }
+ details.insertAdjacentHTML("beforebegin", `<${tag}>${summary.innerHTML.trim()}</${tag}>`);
+ summary.remove();
+ details.replaceWith(...details.childNodes);
+ }
+}
+
+/** Notion toggle-heading summary font-sizes, mapped to the Trilium heading tag (shifted to match plain headings). */
+const TOGGLE_HEADING_TAGS: Record<string, string> = {
+ "1.875em": "h2",
+ "1.5em": "h3",
+ "1.25em": "h4",
+ "1.125em": "h5"
+};
+
+/** The heading tag a toggle heading's summary encodes via its font-size, or undefined if it isn't a heading. */
+function toggleHeadingTag(summary: HTMLElement): string | undefined {
+ const fontSize = summary.getAttribute("style")?.match(/font-size:\s*([\d.]+em)/)?.[1];
+ return fontSize ? TOGGLE_HEADING_TAGS[fontSize] : undefined;
+}
+// #endregion
+
// #region display:contents wrappers
/**
* Notion wraps almost every block in a `<div style="display:contents">`, a layout no-op that survives