mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-09-02 13:02:00 +08:00
refactor(export/single): make note type mapping testable
This commit is contained in:
parent
60d3bafc8e
commit
6ab820f172
@ -8,6 +8,7 @@ import becca from "../../becca/becca.js";
|
|||||||
import type TaskContext from "../task_context.js";
|
import type TaskContext from "../task_context.js";
|
||||||
import type BBranch from "../../becca/entities/bbranch.js";
|
import type BBranch from "../../becca/entities/bbranch.js";
|
||||||
import type { Response } from "express";
|
import type { Response } from "express";
|
||||||
|
import type BNote from "../../becca/entities/bnote.js";
|
||||||
|
|
||||||
function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown", res: Response) {
|
function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown", res: Response) {
|
||||||
const note = branch.getNote();
|
const note = branch.getNote();
|
||||||
@ -20,9 +21,21 @@ function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "ht
|
|||||||
return [400, `Unrecognized format '${format}'`];
|
return [400, `Unrecognized format '${format}'`];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { payload, extension, mime } = mapByNoteType(note, note.getContent(), format);
|
||||||
|
const fileName = `${note.title}.${extension}`;
|
||||||
|
|
||||||
|
res.setHeader("Content-Disposition", getContentDisposition(fileName));
|
||||||
|
res.setHeader("Content-Type", `${mime}; charset=UTF-8`);
|
||||||
|
|
||||||
|
res.send(payload);
|
||||||
|
|
||||||
|
taskContext.increaseProgressCount();
|
||||||
|
taskContext.taskSucceeded();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mapByNoteType(note: BNote, content: string | Buffer<ArrayBufferLike>, format: "html" | "markdown") {
|
||||||
let payload, extension, mime;
|
let payload, extension, mime;
|
||||||
|
|
||||||
let content = note.getContent();
|
|
||||||
if (typeof content !== "string") {
|
if (typeof content !== "string") {
|
||||||
throw new Error("Unsupported content type for export.");
|
throw new Error("Unsupported content type for export.");
|
||||||
}
|
}
|
||||||
@ -58,15 +71,7 @@ function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "ht
|
|||||||
mime = "application/json";
|
mime = "application/json";
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileName = `${note.title}.${extension}`;
|
return { payload, extension, mime };
|
||||||
|
|
||||||
res.setHeader("Content-Disposition", getContentDisposition(fileName));
|
|
||||||
res.setHeader("Content-Type", `${mime}; charset=UTF-8`);
|
|
||||||
|
|
||||||
res.send(payload);
|
|
||||||
|
|
||||||
taskContext.increaseProgressCount();
|
|
||||||
taskContext.taskSucceeded();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function inlineAttachments(content: string) {
|
function inlineAttachments(content: string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user