Skip to content

Build your personal knowledge base with Trilium Notes

feat(import/anytype): convert bullet/numbered/task lists

Elian Doran1959aeeb6f18
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 392b83926c..321aa5e535 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
@@ -240,6 +240,29 @@ describe("Anytype importer — integration", () => {
);
});
+ it("imports nested lists end-to-end (grouping consecutive items, nesting children)", async () => {
+ const page = JSON.stringify({
+ sbType: "Page",
+ snapshot: {
+ data: {
+ blocks: [
+ { id: "list", childrenIds: ["header", "n1", "n2"] },
+ { id: "header", childrenIds: ["title"] },
+ { id: "title", text: { text: "", style: "Title" } },
+ { id: "n1", text: { text: "One", style: "Numbered" }, childrenIds: ["n1a"] },
+ { id: "n1a", text: { text: "One-A", style: "Numbered" }, childrenIds: [] },
+ { id: "n2", text: { text: "Two", style: "Numbered" }, childrenIds: [] }
+ ],
+ details: { id: "list", name: "Lists", resolvedLayout: 0 }
+ }
+ }
+ });
+ const importRoot = await importAnytype({ "objects/list.pb.json": page });
+
+ const note = importRoot.getChildNotes().find((n) => n.title === "Lists");
+ expect(decodeUtf8(note?.getContent() ?? "")).toBe("<ol><li>One<ol><li>One-A</li></ol></li><li>Two</li></ol>");
+ });
+
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 6fc0faf517..7a66275335 100644
--- a/packages/trilium-core/src/services/import/anytype/importer.spec.ts
+++ b/packages/trilium-core/src/services/import/anytype/importer.spec.ts
@@ -31,6 +31,16 @@ function mark(from: number, to: number, type: string, param?: string): AnytypeMa
return { range: { from, to }, type, param };
}
+/** A list item (Marked/Numbered/Checkbox) with optional nested children, checked state and marks. */
+function listItem(id: string, style: string, text: string, childrenIds: string[] = [], checked = false, marks: AnytypeMark[] = []): AnytypeBlock {
+ return { id, text: { text, style, marks: { marks }, checked }, childrenIds };
+}
+
+/** A page whose root's direct children are `rootChildIds`; `blocks` supplies those plus any nested blocks. */
+function listDoc(rootChildIds: string[], blocks: AnytypeBlock[]): AnytypeSnapshot {
+ return snapshot([{ id: "obj", childrenIds: rootChildIds }, ...blocks], { id: "obj", name: "List" });
+}
+
describe("isPage", () => {
it("accepts a basic-layout Page and rejects sets and system objects", () => {
expect(isPage(page("A page", []))).toBe(true);
@@ -52,9 +62,9 @@ describe("isPage", () => {
});
describe("parseObject", () => {
- it("takes the title from details.name and emits non-heading text blocks as paragraphs", () => {
- // Numbered/Marked/Quote 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")]));
+ it("takes the title from details.name and flattens non-heading, non-list styles to paragraphs", () => {
+ // Quote/Callout/Toggle etc. are all flattened to <p> for now (headings and lists are handled).
+ const result = parseObject(page("My Page", [textBlock("b1", "First"), textBlock("b2", "Second", "Quote")]));
expect(result.id).toBe("obj");
expect(result.title).toBe("My Page");
expect(result.content).toBe("<p>First</p><p>Second</p>");
@@ -106,6 +116,54 @@ describe("parseObject", () => {
});
});
+describe("lists", () => {
+ it("groups consecutive Marked items into one <ul>", () => {
+ const doc = listDoc(["m1", "m2"], [listItem("m1", "Marked", "One"), listItem("m2", "Marked", "Two")]);
+ expect(parseObject(doc).content).toBe("<ul><li>One</li><li>Two</li></ul>");
+ });
+
+ it("renders Numbered items as an <ol>", () => {
+ const doc = listDoc(["n1", "n2"], [listItem("n1", "Numbered", "One"), listItem("n2", "Numbered", "Two")]);
+ expect(parseObject(doc).content).toBe("<ol><li>One</li><li>Two</li></ol>");
+ });
+
+ it("renders Checkbox items as a CKEditor todo-list carrying the checked state", () => {
+ const doc = listDoc(["c1", "c2"], [listItem("c1", "Checkbox", "Todo", [], false), listItem("c2", "Checkbox", "Done", [], true)]);
+ expect(parseObject(doc).content).toBe(
+ '<ul class="todo-list">' +
+ '<li><label class="todo-list__label"><input type="checkbox"disabled="disabled"><span class="todo-list__label__description">Todo</span></label></li>' +
+ '<li><label class="todo-list__label"><input type="checkbox"checked="checked" disabled="disabled"><span class="todo-list__label__description">Done</span></label></li>' +
+ "</ul>"
+ );
+ });
+
+ it("nests child list items inside the parent <li>", () => {
+ const doc = listDoc(
+ ["m1", "m2"],
+ [listItem("m1", "Marked", "One", ["m1a"]), listItem("m1a", "Marked", "One-A"), listItem("m2", "Marked", "Two")]
+ );
+ expect(parseObject(doc).content).toBe("<ul><li>One<ul><li>One-A</li></ul></li><li>Two</li></ul>");
+ });
+
+ it("starts a new list when the type changes or a non-list block interrupts", () => {
+ const doc = listDoc(
+ ["m1", "n1", "p1", "m2"],
+ [
+ listItem("m1", "Marked", "Bullet"),
+ listItem("n1", "Numbered", "Number"),
+ { id: "p1", text: { text: "Para", style: "Paragraph", marks: { marks: [] } } },
+ listItem("m2", "Marked", "Again")
+ ]
+ );
+ expect(parseObject(doc).content).toBe("<ul><li>Bullet</li></ul><ol><li>Number</li></ol><p>Para</p><ul><li>Again</li></ul>");
+ });
+
+ it("applies inline marks inside list items", () => {
+ const doc = listDoc(["m1"], [listItem("m1", "Marked", "Bold item", [], false, [mark(0, 4, "Bold")])]);
+ expect(parseObject(doc).content).toBe("<ul><li><strong>Bold</strong> item</li></ul>");
+ });
+});
+
describe("renderInlineText", () => {
it("returns escaped plain text when there are no marks", () => {
expect(renderInlineText("a < b & c > d", [])).toBe("a &lt; b &amp; c &gt; d");
diff --git a/packages/trilium-core/src/services/import/anytype/importer.ts b/packages/trilium-core/src/services/import/anytype/importer.ts
index cf9a16fdbd..dba82efb66 100644
--- a/packages/trilium-core/src/services/import/anytype/importer.ts
+++ b/packages/trilium-core/src/services/import/anytype/importer.ts
@@ -4,10 +4,10 @@
* Anytype exports each object as a single `objects/<cid>.pb.json` file (the protobuf export uses `.pb`
* 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) and
- * code blocks (with the language preserved as the Trilium MIME). Lists, 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).
+ * 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).
*
* 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 —
@@ -92,10 +92,11 @@ export function parseObject(snapshot: AnytypeSnapshot): ParsedObject {
}
/**
- * Walks the block tree from the root in document order, converting each non-empty text block to HTML: a
- * code block becomes `<pre><code>`, a heading becomes `<h2>`/`<h3>`/`<h4>`, and everything else a `<p>`,
- * with inline marks applied. The `header` subtree (title/description/featuredRelations chrome) is skipped,
- * as are the structural Title and Description styles wherever they appear.
+ * Converts the page's block tree to HTML. A sequence of sibling blocks is rendered in document order, with
+ * consecutive list items of the same kind grouped into a single `<ul>`/`<ol>`/todo-list and a list item's
+ * children nested inside its `<li>`. Other blocks become a code block, a heading (`<h2>`/`<h3>`/`<h4>`) or
+ * a `<p>`, with inline marks applied. The `header` subtree (title/description/featuredRelations chrome) is
+ * skipped, as are the structural Title and Description styles wherever they appear.
*/
function extractContent(blocks: AnytypeBlock[], rootId: string): string {
const byId = new Map<string, AnytypeBlock>();
@@ -108,52 +109,104 @@ function extractContent(blocks: AnytypeBlock[], rootId: string): string {
return "";
}
- const parts: string[] = [];
const visited = new Set<string>();
- const walk = (id: string) => {
- // The header holds title/description chrome, not body content; cycle-guard everything else.
- if (id === "header" || visited.has(id)) {
- return;
+ // Renders a run of sibling ids, grouping consecutive same-kind list items into one list. The header
+ // chrome and already-visited blocks (a node reachable twice) are skipped.
+ function renderSequence(ids: string[]): string {
+ const parts: string[] = [];
+ let i = 0;
+ while (i < ids.length) {
+ const id = ids[i];
+ const block = id === "header" || visited.has(id) ? undefined : byId.get(id);
+ if (!block) {
+ i++;
+ continue;
+ }
+
+ const kind = listKind(block);
+ if (kind) {
+ const run: AnytypeBlock[] = [];
+ while (i < ids.length) {
+ const candidate = visited.has(ids[i]) ? undefined : byId.get(ids[i]);
+ if (!candidate || listKind(candidate) !== kind) {
+ break;
+ }
+ visited.add(ids[i]);
+ run.push(candidate);
+ i++;
+ }
+ parts.push(renderList(kind, run));
+ } else {
+ visited.add(id);
+ parts.push(renderLeaf(block));
+ i++;
+ }
}
- visited.add(id);
+ return parts.join("");
+ }
- const block = byId.get(id);
- if (!block) {
- return;
+ function renderList(kind: ListKind, items: AnytypeBlock[]): string {
+ const body = items.map((item) => renderItem(kind, item)).join("");
+ if (kind === "task") {
+ return `<ul class="todo-list">${body}</ul>`;
}
+ return kind === "ol" ? `<ol>${body}</ol>` : `<ul>${body}</ul>`;
+ }
+ function renderItem(kind: ListKind, item: AnytypeBlock): string {
+ const text = renderInlineText(item.text?.text ?? "", item.text?.marks?.marks ?? []);
+ const nested = renderSequence(item.childrenIds ?? []);
+ if (kind === "task") {
+ // CKEditor's read-only todo-list markup (matches the markdown importer's checkbox output).
+ const checkbox = `<input type="checkbox"${item.text?.checked ? 'checked="checked" ' : ""}disabled="disabled">`;
+ return `<li><label class="todo-list__label">${checkbox}<span class="todo-list__label__description">${text}</span></label>${nested}</li>`;
+ }
+ return `<li>${text}${nested}</li>`;
+ }
+
+ function renderLeaf(block: AnytypeBlock): 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;
+ let html = "";
if (rawText.trim() && style !== "Title" && style !== "Description") {
if (style === "Code") {
// A code block is literal: no inline marks, and its language is preserved as the MIME.
- parts.push(renderCodeBlock(rawText, block.fields?.lang));
+ html = renderCodeBlock(rawText, block.fields?.lang);
} else {
const tag = tagForStyle(style);
- parts.push(`<${tag}>${renderInlineText(rawText, block.text?.marks?.marks ?? [])}</${tag}>`);
+ html = `<${tag}>${renderInlineText(rawText, block.text?.marks?.marks ?? [])}</${tag}>`;
}
}
-
- for (const childId of block.childrenIds ?? []) {
- walk(childId);
- }
- };
+ // A non-list block's children (rare — e.g. a toggle's contents) follow it as flattened siblings.
+ return html + renderSequence(block.childrenIds ?? []);
+ }
// The root block is the page container and carries no text of its own — start from its children.
- for (const childId of root.childrenIds ?? []) {
- walk(childId);
- }
+ return renderSequence(root.childrenIds ?? []);
+}
- return parts.join("");
+/** The kind of list a block belongs to (bullet/ordered/task), or null when it isn't a list item. */
+type ListKind = "ul" | "ol" | "task";
+function listKind(block: AnytypeBlock): ListKind | null {
+ switch (block.text?.style) {
+ case "Marked":
+ return "ul";
+ case "Numbered":
+ return "ol";
+ case "Checkbox":
+ return "task";
+ default:
+ return 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 style (paragraphs, lists,
- * quotes, …) is flattened to a paragraph for now.
+ * `<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.
*/
function tagForStyle(style: string | undefined): string {
switch (style) {
@@ -367,14 +420,16 @@ export interface AnytypeMark {
param?: string;
}
-/** The text payload of a text block: its `text`, `style` (Paragraph, Header1, Code, …) and a `marks.marks`
- * list of inline formatting spans over `text`. */
+/** 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`. */
export interface AnytypeText {
text?: string;
style?: string;
marks?: {
marks?: AnytypeMark[];
};
+ /** Whether a `Checkbox`-style list item is ticked. */
+ checked?: boolean;
}
/** A single block in an object's `snapshot.data.blocks`. Non-text blocks (links, dividers, dataviews, …)