2018-11-24 20:58:38 +01:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:37:45 +03:00
|
|
|
import mimeTypes from "mime-types";
|
|
|
|
import html from "html";
|
2025-01-02 13:47:44 +01:00
|
|
|
import { getContentDisposition, escapeHtml } from "../utils.js";
|
2024-07-18 21:35:17 +03:00
|
|
|
import mdService from "./md.js";
|
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import TaskContext from "../task_context.js";
|
|
|
|
import BBranch from "../../becca/entities/bbranch.js";
|
2025-01-09 18:07:02 +02:00
|
|
|
import { 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") {
|
2023-05-03 23:17:07 +02:00
|
|
|
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") {
|
2023-05-03 23:17:07 +02:00
|
|
|
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") {
|
2023-05-29 23:42:08 +02:00
|
|
|
content = inlineAttachments(content);
|
2023-05-03 23:17:07 +02:00
|
|
|
|
2019-03-26 22:24:04 +01:00
|
|
|
if (!content.toLowerCase().includes("<html")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
content = `<html><head><meta charset="utf-8"></head><body>${content}</body></html>`;
|
2019-01-25 21:34:14 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
payload = content.length < 100_000 ? html.prettyPrint(content, { indent_size: 2 }) : content;
|
2023-05-29 23:42:08 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
extension = "html";
|
|
|
|
mime = "text/html";
|
|
|
|
} else if (format === "markdown") {
|
2019-09-30 20:56:32 +02:00
|
|
|
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") {
|
2019-03-26 22:24:04 +01:00
|
|
|
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") {
|
2019-03-26 22:24:04 +01:00
|
|
|
payload = content;
|
2025-01-09 18:07:02 +02:00
|
|
|
extension = "json";
|
|
|
|
mime = "application/json";
|
2018-11-24 20:58:38 +01:00
|
|
|
}
|
|
|
|
|
2023-05-03 23:17:07 +02: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}"`;
|
|
|
|
});
|
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
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/")) {
|
2023-05-29 23:42:08 +02:00
|
|
|
return match;
|
|
|
|
}
|
2023-05-03 23:17:07 +02:00
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
const attachmentContent = attachment.getContent();
|
|
|
|
if (!Buffer.isBuffer(attachmentContent)) {
|
|
|
|
return match;
|
2023-05-03 23:17:07 +02:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const base64Content = attachmentContent.toString("base64");
|
2023-05-29 23:42:08 +02:00
|
|
|
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;
|
2023-05-03 23:17:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const attachmentContent = attachment.getContent();
|
|
|
|
if (!Buffer.isBuffer(attachmentContent)) {
|
2023-05-29 23:42:08 +02:00
|
|
|
return match;
|
2023-05-03 23:17:07 +02:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const base64Content = attachmentContent.toString("base64");
|
2023-05-29 23:42:08 +02:00
|
|
|
const hrefValue = `data:${attachment.mime};base64,${base64Content}`;
|
2023-05-03 23:17:07 +02:00
|
|
|
|
2025-01-02 13:47:44 +01:00
|
|
|
return `href="${hrefValue}" download="${escapeHtml(attachment.title)}"`;
|
2023-05-29 23:42:08 +02:00
|
|
|
});
|
2023-05-03 23:17:07 +02:00
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2018-11-24 20:58:38 +01:00
|
|
|
exportSingleNote
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|