Skip to content

Build your personal knowledge base with Trilium Notes

fix(export): fall back to a .dat dump for unhandled single-export note types

Elian Doran72a7e8efe9ed
diff --git a/packages/trilium-core/src/routes/api/export.spec.ts b/packages/trilium-core/src/routes/api/export.spec.ts
index 09e55d4850..f42e8da5ec 100644
--- a/packages/trilium-core/src/routes/api/export.spec.ts
+++ b/packages/trilium-core/src/routes/api/export.spec.ts
@@ -72,10 +72,10 @@ describe("Export API (core)", () => {
expect(res.headers["Content-Type"]).toBe("text/plain");
});
- it("rejects single-export of an unhandled note type instead of emitting a zero-byte .undefined file", async () => {
+ it("exports an unhandled note type as a .dat file instead of a zero-byte .undefined download", async () => {
// Regression: note types mapByNoteType doesn't handle (book, webView, mindMap, …) used to fall
- // through to res.send(undefined), producing a "<title>.undefined", zero-byte download. The safety
- // net now rejects them with a clear error rather than a corrupt file.
+ // through to res.send(undefined), producing a "<title>.undefined", zero-byte download. They now
+ // fall back to a raw dump with a generic `.dat` extension.
const created = await api.post<{ branch: { branchId: string } }>(
"/api/notes/root/children?target=into",
{ body: { title: "My book", type: "book", content: "" } }
@@ -83,7 +83,7 @@ describe("Export API (core)", () => {
expect(created.status).toBe(200);
const res = await api.get<string>(`/api/branches/${created.body.branch.branchId}/export/single/html/exportTask`);
- expect(res.status).toBe(500);
- expect(res.headers["Content-Type"]).toBe("text/plain");
+ expect(res.status).toBe(200);
+ expect(res.headers["Content-Disposition"]).toContain(".dat");
});
});
diff --git a/packages/trilium-core/src/services/export/single.spec.ts b/packages/trilium-core/src/services/export/single.spec.ts
index 0cd3fc97ef..6d3ed65182 100644
--- a/packages/trilium-core/src/services/export/single.spec.ts
+++ b/packages/trilium-core/src/services/export/single.spec.ts
@@ -46,6 +46,21 @@ describe("Note type mappings", () => {
expect(result.payload).toBe(content);
});
+ it("falls back to a raw .dat dump for note types without a dedicated mapping", () => {
+ // book/webView/mindMap/… aren't handled explicitly; rather than a zero-byte `.undefined`, they
+ // export their raw content with a generic extension (mirrors the zip export's `dat` fallback).
+ const note = buildNote({ type: "book", mime: "", title: "My book" });
+ expect(mapByNoteType(note, "raw contents", "html")).toMatchObject({
+ payload: "raw contents",
+ extension: "dat",
+ mime: "application/octet-stream"
+ });
+
+ // A note carrying its own MIME keeps it in the Content-Type.
+ const webView = buildNote({ type: "webView", mime: "text/html", title: "Site" });
+ expect(mapByNoteType(webView, "x", "html")).toMatchObject({ extension: "dat", mime: "text/html" });
+ });
+
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.ts
index 21bcf69a10..dcc1477ce4 100644
--- a/packages/trilium-core/src/services/export/single.ts
+++ b/packages/trilium-core/src/services/export/single.ts
@@ -13,27 +13,19 @@ import mdService from "./markdown.js";
import { stripListItemIds } from "./strip_list_item_ids.js";
import { ExportFormat } from "../../meta.js";
import { encodeBase64 } from "../utils/binary.js";
-import { ValidationError } from "../../errors.js";
function exportSingleNote(taskContext: TaskContext<"export">, branch: BBranch, format: ExportFormat, res: Response) {
const note = branch.getNote();
if (note.type === "image" || note.type === "file") {
- throw new ValidationError(`Note type '${note.type}' cannot be exported as a single file.`);
+ return [400, `Note type '${note.type}' cannot be exported as single file.`];
}
if (format !== "html" && format !== "markdown") {
- throw new ValidationError(`Unrecognized format '${format}'`);
+ return [400, `Unrecognized format '${format}'`];
}
const { payload, extension, mime } = mapByNoteType(note, note.getContent(), format);
-
- // Safety net: any note type mapByNoteType doesn't handle leaves these undefined. Reject with a
- // clear error rather than sending `undefined` — which produced a "<title>.undefined", zero-byte file.
- if (payload === undefined || extension === undefined) {
- throw new ValidationError(`Note type '${note.type}' cannot be exported as a single file.`);
- }
-
const fileName = `${note.title}.${extension}`;
res.setHeader("Content-Disposition", getContentDisposition(fileName));
@@ -113,6 +105,13 @@ export function mapByNoteType(note: BNote, content: string | Uint8Array, format:
payload = content;
extension = "json";
mime = "application/json";
+ } else {
+ // Any other note type: dump the raw content with a generic extension, so single-note export
+ // produces a real (if opaque) file rather than a zero-byte `.undefined`. Mirrors the `dat`
+ // fallback the zip export uses for unrecognised MIME types.
+ payload = content;
+ extension = "dat";
+ mime = note.mime || "application/octet-stream";
}
return { payload, extension, mime };