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
fix(export): strip CKEditor data-list-item-id from HTML and Markdown exports
Elian Dorana5b59bf24456
diff --git a/apps/edit-docs/src/edit-docs.ts b/apps/edit-docs/src/edit-docs.tsindex 2ab6203c96..9e024228dd 100644--- a/apps/edit-docs/src/edit-docs.ts+++ b/apps/edit-docs/src/edit-docs.ts@@ -185,9 +185,6 @@ async function exportData(noteId: string, format: ExportFormat, outputPath: stricontent = rewriteHelpLinks(content);- // Remove data-list-item-id created by CKEditor for lists- content = content.replace(/ data-list-item-id="[^"]*"/g, "");-return content;function findAttachment(targetAttachmentId: string) {diff --git a/packages/trilium-core/src/services/export/single.spec.ts b/packages/trilium-core/src/services/export/single.spec.tsindex 2d813e197f..7703c2631c 100644--- a/packages/trilium-core/src/services/export/single.spec.ts+++ b/packages/trilium-core/src/services/export/single.spec.ts@@ -15,6 +15,21 @@ describe("Note type mappings", () => {});});+ it("strips CKEditor's data-list-item-id from single-note HTML and Markdown exports", () => {+ // The bullet converts to Markdown syntax; the list inside the table survives as raw HTML —+ // neither should carry the editor-only id in either format.+ const note = buildNote({ type: "text", title: "Listy" });+ const content = `<ul><li data-list-item-id="e0123">Bullet</li></ul>`+ + `<table><tbody><tr><td><ul><li data-list-item-id="e4567">Cell</li></ul></td></tr></tbody></table>`;++ for (const format of ["html", "markdown"] as const) {+ const { payload } = mapByNoteType(note, content, format);+ expect(payload).not.toContain("data-list-item-id");+ expect(payload).toContain("Bullet");+ expect(payload).toContain("Cell");+ }+ });+it("exports markdown code notes with a .md extension", () => {// `mime-types` doesn't recognize Trilium's custom `text/x-markdown`;// without the explicit fallback this was exporting as `.code`.diff --git a/packages/trilium-core/src/services/export/single.ts b/packages/trilium-core/src/services/export/single.tsindex b8421e2ac1..4f17628b28 100644--- a/packages/trilium-core/src/services/export/single.ts+++ b/packages/trilium-core/src/services/export/single.ts@@ -10,6 +10,7 @@ import type BNote from "../../becca/entities/bnote.js";import type TaskContext from "../task_context.js";import { escapeHtml,getContentDisposition } from "../utils/index.js";import mdService from "./markdown.js";+import { stripListItemIds } from "./strip_list_item_ids.js";import { ExportFormat } from "../../meta.js";import { encodeBase64 } from "../utils/binary.js";@@ -59,6 +60,9 @@ export function mapByNoteType(note: BNote, content: string | Uint8Array, format:}if (note.type === "text") {+ // Drop CKEditor's editor-only per-list-item bookkeeping from both HTML and Markdown output.+ content = stripListItemIds(content);+if (format === "html") {content = inlineAttachments(content);diff --git a/packages/trilium-core/src/services/export/strip_list_item_ids.ts b/packages/trilium-core/src/services/export/strip_list_item_ids.tsnew file mode 100644index 0000000000..9eac5ae95e--- /dev/null+++ b/packages/trilium-core/src/services/export/strip_list_item_ids.ts@@ -0,0 +1,10 @@+/**+ * Removes the `data-list-item-id` attribute CKEditor's list feature stamps on every `<li>`. It is+ * that plugin's stable per-item identifier for round-tripping the flat list model — regenerated on+ * load and meaningless outside the editor — so it is dropped from exported content. This is the+ * server-side equivalent of CKEditor's `skipListItemIds` downcast option, which cannot be used here+ * because exports serialize already-stored HTML rather than a live editor.+ */+export function stripListItemIds(content: string): string {+ return content.replace(/ data-list-item-id="[^"]*"/g, "");+}diff --git a/packages/trilium-core/src/services/export/zip.spec.ts b/packages/trilium-core/src/services/export/zip.spec.tsindex f1ab9f77ce..fc0e7570ec 100644--- a/packages/trilium-core/src/services/export/zip.spec.ts+++ b/packages/trilium-core/src/services/export/zip.spec.ts@@ -191,6 +191,26 @@ describe.skipIf(isBrowserRuntime)("zip export (real DB)", () => {expect(entries[rootMeta.dataFileName!].toString("utf-8")).toContain("## Heading");});+ it("strips CKEditor's data-list-item-id from HTML and Markdown exports", async () => {+ // A plain list converts to Markdown syntax (turndown drops attributes), but a list+ // inside a table survives as raw HTML — both must come out without the editor-only id.+ const content = `<ul><li data-list-item-id="e0123">Bullet</li></ul>`+ + `<table><tbody><tr><td><ul><li data-list-item-id="e4567">Cell</li></ul></td></tr></tbody></table>`;+ const { note } = createNote("root", { title: "ListIds", content });+ const branch = note.getParentBranches()[0];++ for (const format of ["html", "markdown"] as const) {+ const { entries } = await exportSubtree(branch, format);+ const dataFileName = parseMeta(entries).files[0].dataFileName ?? "";+ const exported = entries[dataFileName].toString("utf-8");++ expect(exported).not.toContain("data-list-item-id");+ // The list content itself still round-trips.+ expect(exported).toContain("Bullet");+ expect(exported).toContain("Cell");+ }+ });+it("carries owned attributes into the note meta but drops out-of-export relations", async () => {// A note living outside the exported subtree: a relation to it is valid// to save but must be filtered out of the export meta.diff --git a/packages/trilium-core/src/services/export/zip.ts b/packages/trilium-core/src/services/export/zip.tsindex 6913c31d48..2d743be86e 100644--- a/packages/trilium-core/src/services/export/zip.ts+++ b/packages/trilium-core/src/services/export/zip.ts@@ -18,6 +18,7 @@ import { ValidationError } from "../../errors";import { extname } from "../utils/path";import { truncateUtf8Bytes } from "../utils/binary";import { rewriteMarkdownContentLinks, isMarkdownCodeNote } from "./rewrite_links.js";+import { stripListItemIds } from "./strip_list_item_ids.js";// Most filesystems cap a single path component at 255 bytes; keep exported file names within that.const MAX_FILENAME_BYTES = 255;@@ -301,6 +302,13 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,content = content.toString();}+ // Text notes store HTML regardless of the export format, so strip CKEditor's per-list-item+ // bookkeeping before the provider runs: gone from HTML exports and from the raw-HTML tables+ // that survive Markdown conversion alike.+ if (noteMeta?.type === "text" && typeof content === "string") {+ content = stripListItemIds(content);+ }+content = provider.prepareContent(title, content, noteMeta, note, branch);// Rewrite markdown-style links for code notes with markdown MIME.