Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): import Notion images as attachments

Elian Doran5cf8181502c1
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 325586b28d..c9a56b0967 100644
--- a/packages/trilium-core/src/services/import/notion/converter.spec.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.spec.ts
@@ -176,6 +176,21 @@ describe("convertNotionHtml — tables", () => {
});
});
+describe("convertNotionHtml — images", () => {
+ it("normalizes a Notion image figure into a canonical figure>img (src left for the importer to rewrite)", () => {
+ const input = `<div style="display:contents" dir="ltr"><figure id="386c5eca" class="image"><a href="Formatting%20test/Screenshot.png"><img style="width:463.98px" src="Formatting%20test/Screenshot.png"/></a></figure></div>`;
+ expect(convertNotionHtml(input)).toBe(
+ `<figure class="image"><img src="Formatting%20test/Screenshot.png"></figure>`
+ );
+ });
+
+ it("leaves a plain figure>img untouched apart from dropping inline sizing", () => {
+ expect(convertNotionHtml(`<figure class="image"><img src="x.png" style="width:10px"></figure>`)).toBe(
+ `<figure class="image"><img src="x.png"></figure>`
+ );
+ });
+});
+
describe("convertNotionHtml — date mentions", () => {
it("strips the @ prefix Notion puts on inline <time> date mentions", () => {
expect(convertNotionHtml(`<p><time>@June 21, 2026</time></p>`)).toBe(`<p><time>June 21, 2026</time></p>`);
diff --git a/packages/trilium-core/src/services/import/notion/converter.ts b/packages/trilium-core/src/services/import/notion/converter.ts
index 5042a29dff..294022ae2b 100644
--- a/packages/trilium-core/src/services/import/notion/converter.ts
+++ b/packages/trilium-core/src/services/import/notion/converter.ts
@@ -19,6 +19,7 @@ export function convertNotionHtml(html: string): string {
convertToggles(root);
unwrapDisplayContents(root);
convertTables(root);
+ convertImages(root);
convertCallouts(root);
convertBookmarks(root);
return root.toString();
@@ -245,6 +246,26 @@ function stripTableAttributes(table: HTMLElement) {
}
// #endregion
+// #region Images
+/**
+ * Notion image blocks are `<figure class="image"><a href="…"><img src="…"></a></figure>`, where the
+ * `<a>` is a self-link to the file and the `<img>` carries an inline pixel width. Reduce each to Trilium's
+ * canonical `<figure class="image"><img src="…"></figure>` (unwrap the link, drop the sizing and the
+ * Notion id). The `src` still points at the zip-relative path; the importer rewrites it to an attachment.
+ */
+function convertImages(root: HTMLElement) {
+ for (const figure of root.querySelectorAll("figure.image")) {
+ const img = figure.querySelector("img");
+ if (!img) {
+ continue;
+ }
+ img.removeAttribute("style");
+ figure.removeAttribute("id");
+ figure.set_content(img.toString());
+ }
+}
+// #endregion
+
// #region Callouts
/** Notion's default callout icon; it maps 1:1 to a "tip" admonition, so the emoji itself is redundant. */
const DEFAULT_CALLOUT_EMOJI = "💡";
diff --git a/packages/trilium-core/src/services/import/notion/importer.spec.ts b/packages/trilium-core/src/services/import/notion/importer.spec.ts
new file mode 100644
index 0000000000..3ae3e0eae2
--- /dev/null
+++ b/packages/trilium-core/src/services/import/notion/importer.spec.ts
@@ -0,0 +1,19 @@
+import { describe, expect, it } from "vitest";
+
+import { resolveResourcePath } from "./importer.js";
+
+describe("resolveResourcePath", () => {
+ it("resolves an image path relative to the page's directory in the zip", () => {
+ expect(resolveResourcePath("Export/Page abc.html", "Page%20folder/img.png"))
+ .toBe("Export/Page folder/img.png");
+ });
+
+ it("handles a page at the zip root", () => {
+ expect(resolveResourcePath("Page abc.html", "sub/img.png")).toBe("sub/img.png");
+ });
+
+ it("decodes percent-encoding and resolves . and .. segments", () => {
+ expect(resolveResourcePath("a/b/Page.html", "../c/img%20with%20space.png"))
+ .toBe("a/c/img with space.png");
+ });
+});
diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.ts
index b1cba3081e..b5e56b2529 100644
--- a/packages/trilium-core/src/services/import/notion/importer.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.ts
@@ -16,6 +16,7 @@ import { t } from "i18next";
import { type HTMLElement, parse } from "node-html-parser";
import type BNote from "../../../becca/entities/bnote.js";
+import imageService from "../../image.js";
import noteService from "../../notes.js";
import protectedSessionService from "../../protected_session.js";
import { sanitizeHtml } from "../../sanitizer.js";
@@ -40,34 +41,41 @@ interface ParsedPage {
}
async function importNotion(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote): Promise<BNote> {
- const pages = await parsePages(fileBuffer);
+ const { pages, resources } = await parseZip(fileBuffer);
taskContext.setTotalCount(pages.length);
- return createNotes(importRootNote, pages, taskContext);
+ return createNotes(importRootNote, pages, resources, taskContext);
}
-/** Reads every page (.html entry) out of the zip, parsing its title, ancestry, dates and body HTML. */
-async function parsePages(fileBuffer: Uint8Array): Promise<ParsedPage[]> {
+/**
+ * Reads the zip once: HTML entries become parsed pages; every other file (images, attachments) is kept
+ * by its normalized path so page content can later reference it. The `index.html` summary is skipped.
+ */
+async function parseZip(fileBuffer: Uint8Array): Promise<{ pages: ParsedPage[]; resources: Map<string, Uint8Array> }> {
const provider = getZipProvider();
const filenameEncoding = await provider.detectFilenameEncoding(fileBuffer);
const pages: ParsedPage[] = [];
+ const resources = new Map<string, Uint8Array>();
await provider.readZipFile(fileBuffer, async (entry, readContent) => {
const path = entry.fileName;
- // Notion exports the page body as HTML; everything else (images, the per-database CSV, the
- // summary index.html) is deferred to a later iteration.
- if (!path.toLowerCase().endsWith(".html") || isDirectory(path) || baseName(path) === "index.html") {
+ if (isDirectory(path)) {
return;
}
-
- const html = new TextDecoder().decode(await readContent());
- const parsed = parsePage(path, html);
- if (parsed) {
- pages.push(parsed);
+ if (path.toLowerCase().endsWith(".html")) {
+ if (baseName(path) === "index.html") {
+ return;
+ }
+ const parsed = parsePage(path, new TextDecoder().decode(await readContent()));
+ if (parsed) {
+ pages.push(parsed);
+ }
+ } else {
+ resources.set(normalizePath(path), await readContent());
}
}, filenameEncoding);
- return pages;
+ return { pages, resources };
}
function parsePage(path: string, html: string): ParsedPage | null {
@@ -102,7 +110,7 @@ function parsePage(path: string, html: string): ParsedPage | null {
* page's parent always exists before it; each page is parented under the note created for its
* immediate-parent id, or under the import root when it has no (resolvable) parent. Returns that root.
*/
-function createNotes(importRootNote: BNote, pages: ParsedPage[], taskContext: TaskContext<"importNotes">): BNote {
+function createNotes(importRootNote: BNote, pages: ParsedPage[], resources: Map<string, Uint8Array>, taskContext: TaskContext<"importNotes">): BNote {
const isProtected = importRootNote.isProtected && protectedSessionService.isProtectedSessionAvailable();
const rootNote = noteService.createNewNote({ parentNoteId: importRootNote.noteId, title: t("notion_import.root-title"), content: "", type: "text", mime: "text/html", isProtected }).note;
@@ -125,7 +133,14 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], taskContext: Ta
});
noteIdByPageId.set(page.id, note.noteId);
- // Preserve Notion's original timestamps. Must run after createNewNote's content save, which would
+ // Save referenced images as attachments and point their <img> at them. Done after creation, since
+ // attachments hang off the note; re-saves the content only when an image was actually rewritten.
+ const rewritten = rewriteImages(note, page.content, page.path, resources);
+ if (rewritten !== page.content) {
+ note.setContent(rewritten);
+ }
+
+ // Preserve Notion's original timestamps. Must run after the content save above, which would
// otherwise re-stamp the modification date with "now".
if (page.utcDateCreated || page.utcDateModified) {
note.setDateCreatedAndModified(page.utcDateCreated, page.utcDateModified ?? page.utcDateCreated);
@@ -137,6 +152,67 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], taskContext: Ta
return rootNote;
}
+/**
+ * Saves each in-zip image a page references as an attachment on `note` and rewrites the `<img src>` to
+ * point at it. References that don't resolve to a bundled file (e.g. external image URLs) are left as-is.
+ * Returns the (possibly unchanged) content.
+ */
+function rewriteImages(note: BNote, content: string, pagePath: string, resources: Map<string, Uint8Array>): string {
+ const root = parse(content);
+ let changed = false;
+
+ for (const img of root.querySelectorAll("img")) {
+ const src = img.getAttribute("src");
+ if (!src) {
+ continue;
+ }
+ const resourcePath = resolveResourcePath(pagePath, src);
+ const bytes = resources.get(resourcePath);
+ if (!bytes) {
+ continue;
+ }
+ const { attachmentId, title } = imageService.saveImageToAttachment(note.noteId, bytes, baseName(resourcePath), false);
+ if (attachmentId) {
+ img.setAttribute("src", `api/attachments/${attachmentId}/image/${encodeURIComponent(title)}`);
+ changed = true;
+ }
+ }
+
+ return changed ? root.toString() : content;
+}
+
+/**
+ * Resolves an `<img src>` (zip-relative, percent-encoded) against the page's directory in the zip,
+ * returning a normalized path that matches the keys collected in {@link parseZip}.
+ */
+export function resolveResourcePath(pagePath: string, src: string): string {
+ let decoded = src;
+ try {
+ decoded = decodeURIComponent(src);
+ } catch {
+ // Leave malformed percent-encoding as-is rather than throwing.
+ }
+ const lastSlash = pagePath.lastIndexOf("/");
+ const baseDir = lastSlash >= 0 ? pagePath.slice(0, lastSlash) : "";
+ return normalizePath(baseDir ? `${baseDir}/${decoded}` : decoded);
+}
+
+/** Collapses `.`/`..` segments in a forward-slash zip path and drops empty segments. */
+function normalizePath(path: string): string {
+ const parts: string[] = [];
+ for (const segment of path.split("/")) {
+ if (segment === "" || segment === ".") {
+ continue;
+ }
+ if (segment === "..") {
+ parts.pop();
+ } else {
+ parts.push(segment);
+ }
+ }
+ return parts.join("/");
+}
+
/** Returns the Notion id of the first child element of `body` that carries one (Notion's page wrapper). */
function firstChildNotionId(body: HTMLElement | null): string | undefined {
if (!body) {