Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/anytype): reject Protobuf and Markdown exports with guidance

Elian Doran05b72ab0d6a7
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index 1dd294a46a..24f66bd8f7 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -328,7 +328,9 @@
"root-title": "Google Keep import"
},
"anytype_import": {
- "root-title": "Anytype import"
+ "root-title": "Anytype import",
+ "protobuf-export-unsupported": "This looks like an Anytype \"Protobuf\" export, which Trilium cannot import. Please re-export your Anytype space using the \"Any-Block\" format with \"JSON\" selected and import that instead.",
+ "markdown-export-unsupported": "This looks like an Anytype \"Markdown\" export, which Trilium cannot import. Please re-export your Anytype space using the \"Any-Block\" format with \"JSON\" selected and import that instead."
},
"pdf": {
"export_filter": "PDF Document (*.pdf)",
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.ts
index 2607b72640..0b17be4bd2 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
@@ -772,4 +772,28 @@ describe("Anytype importer — integration", () => {
expect(importRoot.title).toBe("Anytype import");
expect(importRoot.getChildNotes()).toHaveLength(0);
});
+
+ it("rejects an Anytype Protobuf export (binary objects/*.pb), guiding the user to re-export as JSON", async () => {
+ // The Protobuf variant ships binary `objects/*.pb` (not the `.pb.json` this importer reads).
+ await expect(
+ importAnytype({ "objects/bafyreiabc.pb": "binary protobuf bytes, not JSON" }, "My space.zip")
+ ).rejects.toThrow(/Protobuf.*re-export.*JSON/is);
+ });
+
+ it("rejects an Anytype Markdown export (*.md files), guiding the user to re-export as JSON", async () => {
+ // The Markdown variant ships one `*.md` per page, with no `objects/` JSON at all.
+ await expect(
+ importAnytype({ "article-1.md": "# Article 1\n\nbody", "article-2.md": "# Article 2" }, "My space.zip")
+ ).rejects.toThrow(/Markdown.*re-export.*JSON/is);
+ });
+
+ it("imports a valid JSON export even if it carries a stray markdown file (the guard only fires with zero pages)", async () => {
+ // A real page is present, so the wrong-format detection must not trip on an incidental `.md` entry.
+ const importRoot = await importAnytype({
+ "objects/p1.pb.json": pageObject("p1", "Real page", ["body"]),
+ "notes.md": "# stray markdown"
+ });
+
+ expect(importRoot.getChildNotes().map((n) => n.title)).toEqual(["Real page"]);
+ });
});
diff --git a/packages/trilium-core/src/services/import/anytype/importer.ts b/packages/trilium-core/src/services/import/anytype/importer.ts
index b71dc288d1..f7a2805deb 100644
--- a/packages/trilium-core/src/services/import/anytype/importer.ts
+++ b/packages/trilium-core/src/services/import/anytype/importer.ts
@@ -38,7 +38,7 @@ import { extractContent } from "./content.js";
import type { AnytypeDetails, AnytypeSnapshot, FileObjectInfo, LinkResolver, ParsedColumn, ParsedObject, RelationInfo, ResolvedLink } from "./model.js";
async function importAnytype(taskContext: TaskContext<"importNotes">, fileBuffer: Uint8Array, importRootNote: BNote, fileName?: string): Promise<BNote> {
- const { objects, relations, options, fileObjects, files } = await parseZip(fileBuffer);
+ const { objects, relations, options, fileObjects, files, protobufObjectCount, markdownFileCount } = await parseZip(fileBuffer);
const relationMap = buildRelationMap(relations);
const optionMap = buildOptionMap(options);
const fileObjectMap = buildFileObjectMap(fileObjects);
@@ -46,6 +46,19 @@ async function importAnytype(taskContext: TaskContext<"importNotes">, fileBuffer
// query sets and system objects stay out.
const pageSnapshots = objects.filter((snapshot) => isPage(snapshot) || isCollectionObject(snapshot));
+ // Anytype offers three export shapes; this importer only understands the JSON "Any-Block" one
+ // (`objects/*.pb.json`). The Protobuf variant ships binary `objects/*.pb`, the Markdown variant ships
+ // `*.md` files — neither yields a JSON object, so they'd otherwise import as an empty tree. When no JSON
+ // page was found but those files were, fail with guidance to re-export as JSON (mirrors the Notion importer).
+ if (pageSnapshots.length === 0) {
+ if (protobufObjectCount > 0) {
+ throw new Error(t("anytype_import.protobuf-export-unsupported"));
+ }
+ if (markdownFileCount > 0) {
+ throw new Error(t("anytype_import.markdown-export-unsupported"));
+ }
+ }
+
// A collection-scoped export omits the collection itself (Anytype doesn't export the wrapper), so its
// name and column schema are lost. Recover them: name the import root from the zip and make it a `table`
// collection whose columns are synthesized from the properties its member pages carry.
@@ -78,16 +91,20 @@ async function importAnytype(taskContext: TaskContext<"importNotes">, fileBuffer
* definitions), `relationsOptions/*.pb.json` (select / multi-select option values), `filesObjects/*.pb.json`
* (file metadata) and the raw bytes under `files/` (keyed by normalized path). The relation definitions name
* and type the custom properties; the options resolve a select value's option id to its name; the file
- * objects + bytes back the file properties. Other sibling folders (types, templates, …) and the protobuf
- * `.pb` files are ignored for now. A malformed entry is skipped rather than failing the import.
+ * objects + bytes back the file properties. Other sibling folders (types, templates, …) are ignored for now.
+ * A malformed entry is skipped rather than failing the import. The wrong-format `objects/*.pb` (Protobuf) and
+ * `*.md` (Markdown) entries are merely counted, so the caller can fail with guidance instead of importing an
+ * empty tree.
*/
-async function parseZip(fileBuffer: Uint8Array): Promise<{ objects: AnytypeSnapshot[]; relations: AnytypeSnapshot[]; options: AnytypeSnapshot[]; fileObjects: AnytypeSnapshot[]; files: Map<string, Uint8Array> }> {
+async function parseZip(fileBuffer: Uint8Array): Promise<{ objects: AnytypeSnapshot[]; relations: AnytypeSnapshot[]; options: AnytypeSnapshot[]; fileObjects: AnytypeSnapshot[]; files: Map<string, Uint8Array>; protobufObjectCount: number; markdownFileCount: number }> {
const provider = getZipProvider();
const objects: AnytypeSnapshot[] = [];
const relations: AnytypeSnapshot[] = [];
const options: AnytypeSnapshot[] = [];
const fileObjects: AnytypeSnapshot[] = [];
const files = new Map<string, Uint8Array>();
+ let protobufObjectCount = 0;
+ let markdownFileCount = 0;
const filenameEncoding = await provider.detectFilenameEncoding(fileBuffer);
await provider.readZipFile(fileBuffer, async (entry, readContent) => {
@@ -96,6 +113,15 @@ async function parseZip(fileBuffer: Uint8Array): Promise<{ objects: AnytypeSnaps
files.set(normalizePath(entry.fileName), await readContent());
return;
}
+ // Tally the other-export-format files (so importAnytype can reject them with guidance), then skip them.
+ if (isProtobufObjectEntry(entry.fileName)) {
+ protobufObjectCount++;
+ return;
+ }
+ if (isMarkdownEntry(entry.fileName)) {
+ markdownFileCount++;
+ return;
+ }
const bucket = isObjectEntry(entry.fileName) ? objects
: isRelationOptionEntry(entry.fileName) ? options
: isFileObjectEntry(entry.fileName) ? fileObjects
@@ -110,7 +136,7 @@ async function parseZip(fileBuffer: Uint8Array): Promise<{ objects: AnytypeSnaps
}
}, filenameEncoding);
- return { objects, relations, options, fileObjects, files };
+ return { objects, relations, options, fileObjects, files, protobufObjectCount, markdownFileCount };
}
/** True for entries that are JSON object files under the export's `objects/` folder. */
@@ -138,6 +164,17 @@ function isFileEntry(fileName: string): boolean {
return normalizePath(fileName).startsWith("files/");
}
+/** True for an Anytype *Protobuf* export's binary object (`objects/*.pb`) — the JSON export uses `.pb.json`. */
+function isProtobufObjectEntry(fileName: string): boolean {
+ const normalized = normalizePath(fileName);
+ return normalized.startsWith("objects/") && normalized.endsWith(".pb");
+}
+
+/** True for an Anytype *Markdown* export's page file (a top-level `*.md`). */
+function isMarkdownEntry(fileName: string): boolean {
+ return normalizePath(fileName).endsWith(".md");
+}
+
function isJsonEntryUnder(fileName: string, folder: string): boolean {
const normalized = normalizePath(fileName);
return normalized.startsWith(folder) && normalized.endsWith(".pb.json");