Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/notion): map text page properties to Trilium labels

Elian Doran33fe0b2ea74e
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 018ba7521d..251067372a 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
@@ -313,6 +313,42 @@ describe("Notion importer — integration", () => {
expect(undated?.utcDateCreated.startsWith("2024-01-02")).toBe(false);
});
+ it("imports a page's text property as a Trilium label", async () => {
+ const id = "2c6c5eca1b8b80f7b9eaf4f396b755dc";
+ // Real Notion markup: the <th> leads with an icon span (no text) before the column name.
+ const propertyTable =
+ `<table class="properties"><tbody>` +
+ `<tr class="property-row property-row-text"><th><span class="icon property-icon"><img src="x.svg"/></span>Text column</th><td>Basic text</td></tr>` +
+ `</tbody></table>`;
+ const importRoot = await importNotion({
+ "Texty 2c6c5eca1b8b80f7b9eaf4f396b755dc.html":
+ `<html><head><title>Texty</title></head><body><div id="${id}">${propertyTable}<div class="page-body"><p>x</p></div></div></body></html>`
+ });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "Texty");
+ // The column name is sanitized (space → underscore, case preserved); the value is kept verbatim.
+ expect(note?.getOwnedLabelValue("Text_column")).toBe("Basic text");
+ });
+
+ it("sanitizes illegal characters in a text property's name and skips blank values", async () => {
+ const id = "2c6c5eca1b8b80f7b9eaf4f396b755dc";
+ const propertyTable =
+ `<table class="properties"><tbody>` +
+ `<tr class="property-row property-row-text"><th>Sub-title (v2)</th><td>Hello world</td></tr>` +
+ `<tr class="property-row property-row-text"><th>Empty</th><td></td></tr>` +
+ `</tbody></table>`;
+ const importRoot = await importNotion({
+ "Mixed 2c6c5eca1b8b80f7b9eaf4f396b755dc.html":
+ `<html><head><title>Mixed</title></head><body><div id="${id}">${propertyTable}<div class="page-body"><p>x</p></div></div></body></html>`
+ });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "Mixed");
+ // Every char outside [\p{L}\p{N}_:] becomes an underscore: "Sub-title (v2)" → "Sub_title__v2_".
+ expect(note?.getOwnedLabelValue("Sub_title__v2_")).toBe("Hello world");
+ // The blank-valued row contributes no label.
+ expect(note?.hasOwnedLabel("Empty")).toBe(false);
+ });
+
it("saves a bundled image as an attachment and rewrites its src; leaves external/srcless images alone", async () => {
const id = "2c6c5eca1b8b80f7b9eaf4f396b755dc";
const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.ts
index 3a0db997cd..46a50435b1 100644
--- a/packages/trilium-core/src/services/import/notion/importer.ts
+++ b/packages/trilium-core/src/services/import/notion/importer.ts
@@ -22,11 +22,19 @@ import protectedSessionService from "../../protected_session.js";
import { sanitizeHtml } from "../../sanitizer.js";
import type TaskContext from "../../task_context.js";
import dateUtils from "../../utils/date.js";
+import { sanitizeAttributeName } from "../../utils/index.js";
import { getZipProvider } from "../../zip_provider.js";
import mimeService from "../mime.js";
import { convertNotionHtml } from "./converter.js";
import { getNotionId, stripNotionId } from "./notion_id.js";
+/** A database column value lifted from a page's Notion properties table, destined for a Trilium label. */
+interface NotionProperty {
+ /** The column name, taken verbatim from the property row's `<th>` (sanitized only when made a label). */
+ name: string;
+ value: string;
+}
+
interface ParsedPage {
/** The page's own Notion id, used to resolve cross-page links pointing at it. */
id: string;
@@ -35,6 +43,8 @@ interface ParsedPage {
path: string;
/** The page's body HTML, sanitized; empty when the body could not be located. */
content: string;
+ /** Database properties from the page's Notion properties table, mapped to Trilium labels on import. */
+ properties: NotionProperty[];
utcDateCreated?: string;
utcDateModified?: string;
}
@@ -65,7 +75,8 @@ function addDatabaseContainers(pages: ParsedPage[], csvPaths: string[]) {
id: getNotionId(baseName(csvPath)) ?? "",
title: stripNotionId(removeExtension(baseName(csvPath))) || "Database",
path: csvPath,
- content: ""
+ content: "",
+ properties: []
});
}
}
@@ -140,6 +151,7 @@ function parsePage(path: string, html: string): ParsedPage | null {
title,
path,
content,
+ properties: extractProperties(root),
utcDateCreated: extractDate(root, "property-row-created_time"),
utcDateModified: extractDate(root, "property-row-last_edited_time")
};
@@ -180,6 +192,11 @@ function createNotes(importRootNote: BNote, pages: ParsedPage[], resources: Map<
noteIdByFolder.set(ownedFolderKey(page.path), note.noteId);
targetByPageId.set(page.id, { noteId: note.noteId, title: page.title });
+ // Carry the page's Notion database properties over as Trilium labels (e.g. a "Text column" → #Text_column).
+ for (const { name, value } of page.properties) {
+ note.addLabel(sanitizeAttributeName(name), value);
+ }
+
// 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);
@@ -406,6 +423,25 @@ export function firstChildNotionId(body: HTMLElement | null): string | undefined
return undefined;
}
+/**
+ * Reads a page's database properties from its Notion properties table. Each column is a
+ * `<tr class="property-row property-row-<type>">` whose `<th>` holds the column name (after an icon span,
+ * which carries no text) and `<td>` the value. This first cut handles only plain text columns
+ * (`property-row-text`); the importer turns each `{ name, value }` into a Trilium label. Rows with a blank
+ * name or value are skipped — Notion already omits empty cells per page, so this is just defensive.
+ */
+function extractProperties(root: HTMLElement): NotionProperty[] {
+ const properties: NotionProperty[] = [];
+ for (const row of root.querySelectorAll("table.properties tr.property-row-text")) {
+ const name = row.querySelector("th")?.textContent?.trim();
+ const value = row.querySelector("td")?.textContent?.trim();
+ if (name && value) {
+ properties.push({ name, value });
+ }
+ }
+ return properties;
+}
+
/**
* Reads a Notion property-row timestamp (created/last-edited) from the page's properties table and
* converts it to Trilium's UTC DB format. Returns undefined when the row, the <time> element or the