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 place and person columns
Elian Doran8e3ede4ceec1
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 e61aafec14..6632796843 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@@ -588,6 +588,45 @@ describe("Notion importer — integration", () => {expect(off?.getOwnedLabelValue("Checkbox")).toBe("false");});+ it("imports a place column as a single-valued text label", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ const props = `<table class="properties"><tbody><tr class="property-row property-row-place"><th><span class="icon property-icon"><img src="x.svg"/></span>Place</th><td>Rotterdam, South Holland, Netherlands</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}">${props}<div class="page-body"><p>x</p></div></div></body></html>`+ });++ const db = importRoot.getChildNotes().find((n) => n.title === "DB");+ expect(db?.getOwnedLabel("label:Place")?.value).toBe("promoted,single,text,alias=Place");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ // The comma-bearing value is stored verbatim (it's a label value, not part of the definition).+ expect(row?.getOwnedLabelValue("Place")).toBe("Rotterdam, South Holland, Netherlands");+ });++ it("imports a person column as multi-valued labels, stripping the avatar initial", async () => {+ const dbId = "388c5eca1b8b8078a20fd18330d81306";+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";+ // Real markup: each person is a <span class="user"> whose .user-icon holds an avatar initial.+ const user = (initial: string, fullName: string) =>+ `<span class="user"><span class="icon text-icon user-icon"><span class="user-icon-inner">${initial}</span></span>${fullName}</span>`;+ const props = `<table class="properties"><tbody><tr class="property-row property-row-person"><th><span class="icon property-icon"><img src="x.svg"/></span>Person</th><td>${user("E", "Elian Doran")}, ${user("A", "Ada Lovelace")}</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}">${props}<div class="page-body"><p>x</p></div></div></body></html>`+ });++ const db = importRoot.getChildNotes().find((n) => n.title === "DB");+ expect(db?.getOwnedLabel("label:Person")?.value).toBe("promoted,multi,text,alias=Person");+ const row = db?.getChildNotes().find((n) => n.title === "Row");+ // The avatar initials ("E", "A") are dropped, leaving just the names, one label per person.+ expect(row?.getOwnedLabels("Person").map((l) => l.value)).toEqual(["Elian Doran", "Ada Lovelace"]);+ });+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 685d4d8296..f22fa51a97 100644--- a/packages/trilium-core/src/services/import/notion/importer.ts+++ b/packages/trilium-core/src/services/import/notion/importer.ts@@ -566,11 +566,12 @@ export function firstChildNotionId(body: HTMLElement | null): string | 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. Handled so far:- * - `text` / `select` / `status`: the cell's text → one single-valued property;+ * - `text` / `select` / `status` / `place`: the cell's text → one single-valued property;* - `multi_select`: each `<span class="selected-value">` option → one entry of a multi-valued property;* - `url` / `email` / `phone_number`: the anchor's href → one single-valued url-typed property (email gets `mailto:`, phone `tel:`);* - `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.+ * - `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.* 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.@@ -585,8 +586,8 @@ function extractProperties(root: HTMLElement): NotionProperty[] {}const type = row.getAttribute("class")?.match(/property-row-(\w+)/)?.[1];- if (type === "text" || type === "select" || type === "status") {- // `select`/`status` are a single chosen option; their cell text is the whole value (a status'+ if (type === "text" || type === "select" || type === "status" || type === "place") {+ // `select`/`status`/`place` resolve to plain text: the cell text is the whole value (a status'// leading `<div class="status-dot">` carries no text), so they take the free-text single path.const value = cell.textContent?.trim();if (value) {@@ -614,6 +615,16 @@ function extractProperties(root: HTMLElement): NotionProperty[] {const value = checkbox.classList.contains("checkbox-on") ? "true" : "false";properties.push({ name, value, labelType: "boolean", multiplicity: "single" });}+ } else if (type === "person") {+ // A person column can list several users; each is a `<span class="user">` whose leading avatar+ // (`.user-icon`, e.g. an initial) would otherwise bleed into the name, so drop it first.+ for (const user of cell.querySelectorAll("span.user")) {+ user.querySelector(".user-icon")?.remove();+ const value = user.textContent?.trim();+ if (value) {+ properties.push({ name, value, labelType: "text", multiplicity: "multi" });+ }+ }}}return properties;