Notes/src/services/export/single.ts

126 lines
4.1 KiB
TypeScript
Raw Normal View History

2018-11-24 20:58:38 +01:00
"use strict";
import mimeTypes from "mime-types";
import html from "html";
import { getContentDisposition, escapeHtml } from "../utils.js";
import mdService from "./md.js";
import becca from "../../becca/becca.js";
import type TaskContext from "../task_context.js";
import type BBranch from "../../becca/entities/bbranch.js";
import type { Response } from "express";
2024-02-19 22:12:00 +02:00
function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown", res: Response) {
2020-06-20 12:31:38 +02:00
const note = branch.getNote();
2018-11-24 20:58:38 +01:00
2025-01-09 18:07:02 +02:00
if (note.type === "image" || note.type === "file") {
return [400, `Note type '${note.type}' cannot be exported as single file.`];
2018-11-24 20:58:38 +01:00
}
2025-01-09 18:07:02 +02:00
if (format !== "html" && format !== "markdown") {
return [400, `Unrecognized format '${format}'`];
2018-11-24 20:58:38 +01:00
}
let payload, extension, mime;
2020-06-20 12:31:38 +02:00
let content = note.getContent();
2024-02-19 22:12:00 +02:00
if (typeof content !== "string") {
2024-04-11 19:49:01 +03:00
throw new Error("Unsupported content type for export.");
2024-02-19 22:12:00 +02:00
}
2019-02-06 21:29:23 +01:00
2025-01-09 18:07:02 +02:00
if (note.type === "text") {
if (format === "html") {
content = inlineAttachments(content);
if (!content.toLowerCase().includes("<html")) {
content = `<html><head><meta charset="utf-8"></head><body>${content}</body></html>`;
}
2025-01-09 18:07:02 +02:00
payload = content.length < 100_000 ? html.prettyPrint(content, { indent_size: 2 }) : content;
2025-01-09 18:07:02 +02:00
extension = "html";
mime = "text/html";
} else if (format === "markdown") {
payload = mdService.toMarkdown(content);
2025-01-09 18:07:02 +02:00
extension = "md";
mime = "text/x-markdown";
2018-11-24 20:58:38 +01:00
}
2025-01-09 18:07:02 +02:00
} else if (note.type === "code") {
payload = content;
2025-01-09 18:07:02 +02:00
extension = mimeTypes.extension(note.mime) || "code";
2018-11-24 20:58:38 +01:00
mime = note.mime;
2025-01-09 18:07:02 +02:00
} else if (note.type === "relationMap" || note.type === "canvas" || note.type === "search") {
payload = content;
2025-01-09 18:07:02 +02:00
extension = "json";
mime = "application/json";
2018-11-24 20:58:38 +01:00
}
const fileName = `${note.title}.${extension}`;
2018-11-24 20:58:38 +01:00
2025-01-09 18:07:02 +02:00
res.setHeader("Content-Disposition", getContentDisposition(fileName));
res.setHeader("Content-Type", `${mime}; charset=UTF-8`);
2018-11-24 20:58:38 +01:00
res.send(payload);
2019-02-10 22:30:55 +01:00
2019-10-18 22:27:38 +02:00
taskContext.increaseProgressCount();
taskContext.taskSucceeded();
2018-11-24 20:58:38 +01:00
}
2024-02-19 22:12:00 +02:00
function inlineAttachments(content: string) {
2023-07-13 23:54:47 +02:00
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/?[^"]+"/g, (match, noteId) => {
const note = becca.getNote(noteId);
2025-01-09 18:07:02 +02:00
if (!note || !note.mime.startsWith("image/")) {
2023-07-13 23:54:47 +02:00
return match;
}
const imageContent = note.getContent();
if (!Buffer.isBuffer(imageContent)) {
return match;
}
2025-01-09 18:07:02 +02:00
const base64Content = imageContent.toString("base64");
2023-07-13 23:54:47 +02:00
const srcValue = `data:${note.mime};base64,${base64Content}`;
return `src="${srcValue}"`;
});
content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/?[^"]+"/g, (match, attachmentId) => {
const attachment = becca.getAttachment(attachmentId);
2025-01-09 18:07:02 +02:00
if (!attachment || !attachment.mime.startsWith("image/")) {
return match;
}
const attachmentContent = attachment.getContent();
if (!Buffer.isBuffer(attachmentContent)) {
return match;
}
2025-01-09 18:07:02 +02:00
const base64Content = attachmentContent.toString("base64");
const srcValue = `data:${attachment.mime};base64,${base64Content}`;
return `src="${srcValue}"`;
});
content = content.replace(/href="[^"]*#root[^"]*attachmentId=([a-zA-Z0-9_]+)\/?"/g, (match, attachmentId) => {
const attachment = becca.getAttachment(attachmentId);
if (!attachment) {
return match;
}
const attachmentContent = attachment.getContent();
if (!Buffer.isBuffer(attachmentContent)) {
return match;
}
2025-01-09 18:07:02 +02:00
const base64Content = attachmentContent.toString("base64");
const hrefValue = `data:${attachment.mime};base64,${base64Content}`;
return `href="${hrefValue}" download="${escapeHtml(attachment.title)}"`;
});
return content;
}
export default {
2018-11-24 20:58:38 +01:00
exportSingleNote
2020-06-20 12:31:38 +02:00
};