Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): stop capping zip export filenames at 30 characters

Elian Dorana83af43fbadb
diff --git a/packages/trilium-core/src/services/export/zip.spec.ts b/packages/trilium-core/src/services/export/zip.spec.ts
index d028f4e1f6..dbde7391c2 100644
--- a/packages/trilium-core/src/services/export/zip.spec.ts
+++ b/packages/trilium-core/src/services/export/zip.spec.ts
@@ -230,6 +230,57 @@ describe.skipIf(isBrowserRuntime)("zip export (real DB)", () => {
expect(childIds).not.toContain(excluded.noteId);
});
+ it("keeps note titles longer than 30 characters in the data file name", async () => {
+ // Regression for #10112: the base file name used to be hard-capped at
+ // 30 characters, mangling long titles in the export.
+ const longTitle = "This is a very long note title well beyond the old thirty char cap";
+ expect(longTitle.length).toBeGreaterThan(30);
+ const { note } = createNote("root", { title: longTitle, content: "<p>long</p>" });
+ const branch = note.getParentBranches()[0];
+
+ const { entries } = await exportSubtree(branch, "html");
+ const rootMeta = parseMeta(entries).files[0];
+
+ expect(rootMeta.dataFileName).toBe(`${longTitle}.html`);
+ expect(entries[`${longTitle}.html`]).toBeDefined();
+ });
+
+ it("keeps the full UTF-8 title for accented note names (issue #10112)", async () => {
+ // The reporter's archive showed "Jean 15.1-8 - prédication corr.md",
+ // i.e. a longer accented title chopped at 30 chars before ".md".
+ const accentedTitle = "Jean 15.1-8 - prédication corrigée";
+ expect(accentedTitle.length).toBeGreaterThan(30);
+ const { note } = createNote("root", { title: accentedTitle, content: "<h2>Heading</h2>" });
+ const branch = note.getParentBranches()[0];
+
+ const { entries } = await exportSubtree(branch, "markdown");
+ const rootMeta = parseMeta(entries).files[0];
+
+ expect(rootMeta.dataFileName).toBe(`${accentedTitle}.md`);
+ expect(entries[`${accentedTitle}.md`]).toBeDefined();
+ });
+
+ it("keeps long attachment names and strips illegal characters from them", async () => {
+ const { note } = createNote("root", { title: "AttachHost", content: "<p>host</p>" });
+ const longAttachmentTitle = "long attachment / with : illegal * chars beyond thirty characters";
+ getContext().init(() =>
+ note.saveAttachment({ role: "file", mime: "text/plain", title: longAttachmentTitle, content: "data" })
+ );
+ const branch = note.getParentBranches()[0];
+
+ const { entries } = await exportSubtree(branch, "html");
+ const rootMeta = parseMeta(entries).files[0];
+
+ const attMeta = (rootMeta.attachments ?? [])[0];
+ expect(attMeta).toBeDefined();
+ const attFileName = attMeta.dataFileName ?? "";
+ // Not capped at 30: the old behaviour cropped to 30 chars + extension.
+ expect(attFileName.length).toBeGreaterThan(33);
+ // Illegal filename characters are stripped (previously left untouched).
+ expect(attFileName).not.toMatch(/[/\\:*?"<>|]/);
+ expect(entries[attFileName]).toBeDefined();
+ });
+
it("emits a .clone data file when the same note appears twice in the subtree", async () => {
const { note: parent } = createNote("root", { title: "CloneHolder", content: "" });
const { note: folderA } = createNote(parent.noteId, { title: "FolderA", content: "" });
diff --git a/packages/trilium-core/src/services/export/zip.ts b/packages/trilium-core/src/services/export/zip.ts
index ff46b9bcab..2b116eb548 100644
--- a/packages/trilium-core/src/services/export/zip.ts
+++ b/packages/trilium-core/src/services/export/zip.ts
@@ -66,17 +66,6 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,
fileName = "note";
}
- // Crop fileName to avoid its length exceeding 30 and prevent cutting into the extension.
- if (fileName.length > 30) {
- // We use regex to match the extension to preserve multiple dots in extensions (e.g. .tar.gz).
- const match = fileName.match(/(\.[a-zA-Z0-9_.!#-]+)$/);
- const ext = match ? match[0] : "";
- // Crop the extension if extension length exceeds 30
- const croppedExt = ext.slice(-30);
- // Crop the file name section and append the cropped extension
- fileName = fileName.slice(0, 30 - croppedExt.length) + croppedExt;
- }
-
const existingExtension = extname(fileName).toLowerCase();
const newExtension = provider.mapExtension(type, mime, existingExtension, format);
@@ -85,6 +74,11 @@ async function exportToZip(taskContext: TaskContext<"export">, branch: BBranch,
fileName += `.${newExtension}`;
}
+ // sanitize() strips illegal filename characters and truncates to the 255-byte
+ // filesystem limit (byte-aware, via truncate-utf8-bytes). This keeps long
+ // titles and attachment names intact instead of hard-capping them at 30
+ // characters, while still guarding against over-long / unsafe names.
+ fileName = sanitize(fileName);
return getUniqueFilename(existingFileNames, fileName);
}