Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/anytype): map heading styles to Trilium headings

Elian Doran793f878e28f0
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 ca46dc283e..7be8e00a11 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
@@ -117,6 +117,30 @@ describe("Anytype importer — integration", () => {
expect(decodeUtf8(children[0]?.getContent() ?? "")).toBe("<p>Regular text</p>");
});
+ it("maps heading styles to h2/h3/h4 end-to-end (modeled on the 'Formatting test' page)", async () => {
+ const page = JSON.stringify({
+ sbType: "Page",
+ snapshot: {
+ data: {
+ blocks: [
+ { id: "fmt", childrenIds: ["header", "fmt-1", "fmt-2", "fmt-3", "fmt-4"] },
+ { id: "header", childrenIds: ["title"] },
+ { id: "title", text: { text: "", style: "Title" } },
+ { id: "fmt-1", text: { text: "Regular text", style: "Paragraph" } },
+ { id: "fmt-2", text: { text: "Title", style: "Header1" } },
+ { id: "fmt-3", text: { text: "Heading", style: "Header2" } },
+ { id: "fmt-4", text: { text: "Subheading", style: "Header3" } }
+ ],
+ details: { id: "fmt", name: "Formatting test", resolvedLayout: 0 }
+ }
+ }
+ });
+ const importRoot = await importAnytype({ "objects/fmt.pb.json": page });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "Formatting test");
+ expect(decodeUtf8(note?.getContent() ?? "")).toBe("<p>Regular text</p><h2>Title</h2><h3>Heading</h3><h4>Subheading</h4>");
+ });
+
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.ts
index 88d11d7717..1c80c9de9a 100644
--- a/packages/trilium-core/src/services/import/anytype/importer.spec.ts
+++ b/packages/trilium-core/src/services/import/anytype/importer.spec.ts
@@ -47,14 +47,27 @@ describe("isPage", () => {
});
describe("parseObject", () => {
- it("takes the title from details.name and emits each text block as a paragraph, ignoring style", () => {
- const result = parseObject(page("My Page", [textBlock("b1", "First"), textBlock("b2", "Second", "Header1")]));
+ it("takes the title from details.name and emits non-heading text blocks as paragraphs", () => {
+ // Numbered/Marked/Quote/Code etc. are all flattened to <p> for now (only headings get their own tag).
+ const result = parseObject(page("My Page", [textBlock("b1", "First"), textBlock("b2", "Second", "Numbered")]));
expect(result.id).toBe("obj");
expect(result.title).toBe("My Page");
- // Header1 still becomes a plain <p> — no formatting in this version.
expect(result.content).toBe("<p>First</p><p>Second</p>");
});
+ it("maps Anytype's three heading styles to Trilium's top heading levels (h2/h3/h4)", () => {
+ // Labels and order taken from the "Formatting test" page: Header1/2/3 are Title/Heading/Subheading.
+ const result = parseObject(
+ page("Headings", [
+ textBlock("b1", "Regular text", "Paragraph"),
+ textBlock("b2", "Title", "Header1"),
+ textBlock("b3", "Heading", "Header2"),
+ textBlock("b4", "Subheading", "Header3")
+ ])
+ );
+ expect(result.content).toBe("<p>Regular text</p><h2>Title</h2><h3>Heading</h3><h4>Subheading</h4>");
+ });
+
it("walks nested blocks in document order (parent text before its children)", () => {
const result = parseObject(page("Nested", [textBlock("b1", "Parent", "Paragraph", ["c1"]), textBlock("c1", "Child")]));
expect(result.content).toBe("<p>Parent</p><p>Child</p>");
diff --git a/packages/trilium-core/src/services/import/anytype/importer.ts b/packages/trilium-core/src/services/import/anytype/importer.ts
index d3a5ce7cbe..be740dc728 100644
--- a/packages/trilium-core/src/services/import/anytype/importer.ts
+++ b/packages/trilium-core/src/services/import/anytype/importer.ts
@@ -90,10 +90,11 @@ export function parseObject(snapshot: AnytypeSnapshot): ParsedObject {
}
/**
- * Walks the block tree from the root in document order, emitting each non-empty text block as a `<p>`. The
- * `header` subtree (title/description/featuredRelations chrome) is skipped, as are the structural Title and
- * Description styles wherever they appear. Formatting (marks), links and other block kinds are ignored for
- * now — only the text is pulled out.
+ * Walks the block tree from the root in document order, wrapping each non-empty text block in a tag chosen
+ * by its style (headings → `<h2>`/`<h3>`/`<h4>`, everything else → `<p>`). The `header` subtree
+ * (title/description/featuredRelations chrome) is skipped, as are the structural Title and Description
+ * styles wherever they appear. Formatting (marks), links and other block kinds are ignored for now — only
+ * the text is pulled out.
*/
function extractTextContent(blocks: AnytypeBlock[], rootId: string): string {
const byId = new Map<string, AnytypeBlock>();
@@ -106,7 +107,7 @@ function extractTextContent(blocks: AnytypeBlock[], rootId: string): string {
return "";
}
- const paragraphs: string[] = [];
+ const parts: string[] = [];
const visited = new Set<string>();
const walk = (id: string) => {
@@ -124,7 +125,8 @@ function extractTextContent(blocks: AnytypeBlock[], rootId: string): string {
const text = block.text?.text?.trim();
const style = block.text?.style;
if (text && style !== "Title" && style !== "Description") {
- paragraphs.push(`<p>${escapeHtml(text)}</p>`);
+ const tag = tagForStyle(style);
+ parts.push(`<${tag}>${escapeHtml(text)}</${tag}>`);
}
for (const childId of block.childrenIds ?? []) {
@@ -137,7 +139,26 @@ function extractTextContent(blocks: AnytypeBlock[], rootId: string): string {
walk(childId);
}
- return paragraphs.join("");
+ return parts.join("");
+}
+
+/**
+ * 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 style (paragraphs, lists,
+ * quotes, code, …) is flattened to a paragraph for now.
+ */
+function tagForStyle(style: string | undefined): string {
+ switch (style) {
+ case "Header1":
+ return "h2";
+ case "Header2":
+ return "h3";
+ case "Header3":
+ return "h4";
+ default:
+ return "p";
+ }
}
function escapeHtml(text: string): string {