mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
inline attachment images into single HTML export
This commit is contained in:
parent
cd01886eb2
commit
fbffc6b2b5
@ -97,7 +97,7 @@ export default class LoadResults {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addNoteContent(noteIds, componentId) {
|
addNoteContent(noteIds, componentId) {
|
||||||
for (const noteId of noteIds) {
|
for (const noteId of noteIds || []) {
|
||||||
this.contentNoteIdToComponentId.push({noteId, componentId});
|
this.contentNoteIdToComponentId.push({noteId, componentId});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,17 @@ const mimeTypes = require('mime-types');
|
|||||||
const html = require('html');
|
const html = require('html');
|
||||||
const utils = require('../utils');
|
const utils = require('../utils');
|
||||||
const mdService = require('./md');
|
const mdService = require('./md');
|
||||||
|
const becca = require("../../becca/becca");
|
||||||
|
|
||||||
function exportSingleNote(taskContext, branch, format, res) {
|
function exportSingleNote(taskContext, branch, format, res) {
|
||||||
const note = branch.getNote();
|
const note = branch.getNote();
|
||||||
|
|
||||||
if (note.type === 'image' || note.type === 'file') {
|
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') {
|
if (format !== 'html' && format !== 'markdown') {
|
||||||
return [400, `Unrecognized format ${format}`];
|
return [400, `Unrecognized format '${format}'`];
|
||||||
}
|
}
|
||||||
|
|
||||||
let payload, extension, mime;
|
let payload, extension, mime;
|
||||||
@ -22,6 +23,8 @@ function exportSingleNote(taskContext, branch, format, res) {
|
|||||||
|
|
||||||
if (note.type === 'text') {
|
if (note.type === 'text') {
|
||||||
if (format === 'html') {
|
if (format === 'html') {
|
||||||
|
content = inlineAttachmentImages(content);
|
||||||
|
|
||||||
if (!content.toLowerCase().includes("<html")) {
|
if (!content.toLowerCase().includes("<html")) {
|
||||||
content = `<html><head><meta charset="utf-8"></head><body>${content}</body></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';
|
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.setHeader('Content-Type', `${mime}; charset=UTF-8`);
|
||||||
|
|
||||||
res.send(payload);
|
res.send(payload);
|
||||||
@ -58,6 +61,34 @@ function exportSingleNote(taskContext, branch, format, res) {
|
|||||||
taskContext.taskSucceeded();
|
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 = {
|
module.exports = {
|
||||||
exportSingleNote
|
exportSingleNote
|
||||||
};
|
};
|
||||||
|
@ -174,15 +174,14 @@ async function exportToZip(taskContext, branch, format, res, setHeaders = true)
|
|||||||
|
|
||||||
if (attachments.length > 0) {
|
if (attachments.length > 0) {
|
||||||
meta.attachments = attachments
|
meta.attachments = attachments
|
||||||
.filter(attachment => ["canvasSvg", "mermaidSvg"].includes(attachment.name))
|
|
||||||
.map(attachment => ({
|
.map(attachment => ({
|
||||||
|
title: attachment.title,
|
||||||
name: attachment.name,
|
role: attachment.role,
|
||||||
mime: attachment.mime,
|
mime: attachment.mime,
|
||||||
dataFileName: getDataFileName(
|
dataFileName: getDataFileName(
|
||||||
null,
|
null,
|
||||||
attachment.mime,
|
attachment.mime,
|
||||||
baseFileName + "_" + attachment.name,
|
baseFileName + "_" + attachment.title,
|
||||||
existingFileNames
|
existingFileNames
|
||||||
)
|
)
|
||||||
}));
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user