Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/notion): map created-by/last-edited-by to text labels

Elian Doranf05838e5ce05
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.ts
index 5d9589c852..37b96392cf 100644
--- a/packages/trilium-core/src/services/import/notion/collection.ts
+++ b/packages/trilium-core/src/services/import/notion/collection.ts
@@ -75,6 +75,7 @@ export function resolveDatabaseContainers(pages: ParsedPage[], csvPaths: string[
* - `formula` / `rollup`: a computed value with no type signal, inferred from the rendered cell shape —
* checkbox → boolean, else numeric text → number, else text (a multi-value rollup collapses to one text value);
* - `person`: each `<span class="user">` name (its avatar stripped) → an entry of a multi-valued property;
+ * - `created_by` / `last_edited_by`: the single `<span class="user">` name → one single-valued text property;
* - `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).
* Blank names/values are skipped (Notion sometimes emits an empty cell, e.g. an unset multi-select, which
@@ -134,14 +135,15 @@ export function extractProperties(root: HTMLElement): NotionProperty[] {
} else if (type === "formula" || type === "rollup") {
properties.push(...extractComputedValue(name, cell));
} 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" });
- }
+ // A person column can list several users, so each name is its own multi-valued entry.
+ for (const value of extractUserNames(cell)) {
+ properties.push({ name, value, labelType: "text", multiplicity: "multi" });
+ }
+ } else if (type === "created_by" || type === "last_edited_by") {
+ // Creator / last-editor metadata is rendered like a person cell but is always a single user.
+ const [value] = extractUserNames(cell);
+ if (value) {
+ properties.push({ name, value, labelType: "text", multiplicity: "single" });
}
} else if (type === "relation") {
// Each linked page is an `<a>` whose href carries the target's Notion id; the second pass resolves
@@ -180,6 +182,23 @@ function toNumberValue(text: string | null | undefined): string | undefined {
return normalized !== "" && Number.isFinite(Number(normalized)) ? normalized : trimmed;
}
+/**
+ * Reads the user names from a Notion people cell (`person`, `created_by`, `last_edited_by`). Each user is a
+ * `<span class="user">` whose leading avatar (`.user-icon`, e.g. an initial) would otherwise bleed into the
+ * name, so it's dropped first. Blank entries are skipped.
+ */
+function extractUserNames(cell: HTMLElement): string[] {
+ const names: string[] = [];
+ for (const user of cell.querySelectorAll("span.user")) {
+ user.querySelector(".user-icon")?.remove();
+ const name = user.textContent?.trim();
+ if (name) {
+ names.push(name);
+ }
+ }
+ return names;
+}
+
/** Reads a Notion checkbox cell (`<div class="checkbox checkbox-on|off">`) as a `true`/`false` boolean label. */
function toBooleanProperty(name: string, cell: HTMLElement): NotionProperty | undefined {
const checkbox = cell.querySelector("div.checkbox");
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 1332976cce..bc4db2fcd7 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
@@ -579,6 +579,30 @@ describe("Notion importer — integration", () => {
expect(row?.getOwnedLabelValue("Count")).toBe("2");
});
+ it("imports created-by and last-edited-by as single-valued text labels of the user name", async () => {
+ const dbId = "388c5eca1b8b8078a20fd18330d81306";
+ const rowId = "388c5eca1b8b80929a78da7c68154bd7";
+ // Both render like a person cell: a <span class="user"> with a leading avatar (an initial) to strip.
+ const user = `<span class="user"><span class="icon text-icon user-icon"><span class="user-icon-inner">E</span></span>Elian Doran</span>`;
+ const rows =
+ `<tr class="property-row property-row-created_by"><th><span class="icon property-icon"><img src="x.svg"/></span>Created by</th><td>${user}</td></tr>` +
+ `<tr class="property-row property-row-last_edited_by"><th>Last edited by</th><td>${user}</td></tr>`;
+ const props = `<table class="properties"><tbody>${rows}</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:Created_by")?.value).toBe("promoted,single,text,alias=Created by");
+ expect(db?.getOwnedLabel("label:Last_edited_by")?.value).toBe("promoted,single,text,alias=Last edited by");
+ const row = db?.getChildNotes().find((n) => n.title === "Row");
+ expect(row?.getOwnedLabelValue("Created_by")).toBe("Elian Doran");
+ expect(row?.getOwnedLabelValue("Last_edited_by")).toBe("Elian Doran");
+ });
+
it("imports url, email and phone columns as url-typed labels (mailto:/tel: schemes)", async () => {
const dbId = "388c5eca1b8b8078a20fd18330d81306";
const rowId = "388c5eca1b8b80929a78da7c68154bd7";