2018-11-24 20:58:38 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const mimeTypes = require('mime-types');
|
|
|
|
const html = require('html');
|
2019-01-13 10:22:17 +01:00
|
|
|
const utils = require('../utils');
|
2019-09-30 20:56:32 +02:00
|
|
|
const mdService = require('./md');
|
2023-05-03 23:17:07 +02:00
|
|
|
const becca = require("../../becca/becca");
|
2018-11-24 20:58:38 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function exportSingleNote(taskContext, branch, format, res) {
|
|
|
|
const note = branch.getNote();
|
2018-11-24 20:58:38 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-02-06 21:29:23 +01:00
|
|
|
|
2018-11-24 20:58:38 +01: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
|
|
|
}
|
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
payload = content.length < 100_000
|
|
|
|
? html.prettyPrint(content, {indent_size: 2})
|
|
|
|
: content;
|
|
|
|
|
2018-11-24 20:58:38 +01:00
|
|
|
extension = 'html';
|
|
|
|
mime = 'text/html';
|
|
|
|
}
|
|
|
|
else if (format === 'markdown') {
|
2019-09-30 20:56:32 +02:00
|
|
|
payload = mdService.toMarkdown(content);
|
2018-11-24 20:58:38 +01:00
|
|
|
extension = 'md';
|
2019-02-25 21:57:11 +01:00
|
|
|
mime = 'text/x-markdown'
|
2018-11-24 20:58:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (note.type === 'code') {
|
2019-03-26 22:24:04 +01:00
|
|
|
payload = content;
|
2018-11-24 20:58:38 +01:00
|
|
|
extension = mimeTypes.extension(note.mime) || 'code';
|
|
|
|
mime = note.mime;
|
|
|
|
}
|
2022-12-06 23:01:42 +01:00
|
|
|
else if (note.type === 'relationMap' || note.type === 'canvas' || note.type === 'search') {
|
2019-03-26 22:24:04 +01:00
|
|
|
payload = content;
|
2018-11-24 20:58:38 +01:00
|
|
|
extension = 'json';
|
|
|
|
mime = 'application/json';
|
|
|
|
}
|
|
|
|
|
2023-05-03 23:17:07 +02:00
|
|
|
const fileName = `${note.title}.${extension}`;
|
2018-11-24 20:58:38 +01:00
|
|
|
|
2023-05-03 23:17:07 +02:00
|
|
|
res.setHeader('Content-Disposition', utils.getContentDisposition(fileName));
|
2022-12-21 15:19:05 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
function inlineAttachments(content) {
|
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);
|
|
|
|
if (!note || !note.mime.startsWith('image/')) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageContent = note.getContent();
|
|
|
|
if (!Buffer.isBuffer(imageContent)) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
const base64Content = imageContent.toString('base64');
|
|
|
|
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);
|
|
|
|
if (!attachment || !attachment.mime.startsWith('image/')) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-29 23:42:08 +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;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
return `href="${hrefValue}" download="${utils.escapeHtml(attachment.title)}"`;
|
|
|
|
});
|
2023-05-03 23:17:07 +02:00
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2018-11-24 20:58:38 +01:00
|
|
|
module.exports = {
|
|
|
|
exportSingleNote
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|