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
fix(import/notion): trim CSV headers and harden the promoted-attribute alias
Elian Doran6ec1aec61274
diff --git a/packages/trilium-core/src/services/import/notion/collection.ts b/packages/trilium-core/src/services/import/notion/collection.tsindex 4c6bc2aa82..7f79d3ad16 100644--- a/packages/trilium-core/src/services/import/notion/collection.ts+++ b/packages/trilium-core/src/services/import/notion/collection.ts@@ -373,8 +373,9 @@ function orderColumns(properties: NotionProperty[], csvColumns: string[] | undef/** Builds a promoted-attribute definition value (e.g. `promoted,single,text,alias=Text column`). */function buildPromotedDefinition({ name, labelType, multiplicity }: NotionProperty): string {// The alias keeps the original (pretty) column name in the UI while the attribute name stays sanitized.- // The definition is comma/`=`-delimited, so neutralize those characters in the alias to avoid corrupting it.- const alias = name.replace(/[,=]/g, " ").trim();+ // The definition is a single-line, comma/`=`-delimited string, so neutralize those delimiters and any+ // control character (e.g. a stray newline) in the alias to avoid corrupting it.+ const alias = name.replace(/[\x00-\x1f,=]/g, " ").trim();// A relation has no value type; a label carries one (text/date/url/boolean/…).const type = labelType ? `${labelType},` : "";return `promoted,${multiplicity},${type}alias=${alias}`;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 fd737c20c9..20f4babca7 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@@ -557,8 +557,9 @@ describe("Notion importer — integration", () => {});it("orders the promoted definitions by the CSV column order, not row discovery order", async () => {- // The CSV header is the authoritative column order: Zeta, Alpha, Mu.- const csv = "Name,Zeta,Alpha,Mu\nRow1,,a,m\nRow2,z,,\n";+ // The CSV header is the authoritative column order: Zeta, Alpha, Mu (with stray padding around some+ // names, which must be trimmed to match the trimmed HTML property headers).+ const csv = "Name, Zeta , Alpha,Mu\nRow1,,a,m\nRow2,z,,\n";const row1Id = "388c5eca1b8b80929a78da7c68154bd7";const row2Id = "388c5eca1b8b80e5903ef480b0523eb1";const textCol = (col: string, val: string) => `<tr class="property-row property-row-text"><th>${col}</th><td>${val}</td></tr>`;@@ -698,10 +699,11 @@ describe("Notion importer — integration", () => {expect(row?.getOwnedLabels("Files___media")).toHaveLength(0);});- it("neutralizes commas in a column name so the alias can't corrupt the definition", async () => {+ it("neutralizes commas and control characters in a column name so the alias can't corrupt the definition", async () => {const dbId = "388c5eca1b8b8078a20fd18330d81306";const rowId = "388c5eca1b8b80929a78da7c68154bd7";- const props = `<table class="properties"><tbody><tr class="property-row property-row-text"><th>Weight, kg</th><td>5</td></tr></tbody></table>`;+ // The name carries a comma and a newline — both would break the single-line, comma-delimited definition.+ const props = `<table class="properties"><tbody><tr class="property-row property-row-text"><th>Weight,\nkg</th><td>5</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>`,@@ -710,7 +712,7 @@ describe("Notion importer — integration", () => {});const db = importRoot.getChildNotes().find((n) => n.title === "DB");- // The comma in "Weight, kg" becomes a space in the alias, so the definition keeps exactly four tokens.+ // Both the comma and the newline become spaces, so the definition stays one line of four tokens.expect(db?.getOwnedLabel("label:Weight__kg")?.value).toBe("promoted,single,text,alias=Weight kg");});diff --git a/packages/trilium-core/src/services/import/notion/importer.ts b/packages/trilium-core/src/services/import/notion/importer.tsindex 4e8cf69fbf..02268ce7ad 100644--- a/packages/trilium-core/src/services/import/notion/importer.ts+++ b/packages/trilium-core/src/services/import/notion/importer.ts@@ -84,7 +84,9 @@ async function parseZip(fileBuffer: Uint8Array): Promise<{ pages: ParsedPage[];// own page), and its header row lists every column in the database's order.csvPaths.push(path);const [header = []] = parseCsv(new TextDecoder().decode(await readContent()));- csvColumnsByFolder.set(ownedFolderKey(path), header.map((column) => sanitizeAttributeName(column)));+ // Trim before sanitizing so a padded CSV header (`Name, Age , …`) still matches the HTML+ // property `<th>` text, which is trimmed on extraction.+ csvColumnsByFolder.set(ownedFolderKey(path), header.map((column) => sanitizeAttributeName(column.trim())));} else {resources.set(normalizePath(path), await readContent());}