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/anytype): convert toggles
Elian Doranc9a7340948e4
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 321aa5e535..cb5309b3a3 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@@ -263,6 +263,28 @@ describe("Anytype importer — integration", () => {expect(decodeUtf8(note?.getContent() ?? "")).toBe("<ol><li>One<ol><li>One-A</li></ol></li><li>Two</li></ol>");});+ it("imports a toggle as a collapsible block end-to-end", async () => {+ const page = JSON.stringify({+ sbType: "Page",+ snapshot: {+ data: {+ blocks: [+ { id: "tog", childrenIds: ["header", "t1"] },+ { id: "header", childrenIds: ["title"] },+ { id: "title", text: { text: "", style: "Title" } },+ { id: "t1", text: { text: "Toggle", style: "Toggle" }, childrenIds: ["t1a"] },+ { id: "t1a", text: { text: "Inside", style: "Paragraph" }, childrenIds: [] }+ ],+ details: { id: "tog", name: "Toggles", resolvedLayout: 0 }+ }+ }+ });+ const importRoot = await importAnytype({ "objects/tog.pb.json": page });++ const note = importRoot.getChildNotes().find((n) => n.title === "Toggles");+ expect(decodeUtf8(note?.getContent() ?? "")).toBe('<details class="trilium-collapsible"><summary>Toggle</summary><p>Inside</p></details>');+ });+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 7a66275335..64d8c745ef 100644--- a/packages/trilium-core/src/services/import/anytype/importer.spec.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.spec.ts@@ -164,6 +164,35 @@ describe("lists", () => {});});+describe("toggles", () => {+ it("converts a Toggle to a collapsible block, nesting its children as the collapsed body", () => {+ const doc = listDoc(+ ["t1", "p1"],+ [+ { id: "t1", text: { text: "Summary", style: "Toggle", marks: { marks: [mark(0, 7, "Bold")] } }, childrenIds: ["t1a"] },+ { id: "t1a", text: { text: "Hidden", style: "Paragraph", marks: { marks: [] } } },+ { id: "p1", text: { text: "After", style: "Paragraph", marks: { marks: [] } } }+ ]+ );+ expect(parseObject(doc).content).toBe(+ '<details class="trilium-collapsible"><summary><strong>Summary</strong></summary><p>Hidden</p></details><p>After</p>'+ );+ });++ it("converts toggle headings to normal headings (h2/h3/h4), keeping their content", () => {+ const doc = listDoc(+ ["th1", "th2", "th3"],+ [+ { id: "th1", text: { text: "T1", style: "ToggleHeader1", marks: { marks: [] } }, childrenIds: ["th1a"] },+ { id: "th1a", text: { text: "Body", style: "Paragraph", marks: { marks: [] } } },+ { id: "th2", text: { text: "T2", style: "ToggleHeader2", marks: { marks: [] } } },+ { id: "th3", text: { text: "T3", style: "ToggleHeader3", marks: { marks: [] } } }+ ]+ );+ expect(parseObject(doc).content).toBe("<h2>T1</h2><p>Body</p><h3>T2</h3><h4>T3</h4>");+ });+});+describe("renderInlineText", () => {it("returns escaped plain text when there are no marks", () => {expect(renderInlineText("a < b & c > d", [])).toBe("a < b & c > d");diff --git a/packages/trilium-core/src/services/import/anytype/importer.ts b/packages/trilium-core/src/services/import/anytype/importer.tsindex dba82efb66..a299bec348 100644--- a/packages/trilium-core/src/services/import/anytype/importer.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.ts@@ -5,9 +5,10 @@* instead — not handled here), alongside sibling `relations/`, `types/`, `templates/` and `relationsOptions/`* folders. This importer reads the *pages* (basic note-like objects) and converts each text block to HTML:* headings, inline marks (bold/italic/strikethrough/underline/inline-code and text/background colours),- * code blocks (with the language preserved as the Trilium MIME) and bullet/numbered/task lists (grouped- * and nested). Links, relations, types and collections are still deferred, and every page lands as a flat- * child of a fresh "Anytype import" root (no hierarchy yet).+ * code blocks (with the language preserved as the Trilium MIME), bullet/numbered/task lists (grouped and+ * nested) and toggles (normal toggles → collapsible blocks; toggle headings → plain headings). Links,+ * relations, types and collections are still deferred, and every page lands as a flat child of a fresh+ * "Anytype import" root (no hierarchy yet).** Invoked from the shared file-import dispatcher (routes/api/import.ts) when the upload is tagged* `format=anytype`, so progress, completion and failure are reported by that dispatcher's TaskContext —@@ -169,6 +170,14 @@ function extractContent(blocks: AnytypeBlock[], rootId: string): string {// Use the raw text (not trimmed) so mark offsets stay aligned; only the emptiness test trims.const rawText = block.text?.text ?? "";const style = block.text?.style;+ const marks = block.text?.marks?.marks ?? [];++ if (style === "Toggle") {+ // A normal toggle becomes a Trilium collapsible block: its label is the summary, its children+ // the collapsed body. (Toggle *headings* fall through to a normal heading below, via tagForStyle.)+ return `<details class="trilium-collapsible"><summary>${renderInlineText(rawText, marks)}</summary>${renderSequence(block.childrenIds ?? [])}</details>`;+ }+let html = "";if (rawText.trim() && style !== "Title" && style !== "Description") {if (style === "Code") {@@ -176,10 +185,10 @@ function extractContent(blocks: AnytypeBlock[], rootId: string): string {html = renderCodeBlock(rawText, block.fields?.lang);} else {const tag = tagForStyle(style);- html = `<${tag}>${renderInlineText(rawText, block.text?.marks?.marks ?? [])}</${tag}>`;+ html = `<${tag}>${renderInlineText(rawText, marks)}</${tag}>`;}}- // A non-list block's children (rare — e.g. a toggle's contents) follow it as flattened siblings.+ // A non-list block's children (e.g. a toggle heading's collapsed content) follow as flattened siblings.return html + renderSequence(block.childrenIds ?? []);}@@ -205,16 +214,20 @@ function listKind(block: AnytypeBlock): ListKind | null {/*** Maps an Anytype text-block style to the Trilium tag it becomes. Anytype's three in-body heading levels* (UI-labelled Title / Heading / Subheading) map to Trilium's top three heading levels: Trilium reserves- * `<h1>` for the note title, so its body headings start at `<h2>`. Every other non-list style (paragraphs,- * quotes, callouts, toggles, …) is flattened to a paragraph for now.+ * `<h1>` for the note title, so its body headings start at `<h2>`. Toggle headings collapse to the same+ * plain headings (their toggle nature is dropped). Every other non-list style (paragraphs, quotes,+ * callouts, …) is flattened to a paragraph for now.*/function tagForStyle(style: string | undefined): string {switch (style) {case "Header1":+ case "ToggleHeader1":return "h2";case "Header2":+ case "ToggleHeader2":return "h3";case "Header3":+ case "ToggleHeader3":return "h4";default:return "p";