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): preserve Google Keep note colour as #color
Elian Doranb583572f6116
diff --git a/packages/trilium-core/src/services/import/keep/importer.spec.ts b/packages/trilium-core/src/services/import/keep/importer.spec.tsindex 9dcb29cd60..9ebf51c01f 100644--- a/packages/trilium-core/src/services/import/keep/importer.spec.ts+++ b/packages/trilium-core/src/services/import/keep/importer.spec.ts@@ -94,6 +94,17 @@ describe("Google Keep importer — parseNote", () => {expect(note?.utcDateModified).toBe("2026-06-21 08:14:14.353Z");});+ it("maps a Keep palette colour to its hex for the #color label", () => {+ expect(parseNote("n.json", JSON.stringify({ color: "GREEN" }))?.colorHex).toBe("#95d641");+ expect(parseNote("n.json", JSON.stringify({ color: "CERULEAN" }))?.colorHex).toBe("#82b1ff");+ });++ it("leaves colour undefined for Keep's default, an unknown colour, or no colour", () => {+ expect(parseNote("n.json", JSON.stringify({ color: "DEFAULT" }))?.colorHex).toBeUndefined();+ expect(parseNote("n.json", JSON.stringify({ color: "MAGENTA" }))?.colorHex).toBeUndefined();+ expect(parseNote("n.json", JSON.stringify({}))?.colorHex).toBeUndefined();+ });+it("leaves timestamps undefined and content empty for a bare note", () => {const note = parseNote("empty.json", JSON.stringify({}));diff --git a/packages/trilium-core/src/services/import/keep/importer.ts b/packages/trilium-core/src/services/import/keep/importer.tsindex e46fe21bff..f01ea895eb 100644--- a/packages/trilium-core/src/services/import/keep/importer.ts+++ b/packages/trilium-core/src/services/import/keep/importer.ts@@ -41,6 +41,8 @@ interface KeepNote {textContentHtml?: string;/** Body of a checklist note; mutually exclusive with the text fields. */listContent?: KeepListItem[];+ /** Note background colour, one of Keep's named palette entries ("DEFAULT" for none). */+ color?: string;/** Creation/last-edit timestamps, in microseconds since the Unix epoch. */createdTimestampUsec?: number;userEditedTimestampUsec?: number;@@ -50,6 +52,8 @@ interface ParsedNote {title: string;/** Body HTML; empty when the note has neither text nor list content. */content: string;+ /** Hex colour for Trilium's `#color` label, or undefined for Keep's default (no colour). */+ colorHex?: string;utcDateCreated?: string;utcDateModified?: string;}@@ -100,6 +104,7 @@ export function parseNote(path: string, json: string): ParsedNote | null {return {title,content: buildContent(note),+ colorHex: keepColorToHex(note.color),utcDateCreated: usecToUtc(note.createdTimestampUsec),utcDateModified: usecToUtc(note.userEditedTimestampUsec)};@@ -159,6 +164,10 @@ function createNotes(importRootNote: BNote, notes: ParsedNote[], taskContext: TaisProtected});+ if (parsed.colorHex) {+ note.addLabel("color", parsed.colorHex);+ }+// Preserve Keep's original timestamps. Must run after createNewNote's content save, which would// otherwise re-stamp the modification date with "now".if (parsed.utcDateCreated || parsed.utcDateModified) {@@ -171,6 +180,29 @@ function createNotes(importRootNote: BNote, notes: ParsedNote[], taskContext: Tareturn rootNote;}+/**+ * Keep's named note colours mapped to their exact palette hex (taken from the colour classes in Keep's own+ * exported `.html`). "DEFAULT" (no colour) is intentionally absent.+ */+const KEEP_COLOR_HEX: Record<string, string> = {+ RED: "#ff6d3f",+ ORANGE: "#ff9b00",+ YELLOW: "#ffda00",+ GREEN: "#95d641",+ TEAL: "#1ce8b5",+ BLUE: "#3fc3ff",+ CERULEAN: "#82b1ff",+ PURPLE: "#b388ff",+ PINK: "#f8bbd0",+ BROWN: "#d7ccc8",+ GRAY: "#b8c4c9"+};++/** Maps a Keep note colour to its Trilium `#color` hex, or undefined for the default/unknown/missing colour. */+function keepColorToHex(color: string | undefined): string | undefined {+ return color ? KEEP_COLOR_HEX[color.toUpperCase()] : undefined;+}+/** Converts a Keep microsecond-epoch timestamp to Trilium's UTC DB format, or undefined if absent/invalid. */function usecToUtc(usec: number | undefined): string | undefined {if (!usec) {