inline attachment images into single HTML export

This commit is contained in:
zadam 2023-05-03 23:17:07 +02:00
parent cd01886eb2
commit fbffc6b2b5
3 changed files with 39 additions and 9 deletions

View File

@ -97,7 +97,7 @@ export default class LoadResults {
}
addNoteContent(noteIds, componentId) {
for (const noteId of noteIds) {
for (const noteId of noteIds || []) {
this.contentNoteIdToComponentId.push({noteId, componentId});
}
}

View File

@ -4,16 +4,17 @@ const mimeTypes = require('mime-types');
const html = require('html');
const utils = require('../utils');
const mdService = require('./md');
const becca = require("../../becca/becca");
function exportSingleNote(taskContext, branch, format, res) {
const note = branch.getNote();
if (note.type === 'image' || note.type === 'file') {
return [400, `Note type ${note.type} cannot be exported as single file.`];
return [400, `Note type '${note.type}' cannot be exported as single file.`];
}
if (format !== 'html' && format !== 'markdown') {
return [400, `Unrecognized format ${format}`];
return [400, `Unrecognized format '${format}'`];
}
let payload, extension, mime;
@ -22,6 +23,8 @@ function exportSingleNote(taskContext, branch, format, res) {
if (note.type === 'text') {
if (format === 'html') {
content = inlineAttachmentImages(content);
if (!content.toLowerCase().includes("<html")) {
content = `<html><head><meta charset="utf-8"></head><body>${content}</body></html>`;
}
@ -47,9 +50,9 @@ function exportSingleNote(taskContext, branch, format, res) {
mime = 'application/json';
}
const filename = `${note.title}.${extension}`;
const fileName = `${note.title}.${extension}`;
res.setHeader('Content-Disposition', utils.getContentDisposition(filename));
res.setHeader('Content-Disposition', utils.getContentDisposition(fileName));
res.setHeader('Content-Type', `${mime}; charset=UTF-8`);
res.send(payload);
@ -58,6 +61,34 @@ function exportSingleNote(taskContext, branch, format, res) {
taskContext.taskSucceeded();
}
function inlineAttachmentImages(content) {
const re = /src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/?[^"]+"/g;
let match;
while (match = re.exec(content)) {
const attachment = becca.getAttachment(match[1]);
if (!attachment) {
continue;
}
if (!attachment.mime.startsWith('image/')) {
continue;
}
const attachmentContent = attachment.getContent();
if (!Buffer.isBuffer(attachmentContent)) {
continue;
}
const base64Content = attachmentContent.toString('base64');
const srcValue = `data:${attachment.mime};base64,${base64Content}`;
content = content.replaceAll(match[0], `src="${srcValue}"`);
}
return content;
}
module.exports = {
exportSingleNote
};

View File

@ -174,15 +174,14 @@ async function exportToZip(taskContext, branch, format, res, setHeaders = true)
if (attachments.length > 0) {
meta.attachments = attachments
.filter(attachment => ["canvasSvg", "mermaidSvg"].includes(attachment.name))
.map(attachment => ({
name: attachment.name,
title: attachment.title,
role: attachment.role,
mime: attachment.mime,
dataFileName: getDataFileName(
null,
attachment.mime,
baseFileName + "_" + attachment.name,
baseFileName + "_" + attachment.title,
existingFileNames
)
}));