Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): convert Notion callouts to admonitions

Elian Doranfdf4756c3a11
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 6675a8efd5..966031d21b 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -80,8 +80,8 @@ describe("convertNotionHtml — display:contents wrappers", () => {
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>`;
+ it("leaves meaningful (non display:contents) divs intact", () => {
+ const input = `<section><div style="font-size:1.5em"><span class="icon">x</span></div><div style="width:100%">body</div></section>`;
expect(convertNotionHtml(input)).toBe(input);
});
});
@@ -116,3 +116,26 @@ describe("convertNotionHtml — toggles", () => {
);
});
});
+
+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>`;
+
+ it("maps the default light-bulb callout to a tip admonition, dropping the redundant icon", () => {
+ expect(convertNotionHtml(callout("💡", `<p>Callout with default icon.</p>`))).toBe(
+ `<aside class="admonition tip"><p>Callout with default icon.</p></aside>`
+ );
+ });
+
+ it("maps a non-default emoji callout to a note admonition, preserving the emoji in the content", () => {
+ expect(convertNotionHtml(callout("♻️", `<p>Callout with custom emoji.</p>`))).toBe(
+ `<aside class="admonition note"><p>♻️ Callout with custom emoji.</p></aside>`
+ );
+ });
+
+ it("preserves a non-default emoji as a leading paragraph when the content doesn't start with one", () => {
+ expect(convertNotionHtml(callout("♻️", `<h2>Heading body</h2>`))).toBe(
+ `<aside class="admonition note"><p>♻️</p><h2>Heading body</h2></aside>`
+ );
+ });
+});
diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.ts
index c4c3c86238..6484b294cb 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -15,9 +15,49 @@ export function convertNotionHtml(html: string): string {
convertTodoLists(root);
convertToggles(root);
unwrapDisplayContents(root);
+ convertCallouts(root);
return root.toString();
}
+/** Notion's default callout icon; it maps 1:1 to a "tip" admonition, so the emoji itself is redundant. */
+const DEFAULT_CALLOUT_EMOJI = "💡";
+
+/**
+ * Notion callouts are `<figure class="callout">` with an icon div (`<span class="icon">…</span>`) and a
+ * content div. Trilium has admonitions, so rewrite each into `<aside class="admonition <type>">`. The
+ * default light-bulb maps to a "tip" (the icon is implied, so it's dropped); any other emoji maps to a
+ * neutral "note" with the emoji preserved at the start of the content so the information isn't lost.
+ *
+ * Runs after wrapper-stripping so the content is already clean, and in reverse document order so a nested
+ * callout is converted before the callout that contains it.
+ */
+function convertCallouts(root: HTMLElement) {
+ for (const figure of [...root.querySelectorAll("figure.callout")].reverse()) {
+ const divs = directChildren(figure, "div");
+ const iconDiv = divs.find((div) => (div.getAttribute("style") ?? "").includes("font-size"));
+ const contentDiv = divs.find((div) => (div.getAttribute("style") ?? "").replace(/\s/g, "").includes("width:100%")) ?? divs[divs.length - 1];
+
+ const emoji = iconDiv?.querySelector("span.icon")?.textContent?.trim() ?? "";
+ const type = emoji === DEFAULT_CALLOUT_EMOJI ? "tip" : "note";
+ if (type === "note" && emoji && contentDiv) {
+ prependEmoji(contentDiv, emoji);
+ }
+
+ figure.insertAdjacentHTML("beforebegin", `<aside class="admonition ${type}">${contentDiv?.innerHTML ?? ""}</aside>`);
+ figure.remove();
+ }
+}
+
+/** Prepends the callout's emoji to its content: into the first paragraph, or as a leading one otherwise. */
+function prependEmoji(contentDiv: HTMLElement, emoji: string) {
+ const first = directChild(contentDiv, () => true);
+ if (first && isTag(first, "p")) {
+ first.set_content(`${emoji} ${first.innerHTML}`);
+ } else {
+ contentDiv.insertAdjacentHTML("afterbegin", `<p>${emoji}</p>`);
+ }
+}
+
/**
* Trilium has no list-based toggle; its collapsible block is a bare `<details>`
* (data view `<details class="trilium-collapsible">`). Notion exports each toggle as