Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): convert Notion bookmark cards to link-embeds

Elian Doran3019b499efef
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 455fa09107..e7489eee99 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -150,3 +150,19 @@ describe("convertNotionHtml — math", () => {
expect(convertNotionHtml(`<p>Text<style>@import url('x')</style></p>`)).toBe(`<p>Text</p>`);
});
});
+
+describe("convertNotionHtml — bookmarks", () => {
+ it("converts a Notion bookmark card to a Trilium link-embed, keeping the favicon", () => {
+ const input = `<div style="display:contents" dir="ltr"><figure id="386c5eca"><a href="https://triliumnotes.org/" class="bookmark source"><div class="bookmark-info"><div class="bookmark-text"><div class="bookmark-title">Trilium Notes</div><div class="bookmark-description">Trilium is an open-source solution for note-taking and personal knowledge bases. Use it locally or sync with your own server to access notes anywhere.</div></div><div class="bookmark-href"><img src="https://triliumnotes.org/assets/favicon-BI5VJBD3.ico" class="icon bookmark-icon"/>https://triliumnotes.org/</div></div></a></figure></div>`;
+ expect(convertNotionHtml(input)).toBe(
+ `<section class="link-embed" data-url="https://triliumnotes.org/" data-embed-type="opengraph" data-title="Trilium Notes" data-description="Trilium is an open-source solution for note-taking and personal knowledge bases. Use it locally or sync with your own server to access notes anywhere." data-favicon="https://triliumnotes.org/assets/favicon-BI5VJBD3.ico"></section>`
+ );
+ });
+
+ it("omits optional attributes that the bookmark doesn't provide", () => {
+ const input = `<figure><a href="https://example.com/" class="bookmark source"><div class="bookmark-info"><div class="bookmark-text"><div class="bookmark-title">Example</div></div></div></a></figure>`;
+ expect(convertNotionHtml(input)).toBe(
+ `<section class="link-embed" data-url="https://example.com/" data-embed-type="opengraph" data-title="Example"></section>`
+ );
+ });
+});
diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.ts
index 7abba0e612..e9e4ca54a0 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -17,9 +17,46 @@ export function convertNotionHtml(html: string): string {
convertToggles(root);
unwrapDisplayContents(root);
convertCallouts(root);
+ convertBookmarks(root);
return root.toString();
}
+/**
+ * Notion bookmark cards are `<figure><a class="bookmark source" href="…"><div class="bookmark-info">…`,
+ * carrying a title, description and favicon. Trilium has the equivalent link-embed, so rewrite each into
+ * `<section class="link-embed" data-…>` (an open-graph embed). Optional fields are only emitted when the
+ * bookmark provides them.
+ */
+function convertBookmarks(root: HTMLElement) {
+ for (const figure of root.querySelectorAll("figure")) {
+ const url = figure.querySelector("a.bookmark")?.getAttribute("href");
+ if (!url) {
+ continue;
+ }
+
+ const section = parse(`<section></section>`).querySelector("section");
+ if (!section) {
+ continue;
+ }
+ section.setAttribute("class", "link-embed");
+ section.setAttribute("data-url", url);
+ section.setAttribute("data-embed-type", "opengraph");
+
+ const optional: Record<string, string | undefined> = {
+ "data-title": figure.querySelector(".bookmark-title")?.textContent?.trim(),
+ "data-description": figure.querySelector(".bookmark-description")?.textContent?.trim(),
+ "data-favicon": figure.querySelector("img.bookmark-icon")?.getAttribute("src")
+ };
+ for (const [key, value] of Object.entries(optional)) {
+ if (value) {
+ section.setAttribute(key, value);
+ }
+ }
+
+ figure.replaceWith(section);
+ }
+}
+
/**
* Notion renders each equation with KaTeX, preceded by a `<style>@import …katex…</style>` whose text
* would otherwise survive sanitization as visible junk. Drop those style blocks and replace each inline