Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): strip Notion's display:contents block wrappers

Elian Doran982df212fc37
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 01ea3e3bac..0b0bd6196b 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -56,3 +56,24 @@ describe("convertNotionHtml — to-do nesting and merging", () => {
expect(convertNotionHtml(input)).toBe(`${list(item("A"))}<p>Sep</p>${list(item("B"))}`);
});
});
+
+describe("convertNotionHtml — display:contents wrappers", () => {
+ it("unwraps the display:contents div Notion puts around each block", () => {
+ expect(convertNotionHtml(`<div style="display:contents" dir="auto"><p id="x">A</p></div>`)).toBe(`<p id="x">A</p>`);
+ });
+
+ it("unwraps several wrapped blocks", () => {
+ const input = `<div style="display:contents" dir="auto"><h1>A</h1></div><div style="display:contents" dir="auto"><p>B</p></div>`;
+ expect(convertNotionHtml(input)).toBe(`<h1>A</h1><p>B</p>`);
+ });
+
+ it("flattens nested display:contents wrappers", () => {
+ const input = `<div style="display:contents"><div style="display:contents"><p>A</p></div></div>`;
+ expect(convertNotionHtml(input)).toBe(`<p>A</p>`);
+ });
+
+ it("leaves meaningful divs intact", () => {
+ const input = `<figure class="callout"><div style="font-size:1.5em"><span class="icon">x</span></div><div style="width:100%">body</div></figure>`;
+ expect(convertNotionHtml(input)).toBe(input);
+ });
+});
diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.ts
index e7c554eb42..08c6fb7b7f 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -13,9 +13,25 @@ import { HTMLElement, parse } from "node-html-parser";
export function convertNotionHtml(html: string): string {
const root = parse(html);
convertTodoLists(root);
+ unwrapDisplayContents(root);
return root.toString();
}
+/**
+ * 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.
+ * Hoist each such div's children in its place; nested wrappers flatten too (querySelectorAll visits the
+ * outer first, and the inner stays a live, reparented node). Other divs (callouts, `.indented`, …) are
+ * left untouched.
+ */
+function unwrapDisplayContents(root: HTMLElement) {
+ for (const div of root.querySelectorAll("div")) {
+ if (isDisplayContents(div)) {
+ div.replaceWith(...div.childNodes);
+ }
+ }
+}
+
/**
* Notion renders every to-do item as its own `<ul class="to-do-list">` (each wrapped in a
* `display:contents` `<div>`), whose `<li>` holds a `<div class="checkbox checkbox-on|off">`, a
@@ -95,7 +111,7 @@ function todoUl(node: HTMLElement): HTMLElement | null {
if (isTodoUl(node)) {
return node;
}
- if (isTag(node, "div") && (node.getAttribute("style") ?? "").replace(/\s/g, "").includes("display:contents")) {
+ if (isDisplayContents(node)) {
const elements = node.childNodes.filter((child): child is HTMLElement => child instanceof HTMLElement);
if (elements.length === 1 && isTodoUl(elements[0])) {
return elements[0];
@@ -104,6 +120,10 @@ function todoUl(node: HTMLElement): HTMLElement | null {
return null;
}
+function isDisplayContents(node: HTMLElement): boolean {
+ return isTag(node, "div") && (node.getAttribute("style") ?? "").replace(/\s/g, "").includes("display:contents");
+}
+
function isTodoUl(node: HTMLElement): boolean {
return isTag(node, "ul") && node.classList.contains("to-do-list");
}