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/anytype): import basic pages that omit the default `layout`
Elian Doraned1f84c7d9f1
diff --git a/packages/trilium-core/src/services/import/anytype/importer.integration.spec.ts b/packages/trilium-core/src/services/import/anytype/importer.integration.spec.tsindex 59340758d8..ca46dc283e 100644--- a/packages/trilium-core/src/services/import/anytype/importer.integration.spec.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.integration.spec.ts@@ -93,6 +93,30 @@ describe("Anytype importer — integration", () => {expect(importRoot.getChildNotes().map((note) => note.title)).toEqual(["Real Page"]);});+ it("imports a single basic page whose export omits `layout` (only resolvedLayout is set)", async () => {+ // A single-object export of a basic page drops the default `layout` field entirely, with backslash+ // path separators on Windows — both must still resolve to one imported page with its text.+ const page = JSON.stringify({+ sbType: "Page",+ snapshot: {+ data: {+ blocks: [+ { id: "solo", childrenIds: ["header", "solo-b0"] },+ { id: "header", childrenIds: ["title"] },+ { id: "title", text: { text: "", style: "Title" } },+ { id: "solo-b0", text: { text: "Regular text", style: "Paragraph" } }+ ],+ details: { id: "solo", name: "Formatting test", resolvedLayout: 0 }+ }+ }+ });+ const importRoot = await importAnytype({ "objects\\solo.pb.json": page });++ const children = importRoot.getChildNotes();+ expect(children.map((note) => note.title)).toEqual(["Formatting test"]);+ expect(decodeUtf8(children[0]?.getContent() ?? "")).toBe("<p>Regular text</p>");+ });+it("produces an empty root when the export has no pages", async () => {const importRoot = await importAnytype({"types/type1.pb.json": JSON.stringify({ sbType: "STType", snapshot: { data: { details: { id: "type1", name: "Article" } } } })diff --git a/packages/trilium-core/src/services/import/anytype/importer.spec.ts b/packages/trilium-core/src/services/import/anytype/importer.spec.tsindex c2e2f15c65..88d11d7717 100644--- a/packages/trilium-core/src/services/import/anytype/importer.spec.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.spec.ts@@ -4,7 +4,7 @@ import { isPage, parseObject } from "./importer.js";import type { AnytypeBlock, AnytypeSnapshot } from "./model.js";/** Wraps blocks + details into the export's snapshot shape. */-function snapshot(blocks: AnytypeBlock[], details: { id?: string; name?: string; layout?: number }, sbType = "Page"): AnytypeSnapshot {+function snapshot(blocks: AnytypeBlock[], details: { id?: string; name?: string; layout?: number; resolvedLayout?: number }, sbType = "Page"): AnytypeSnapshot {return { sbType, snapshot: { data: { blocks, details } } };}@@ -35,6 +35,15 @@ describe("isPage", () => {expect(isPage(snapshot([], { id: "p", name: "Someone", layout: 19 }, "Participant"))).toBe(false);expect(isPage(snapshot([], { id: "w", layout: 10 }, "Workspace"))).toBe(false);});++ it("accepts a basic page that omits `layout` (single-object exports), falling back to resolvedLayout", () => {+ // Anytype drops `layout` when it's the default, carrying the value in resolvedLayout instead.+ expect(isPage(snapshot([], { id: "p", name: "Solo page", resolvedLayout: 0 }))).toBe(true);+ // With neither field present, a Page still defaults to basic.+ expect(isPage(snapshot([], { id: "p", name: "Bare page" }))).toBe(true);+ // resolvedLayout still excludes a set whose `layout` is likewise omitted.+ expect(isPage(snapshot([], { id: "s", name: "Solo set", resolvedLayout: 3 }))).toBe(false);+ });});describe("parseObject", () => {diff --git a/packages/trilium-core/src/services/import/anytype/importer.ts b/packages/trilium-core/src/services/import/anytype/importer.tsindex 3d42b4fe60..d3a5ce7cbe 100644--- a/packages/trilium-core/src/services/import/anytype/importer.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.ts@@ -64,9 +64,18 @@ function isObjectEntry(fileName: string): boolean {* Whether a snapshot is a page we should import. A page is a `Page` smartblock with the basic layout (0);* this excludes sets/collections (layout 3) and system objects like the participant, workspace and* dashboard widget. Conservative on purpose — other content layouts can be admitted as the importer grows.+ *+ * Anytype omits `layout` when it's the default (Basic = 0) — a single-object export of a basic page has no+ * `layout` field at all — so we fall back to `resolvedLayout` (always present) and treat a wholly missing+ * value as basic. Sets and other non-page layouts are still excluded by their non-zero value.*/export function isPage(snapshot: AnytypeSnapshot): boolean {- return snapshot.sbType === "Page" && snapshot.snapshot?.data?.details?.layout === 0;+ if (snapshot.sbType !== "Page") {+ return false;+ }+ const details = snapshot.snapshot?.data?.details;+ const layout = details?.layout ?? details?.resolvedLayout ?? 0;+ return layout === 0;}/** Reduces a page snapshot to the title and plain-text body needed to create a note. */diff --git a/packages/trilium-core/src/services/import/anytype/model.ts b/packages/trilium-core/src/services/import/anytype/model.tsindex 372fea97c3..587348b8bb 100644--- a/packages/trilium-core/src/services/import/anytype/model.ts+++ b/packages/trilium-core/src/services/import/anytype/model.ts@@ -23,11 +23,14 @@ export interface AnytypeBlock {}/** The object's `snapshot.data.details` — its metadata map. `layout` distinguishes a basic page (0) from- * a set/collection (3) and other system layouts; `name` is the title; `id` matches the root block's id. */+ * a set/collection (3) and other system layouts; `name` is the title; `id` matches the root block's id.+ * `layout` is omitted when it's the default (0), so `resolvedLayout` (always present) is the reliable+ * source of the effective layout. */export interface AnytypeDetails {id?: string;name?: string;layout?: number;+ resolvedLayout?: number;}/** One exported object file: `sbType` is the smartblock kind ("Page", "Participant", "Workspace", …) and