Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(import/notion): import file columns as role:file attachments
Elian Doranff400362958f
diff --git a/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts b/packages/trilium-core/src/services/import/notion/importer.integration.spec.tsindex 68d991e6bb..181b92f826 100644--- a/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts+++ b/packages/trilium-core/src/services/import/notion/importer.integration.spec.ts@@ -661,6 +661,28 @@ describe("Notion importer — integration", () => {expect(bar?.getOwnedRelations("Related").map((r) => r.value)).toEqual([bar?.noteId]);});+ it("saves a file column's bundled files as role:file attachments, skipping external links and adding no definition", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ // A file cell links a bundled file plus an external URL that isn't in the zip.+ const fileCell = `<table class="properties"><tbody><tr class="property-row property-row-file"><th><span class="icon property-icon"><img src="x.svg"/></span>Files & media</th><td><span><a href="report.pdf">report.pdf</a></span><span><a href="https://example.com/external.pdf">external.pdf</a></span></td></tr></tbody></table>`;+ const importRoot = await importNotion({+ "DB 388c5eca1b8b8078a20fd18330d81306.html":+ `<html><head><title>DB</title></head><body><div id="${dbId}"><div class="page-body"></div></div></body></html>`,+ "DB/Row 388c5eca1b8b80929a78da7c68154bd7.html":+ `<html><head><title>Row</title></head><body><div id="${rowId}">${fileCell}<div class="page-body"><p>x</p></div></div></body></html>`,+ "DB/report.pdf": "%PDF-1.4 fake"+ });++ const db = importRoot.getChildNotes().find((n) => n.title === "DB");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ // The bundled file becomes a role:file attachment; the external link (not in the zip) is skipped.+ expect(row?.getAttachmentsByRole("file").map((a) => a.title)).toEqual(["report.pdf"]);+ // A file column is content, not metadata — no promoted definition and no value label.+ expect(db?.getOwnedLabel("label:Files___media")).toBeFalsy();+ expect(row?.getOwnedLabels("Files___media")).toHaveLength(0);+ });+it("neutralizes commas in a column name so the alias can't corrupt the definition", async () => {const dbId = "388c5eca1b8b8078a20fd18330d81306";const rowId = "388c5eca1b8b80929a78da7c68154bd7";diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.tsindex 74500806ac..ba6c3ab603 100644--- a/packages/trilium-core/src/services/import/notion/importer.ts+++ b/packages/trilium-core/src/services/import/notion/importer.ts@@ -39,8 +39,11 @@ interface NotionProperty {labelType?: LabelType;/** Whether the column holds one value or many (e.g. multi-select); sets the definition's multiplicity. */multiplicity: Multiplicity;- /** Set for a relation column: its `value` is a target Notion id, mapped to a `~relation` in the second pass. */- kind?: "relation";+ /**+ * For a relation column, `value` is a target Notion id mapped to a `~relation` in the second pass; for a+ * file column, `value` is a zip-relative href saved as a `role:"file"` attachment. A plain label has neither.+ */+ kind?: "relation" | "file";}interface ParsedPage {@@ -243,9 +246,11 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], resources: Map<targetByPageId.set(page.id, { noteId: note.noteId, title: page.title });// Carry the page's Notion database properties over as Trilium attributes (e.g. a "Text column" →- // #Text_column). Relations are deferred to the second pass, once every target note exists.+ // #Text_column). File columns become attachments; relations are deferred to the second pass.for (const property of page.properties) {- if (property.kind !== "relation") {+ if (property.kind === "file") {+ saveFileAttachment(note, property.value, page.path, resources);+ } else if (property.kind !== "relation") {note.addLabel(sanitizeAttributeName(property.name), property.value);}}@@ -314,6 +319,9 @@ function applyDatabaseSchemas(pages: ParsedPage[], noteByFolder: Map<string, BNoschemaByFolder.set(folderKey, schema);}for (const property of page.properties) {+ if (property.kind === "file") {+ continue; // files become attachments, not promoted definitions+ }const labelName = sanitizeAttributeName(property.name);if (!schema.has(labelName)) {schema.set(labelName, property);@@ -446,6 +454,27 @@ function rewriteAttachments(note: BNote, content: string, pagePath: string, resoreturn changed ? root.toString() : content;}+/**+ * Saves a File-property reference as a `role:"file"` attachment on `note`. `href` is the value of one+ * `<a>` in the file cell; it's resolved against the zip the same way page content is, so only files bundled+ * in the export attach — an external link (or a missing file) is silently skipped.+ */+function saveFileAttachment(note: BNote, href: string, pagePath: string, resources: Map<string, Uint8Array>) {+ const resourcePath = resolveResourcePath(pagePath, href);+ const bytes = resources.get(resourcePath);+ if (!bytes) {+ return;+ }++ const title = baseName(resourcePath);+ note.saveAttachment({+ role: "file",+ mime: mimeService.getMime(title) || "application/octet-stream",+ title,+ content: bytes+ });+}+/** A resolved import target: the note created for a Notion page, plus that page's title. */export interface LinkTarget {noteId: string;@@ -593,7 +622,8 @@ export function firstChildNotionId(body: HTMLElement | null): string | undefined* - `date`: the `<time>` value → a `date`/`datetime` label; a range adds a separate `<name> end` column;* - `checkbox`: `checkbox-on`/`checkbox-off` → a `true`/`false` boolean label;* - `person`: each `<span class="user">` name (its avatar stripped) → an entry of a multi-valued property;- * - `relation`: each linked page's `<a>` href → a multi-valued relation, resolved to a note in the second pass.+ * - `relation`: each linked page's `<a>` href → a multi-valued relation, resolved to a note in the second pass;+ * - `file`: each `<a>` href → a `role:"file"` attachment on the note (no promoted definition — it's content).* The importer turns each `{ name, value }` into a Trilium label; blank names/values are skipped (Notion* sometimes emits an empty cell, e.g. an unset multi-select, which should contribute no label). Other types* (dates handled separately by extractDate) fall through untouched.@@ -656,6 +686,14 @@ function extractProperties(root: HTMLElement): NotionProperty[] {properties.push({ name, value: targetId, multiplicity: "multi", kind: "relation" });}}+ } else if (type === "file") {+ // Each `<a>` href points at a bundled file; the first pass saves it as a `role:"file"` attachment.+ for (const anchor of cell.querySelectorAll("a")) {+ const href = anchor.getAttribute("href");+ if (href) {+ properties.push({ name, value: href, multiplicity: "multi", kind: "file" });+ }+ }}}return properties;