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 callouts to admonitions
Elian Doran0287959faab8
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 04d29ebacd..80a7ea762d 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@@ -307,6 +307,31 @@ describe("Anytype importer β integration", () => {expect(decodeUtf8(note?.getContent() ?? "")).toBe("<p>Above</p><hr>");});+ it("imports callouts as admonitions end-to-end, with Notion-style emoji handling", async () => {+ const page = JSON.stringify({+ sbType: "Page",+ snapshot: {+ data: {+ blocks: [+ { id: "co", childrenIds: ["header", "c1", "c2"] },+ { id: "header", childrenIds: ["title"] },+ { id: "title", text: { text: "", style: "Title" } },+ { id: "c1", text: { text: "Callout with default icon", style: "Callout", iconEmoji: "" } },+ { id: "c2", text: { text: "Callout with custom emoji", style: "Callout", iconEmoji: "πΆβπ«οΈ" } }+ ],+ details: { id: "co", name: "Callouts", resolvedLayout: 0 }+ }+ }+ });+ const importRoot = await importAnytype({ "objects/co.pb.json": page });++ const note = importRoot.getChildNotes().find((n) => n.title === "Callouts");+ expect(decodeUtf8(note?.getContent() ?? "")).toBe(+ '<aside class="admonition tip"><p>Callout with default icon</p></aside>' ++ '<aside class="admonition note"><p>πΆβπ«οΈ Callout with custom emoji</p></aside>'+ );+ });+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 3c4301aa31..bfde3bb028 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,29 @@ describe("lists", () => {});});+describe("callouts", () => {+ it("converts a default-icon callout to a tip admonition (icon implied, dropped)", () => {+ const doc = listDoc(["c1"], [{ id: "c1", text: { text: "Heads up", style: "Callout", marks: { marks: [] }, iconEmoji: "" } }]);+ expect(parseObject(doc).content).toBe('<aside class="admonition tip"><p>Heads up</p></aside>');+ });++ it("converts a custom-emoji callout to a note admonition, keeping the emoji at the start", () => {+ const doc = listDoc(["c1"], [{ id: "c1", text: { text: "Foggy", style: "Callout", marks: { marks: [] }, iconEmoji: "πΆβπ«οΈ" } }]);+ expect(parseObject(doc).content).toBe('<aside class="admonition note"><p>πΆβπ«οΈ Foggy</p></aside>');+ });++ it("applies inline marks in the callout body and renders child blocks inside the aside", () => {+ const doc = listDoc(+ ["c1"],+ [+ { id: "c1", text: { text: "Bold note", style: "Callout", marks: { marks: [mark(0, 4, "Bold")] }, iconEmoji: "" }, childrenIds: ["c1a"] },+ { id: "c1a", text: { text: "More", style: "Paragraph", marks: { marks: [] } } }+ ]+ );+ expect(parseObject(doc).content).toBe('<aside class="admonition tip"><p><strong>Bold</strong> note</p><p>More</p></aside>');+ });+});+describe("dividers", () => {it("converts Line and Dots divider blocks to <hr>", () => {const doc = listDoc(diff --git a/packages/trilium-core/src/services/import/anytype/importer.ts b/packages/trilium-core/src/services/import/anytype/importer.tsindex 5c3678567a..330934fb1d 100644--- a/packages/trilium-core/src/services/import/anytype/importer.ts+++ b/packages/trilium-core/src/services/import/anytype/importer.ts@@ -6,9 +6,9 @@* 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), bullet/numbered/task lists (grouped and- * nested), toggles (normal toggles β collapsible blocks; toggle headings β plain headings) and dividers- * (β `<hr>`). 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).+ * nested), toggles (normal toggles β collapsible blocks; toggle headings β plain headings), callouts+ * (β admonitions) and dividers (β `<hr>`). 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 β@@ -183,6 +183,17 @@ function extractContent(blocks: AnytypeBlock[], rootId: string): string {return `<details class="trilium-collapsible"><summary>${renderInlineText(rawText, marks)}</summary>${renderSequence(block.childrenIds ?? [])}</details>`;}+ if (style === "Callout") {+ // A callout becomes a Trilium admonition. Following the Notion importer: the default icon (no+ // custom emoji) maps to a "tip" with the icon dropped; any custom emoji maps to a "note" with+ // the emoji kept at the start of the body (admonitions have no per-block icon).+ const emoji = block.text?.iconEmoji ?? "";+ const type = emoji ? "note" : "tip";+ const lead = [emoji, renderInlineText(rawText, marks)].filter(Boolean).join(" ");+ const firstPara = lead ? `<p>${lead}</p>` : "";+ return `<aside class="admonition ${type}">${firstPara}${renderSequence(block.childrenIds ?? [])}</aside>`;+ }+let html = "";if (rawText.trim() && style !== "Title" && style !== "Description") {if (style === "Code") {@@ -438,8 +449,8 @@ export interface AnytypeMark {param?: string;}-/** The text payload of a text block: its `text`, `style` (Paragraph, Header1, Marked, Checkbox, Code, β¦),- * a `marks.marks` list of inline formatting spans over `text`, and (for `Checkbox` items) `checked`. */+/** The text payload of a text block: its `text`, `style` (Paragraph, Header1, Marked, Checkbox, Callout, β¦),+ * a `marks.marks` list of inline formatting spans over `text`, and per-style extras (`checked`, `iconEmoji`). */export interface AnytypeText {text?: string;style?: string;@@ -448,6 +459,8 @@ export interface AnytypeText {};/** Whether a `Checkbox`-style list item is ticked. */checked?: boolean;+ /** A `Callout`'s icon; empty for the default icon, otherwise a custom emoji. */+ iconEmoji?: string;}/** A single block in an object's `snapshot.data.blocks`. Non-text blocks (links, dividers, dataviews, β¦)