Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/notion): link file-column files from row content

Elian Doranb05d7cf1f68b
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.ts
index d45adaad72..10aafa5e65 100644
--- a/packages/trilium-core/src/services/import/notion/collection.ts
+++ b/packages/trilium-core/src/services/import/notion/collection.ts
@@ -13,7 +13,7 @@ import { dayjs } from "@triliumnext/commons";
import type { HTMLElement } from "node-html-parser";
import type BNote from "../../../becca/entities/bnote.js";
-import { sanitizeAttributeName } from "../../utils/index.js";
+import { escapeHtml, sanitizeAttributeName } from "../../utils/index.js";
import mimeService from "../mime.js";
import type { LinkTarget, NotionProperty, ParsedPage } from "./model.js";
import { getNotionId, stripNotionId } from "./notion_id.js";
@@ -257,15 +257,24 @@ function dateColumnKey(path: string, name: string): string {
* Applies a page's own property values to its note: file columns become `role:"file"` attachments, every
* other (non-relation) column becomes a label. Relations are deferred to {@link applyRelationProperties},
* which runs once every target note exists.
+ *
+ * Returns HTML to prepend to the note's body: one reference-link paragraph per bundled file, so a file
+ * column's files are reachable from the row's content, not only from its attachments list. Empty when the
+ * row attaches no bundled file.
*/
-export function applyOwnedProperties(note: BNote, page: ParsedPage, resources: Map<string, Uint8Array>) {
+export function applyOwnedProperties(note: BNote, page: ParsedPage, resources: Map<string, Uint8Array>): string {
+ const fileLinks: string[] = [];
for (const property of page.properties) {
if (property.kind === "file") {
- saveFileAttachment(note, property.value, page.path, resources);
+ const attachment = saveFileAttachment(note, property.value, page.path, resources);
+ if (attachment?.attachmentId) {
+ fileLinks.push(`<p>${attachmentReferenceLink(note.noteId, attachment.attachmentId, attachment.title)}</p>`);
+ }
} else if (property.kind !== "relation") {
note.addLabel(sanitizeAttributeName(property.name), property.value);
}
}
+ return fileLinks.join("");
}
/**
@@ -284,19 +293,20 @@ export function applyRelationProperties(note: BNote, page: ParsedPage, resolve:
}
/**
- * 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.
+ * Saves a File-property reference as a `role:"file"` attachment on `note` and returns it (or `undefined` if
+ * the file isn't bundled). `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;
+ return undefined;
}
const title = baseName(resourcePath);
- note.saveAttachment({
+ return note.saveAttachment({
role: "file",
mime: mimeService.getMime(title) || "application/octet-stream",
title,
@@ -304,6 +314,11 @@ function saveFileAttachment(note: BNote, href: string, pagePath: string, resourc
});
}
+/** Builds a Trilium attachment reference-link (the chip CKEditor renders) — the shape rewriteAttachments produces. */
+function attachmentReferenceLink(noteId: string, attachmentId: string, title: string): string {
+ return `<a class="reference-link" href="#root/${noteId}?viewMode=attachments&attachmentId=${attachmentId}">${escapeHtml(title)}</a>`;
+}
+
/**
* A Notion database's columns are a schema shared by every row, so each column becomes a single
* *inheritable* promoted-attribute definition on the row's container note (the database) — not a copy on
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.ts
index bb6a17dbe5..6c19af11f2 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
@@ -735,7 +735,14 @@ describe("Notion importer — integration", () => {
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.
+ const attachment = row?.getAttachmentsByRole("file").find((a) => a.title === "report.pdf");
expect(row?.getAttachmentsByRole("file").map((a) => a.title)).toEqual(["report.pdf"]);
+ // The bundled file is also a reference-link prepended before the original body; the external one,
+ // having no attachment, contributes no link (so there's exactly one).
+ const content = String(row?.getContent() ?? "");
+ expect(content).toContain(`<a class="reference-link" href="#root/${row?.noteId}?viewMode=attachments&attachmentId=${attachment?.attachmentId}">report.pdf</a>`);
+ expect(content.match(/reference-link/g)).toHaveLength(1);
+ expect(content.indexOf("reference-link")).toBeLessThan(content.indexOf("<p>x</p>"));
// 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);
diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.ts
index 2b31bcd7fc..1d30d71a47 100644
--- a/packages/trilium-core/src/services/import/notion/importer.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.ts
@@ -168,12 +168,14 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], resources: Map<
}
// Carry the page's own database property values over (file columns become attachments, the rest
- // labels); relations are deferred to the second pass, once every target note exists.
- applyOwnedProperties(note, page, resources);
+ // labels); relations are deferred to the second pass, once every target note exists. A file column
+ // also returns reference-links, prepended to the body so its files are reachable from the content.
+ const fileLinks = applyOwnedProperties(note, page, resources);
+ const body = fileLinks + page.content;
// Attachments hang off the note, so this must run after creation; it returns the content with the
// <img> srcs and file links pointing at the saved attachments.
- const withImages = rewriteImages(note, page.content, page.path, resources);
+ const withImages = rewriteImages(note, body, page.path, resources);
created.push({ note, content: rewriteAttachments(note, withImages, page.path, resources), page });
taskContext.increaseProgressCount();
}