2018-11-16 12:12:04 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const html = require('html');
|
2019-12-22 10:57:55 +01:00
|
|
|
const dateUtils = require('../date_utils');
|
2018-11-30 22:43:03 +01:00
|
|
|
const path = require('path');
|
2018-11-24 14:44:56 +01:00
|
|
|
const mimeTypes = require('mime-types');
|
2019-09-30 20:56:32 +02:00
|
|
|
const mdService = require('./md');
|
2018-11-27 10:31:55 +01:00
|
|
|
const packageInfo = require('../../../package.json');
|
2019-01-13 10:22:17 +01:00
|
|
|
const utils = require('../utils');
|
2019-09-01 22:09:55 +02:00
|
|
|
const protectedSessionService = require('../protected_session');
|
2019-01-13 12:21:17 +01:00
|
|
|
const sanitize = require("sanitize-filename");
|
2019-12-25 11:34:45 +01:00
|
|
|
const fs = require("fs");
|
2021-06-26 23:10:03 +02:00
|
|
|
const becca = require("../../becca/becca");
|
2019-12-25 11:34:45 +01:00
|
|
|
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
|
2021-05-14 22:57:44 +02:00
|
|
|
const archiver = require('archiver');
|
2022-02-09 22:39:37 +01:00
|
|
|
const log = require("../log");
|
2022-12-29 21:15:34 +01:00
|
|
|
const TaskContext = require("../task_context");
|
|
|
|
const ValidationError = require("../../errors/validation_error");
|
2023-05-06 14:38:45 +02:00
|
|
|
const NoteMeta = require("../meta/note_meta");
|
|
|
|
const AttachmentMeta = require("../meta/attachment_meta");
|
|
|
|
const AttributeMeta = require("../meta/attribute_meta");
|
2018-11-24 14:44:56 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-18 22:27:38 +02:00
|
|
|
* @param {TaskContext} taskContext
|
2023-01-03 14:31:46 +01:00
|
|
|
* @param {BBranch} branch
|
2019-02-10 22:30:55 +01:00
|
|
|
* @param {string} format - 'html' or 'markdown'
|
2023-01-15 21:04:17 +01:00
|
|
|
* @param {object} res - express response
|
|
|
|
* @param {boolean} setHeaders
|
2018-11-24 14:44:56 +01:00
|
|
|
*/
|
2022-12-29 23:12:38 +01:00
|
|
|
async function exportToZip(taskContext, branch, format, res, setHeaders = true) {
|
2022-12-29 21:15:34 +01:00
|
|
|
if (!['html', 'markdown'].includes(format)) {
|
|
|
|
throw new ValidationError(`Only 'html' and 'markdown' allowed as export format, '${format}' given`);
|
|
|
|
}
|
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
const archive = archiver('zip', {
|
|
|
|
zlib: { level: 9 } // Sets the compression level.
|
|
|
|
});
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/** @type {Object.<string, NoteMeta>} */
|
2018-11-25 22:38:09 +01:00
|
|
|
const noteIdToMeta = {};
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {Object.<string, integer>} existingFileNames
|
|
|
|
* @param {string} fileName
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-11-25 22:09:52 +01:00
|
|
|
function getUniqueFilename(existingFileNames, fileName) {
|
2018-11-25 15:17:28 +01:00
|
|
|
const lcFileName = fileName.toLowerCase();
|
2018-11-25 10:26:45 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
if (lcFileName in existingFileNames) {
|
2018-11-25 15:17:28 +01:00
|
|
|
let index;
|
|
|
|
let newName;
|
|
|
|
|
|
|
|
do {
|
2018-11-25 22:09:52 +01:00
|
|
|
index = existingFileNames[lcFileName]++;
|
2018-11-25 15:17:28 +01:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
newName = `${index}_${lcFileName}`;
|
2018-11-25 15:17:28 +01:00
|
|
|
}
|
2018-11-25 22:09:52 +01:00
|
|
|
while (newName in existingFileNames);
|
2018-11-25 15:17:28 +01:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
return `${index}_${fileName}`;
|
2018-11-25 15:17:28 +01:00
|
|
|
}
|
2023-05-06 14:38:45 +02:00
|
|
|
else {[]
|
2018-11-25 22:09:52 +01:00
|
|
|
existingFileNames[lcFileName] = 1;
|
2018-11-25 15:17:28 +01:00
|
|
|
|
|
|
|
return fileName;
|
|
|
|
}
|
2018-11-25 10:26:45 +01:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {string|null} type
|
|
|
|
* @param {string} mime
|
|
|
|
* @param {string} baseFileName
|
|
|
|
* @param {Object.<string, integer>} existingFileNames
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2023-01-25 09:55:29 +01:00
|
|
|
function getDataFileName(type, mime, baseFileName, existingFileNames) {
|
2021-12-17 22:03:00 +01:00
|
|
|
let fileName = baseFileName;
|
|
|
|
|
2022-01-15 22:35:54 +01:00
|
|
|
let existingExtension = path.extname(fileName).toLowerCase();
|
|
|
|
let newExtension;
|
2022-02-09 22:39:37 +01:00
|
|
|
|
2021-12-17 22:03:00 +01:00
|
|
|
if (fileName.length > 30) {
|
|
|
|
fileName = fileName.substr(0, 30);
|
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
// the following two are handled specifically since we always want to have these extensions no matter the automatic detection
|
2019-07-11 20:54:57 +02:00
|
|
|
// and/or existing detected extensions in the note name
|
2023-01-25 09:55:29 +01:00
|
|
|
if (type === 'text' && format === 'markdown') {
|
2019-07-11 20:54:57 +02:00
|
|
|
newExtension = 'md';
|
|
|
|
}
|
2023-01-25 09:55:29 +01:00
|
|
|
else if (type === 'text' && format === 'html') {
|
2019-07-11 20:54:57 +02:00
|
|
|
newExtension = 'html';
|
2018-11-25 22:09:52 +01:00
|
|
|
}
|
2023-01-25 09:55:29 +01:00
|
|
|
else if (mime === 'application/x-javascript' || mime === 'text/javascript') {
|
2019-07-11 20:54:57 +02:00
|
|
|
newExtension = 'js';
|
|
|
|
}
|
|
|
|
else if (existingExtension.length > 0) { // if the page already has an extension, then we'll just keep it
|
|
|
|
newExtension = null;
|
2018-11-25 22:09:52 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-01-25 09:55:29 +01:00
|
|
|
if (mime?.toLowerCase()?.trim() === "image/jpg") {
|
2021-12-17 22:03:00 +01:00
|
|
|
newExtension = 'jpg';
|
2023-01-25 09:55:29 +01:00
|
|
|
} else if (mime?.toLowerCase()?.trim() === "text/mermaid") {
|
|
|
|
newExtension = 'txt';
|
|
|
|
} else {
|
|
|
|
newExtension = mimeTypes.extension(mime) || "dat";
|
2021-12-17 22:03:00 +01:00
|
|
|
}
|
2021-05-15 13:22:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 22:15:57 +02:00
|
|
|
// if the note is already named with extension (e.g. "jquery"), then it's silly to append exact same extension again
|
2022-12-21 15:19:05 +01:00
|
|
|
if (newExtension && existingExtension !== `.${newExtension.toLowerCase()}`) {
|
|
|
|
fileName += `.${newExtension}`;
|
2018-11-25 22:09:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return getUniqueFilename(existingFileNames, fileName);
|
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {BBranch} branch
|
|
|
|
* @param {NoteMeta} parentMeta
|
|
|
|
* @param {Object.<string, integer>} existingFileNames
|
|
|
|
* @returns {NoteMeta|null}
|
|
|
|
*/
|
2023-05-06 22:50:28 +02:00
|
|
|
function createNoteMeta(branch, parentMeta, existingFileNames) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = branch.getNote();
|
2018-11-25 22:09:52 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (note.hasOwnedLabel('excludeFromExport')) {
|
2023-05-06 14:38:45 +02:00
|
|
|
return null;
|
2018-11-25 22:09:52 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 21:50:16 +01:00
|
|
|
const title = note.getTitleOrProtected();
|
2022-12-21 15:19:05 +01:00
|
|
|
const completeTitle = branch.prefix ? (`${branch.prefix} - ${title}`) : title;
|
2019-12-01 11:49:14 +01:00
|
|
|
let baseFileName = sanitize(completeTitle);
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
if (baseFileName.length > 200) { // the actual limit is 256 bytes(!) but let's be conservative
|
2019-12-01 11:49:14 +01:00
|
|
|
baseFileName = baseFileName.substr(0, 200);
|
|
|
|
}
|
|
|
|
|
2019-09-01 16:46:36 +02:00
|
|
|
const notePath = parentMeta.notePath.concat([note.noteId]);
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2018-11-25 22:38:09 +01:00
|
|
|
if (note.noteId in noteIdToMeta) {
|
2022-12-21 15:19:05 +01:00
|
|
|
const fileName = getUniqueFilename(existingFileNames, `${baseFileName}.clone.${format === 'html' ? 'html' : 'md'}`);
|
2018-11-25 22:09:52 +01:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
const meta = new NoteMeta();
|
|
|
|
meta.isClone = true;
|
|
|
|
meta.noteId = note.noteId;
|
|
|
|
meta.notePath = notePath;
|
|
|
|
meta.title = note.getTitleOrProtected();
|
|
|
|
meta.prefix = branch.prefix;
|
|
|
|
meta.dataFileName = fileName;
|
|
|
|
meta.type = 'text'; // export will have text description
|
|
|
|
meta.format = format;
|
|
|
|
return meta;
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
const meta = new NoteMeta();
|
|
|
|
meta.isClone = false;
|
|
|
|
meta.noteId = note.noteId;
|
|
|
|
meta.notePath = notePath;
|
|
|
|
meta.title = note.getTitleOrProtected();
|
|
|
|
meta.notePosition = branch.notePosition;
|
|
|
|
meta.prefix = branch.prefix;
|
|
|
|
meta.isExpanded = branch.isExpanded;
|
|
|
|
meta.type = note.type;
|
|
|
|
meta.mime = note.mime;
|
|
|
|
meta.attributes = note.getOwnedAttributes().map(attribute => {
|
|
|
|
const attrMeta = new AttributeMeta();
|
|
|
|
attrMeta.type = attribute.type;
|
|
|
|
attrMeta.name = attribute.name;
|
|
|
|
attrMeta.value = attribute.value;
|
|
|
|
attrMeta.isInheritable = attribute.isInheritable;
|
|
|
|
attrMeta.position = attribute.position;
|
|
|
|
return attrMeta;
|
|
|
|
});
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-10-18 22:27:38 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-10 22:30:55 +01:00
|
|
|
|
2018-11-24 14:44:56 +01:00
|
|
|
if (note.type === 'text') {
|
2018-11-25 22:09:52 +01:00
|
|
|
meta.format = format;
|
2018-11-24 14:44:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-25 22:38:09 +01:00
|
|
|
noteIdToMeta[note.noteId] = meta;
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2022-01-15 09:04:44 +01:00
|
|
|
const childBranches = note.getChildBranches()
|
2022-12-21 16:11:00 +01:00
|
|
|
.filter(branch => branch.noteId !== '_hidden');
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-09-01 22:09:55 +02:00
|
|
|
const available = !note.isProtected || protectedSessionService.isProtectedSessionAvailable();
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
// if it's a leaf, then we'll export it even if it's empty
|
2020-12-22 22:30:04 +01:00
|
|
|
if (available && (note.getContent().length > 0 || childBranches.length === 0)) {
|
2023-01-25 09:55:29 +01:00
|
|
|
meta.dataFileName = getDataFileName(note.type, note.mime, baseFileName, existingFileNames);
|
|
|
|
}
|
|
|
|
|
2023-03-16 12:17:55 +01:00
|
|
|
const attachments = note.getAttachments();
|
2023-05-06 14:38:45 +02:00
|
|
|
meta.attachments = attachments
|
|
|
|
.map(attachment => {
|
|
|
|
const attMeta = new AttachmentMeta();
|
|
|
|
attMeta.attachmentId = attachment.attachmentId;
|
|
|
|
attMeta.title = attachment.title;
|
|
|
|
attMeta.role = attachment.role;
|
|
|
|
attMeta.mime = attachment.mime;
|
2023-05-06 22:50:28 +02:00
|
|
|
attMeta.position = attachment.position;
|
2023-05-06 14:38:45 +02:00
|
|
|
attMeta.dataFileName = getDataFileName(
|
2023-03-08 09:01:23 +01:00
|
|
|
null,
|
2023-03-16 11:03:28 +01:00
|
|
|
attachment.mime,
|
2023-05-03 23:17:07 +02:00
|
|
|
baseFileName + "_" + attachment.title,
|
2023-03-08 09:01:23 +01:00
|
|
|
existingFileNames
|
2023-05-06 14:38:45 +02:00
|
|
|
);
|
|
|
|
return attMeta;
|
|
|
|
});
|
2023-03-08 09:01:23 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
if (childBranches.length > 0) {
|
|
|
|
meta.dirFileName = getUniqueFilename(existingFileNames, baseFileName);
|
|
|
|
meta.children = [];
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2018-11-26 22:22:16 +01:00
|
|
|
// namespace is shared by children in the same note
|
2018-11-25 22:09:52 +01:00
|
|
|
const childExistingNames = {};
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
for (const childBranch of childBranches) {
|
2023-05-06 22:50:28 +02:00
|
|
|
const note = createNoteMeta(childBranch, meta, childExistingNames);
|
2018-11-24 14:44:56 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
// can be undefined if export is disabled for this note
|
|
|
|
if (note) {
|
|
|
|
meta.children.push(note);
|
|
|
|
}
|
2018-11-24 14:44:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {string} targetNoteId
|
|
|
|
* @param {NoteMeta} sourceMeta
|
|
|
|
* @return {string|null}
|
|
|
|
*/
|
2023-05-06 22:50:28 +02:00
|
|
|
function getNoteTargetUrl(targetNoteId, sourceMeta) {
|
2019-08-31 11:15:32 +02:00
|
|
|
const targetMeta = noteIdToMeta[targetNoteId];
|
2019-02-06 21:29:23 +01:00
|
|
|
|
2019-08-31 11:15:32 +02:00
|
|
|
if (!targetMeta) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2019-08-31 11:15:32 +02:00
|
|
|
const targetPath = targetMeta.notePath.slice();
|
|
|
|
const sourcePath = sourceMeta.notePath.slice();
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
// > 1 for the edge case that targetPath and sourcePath are exact same (a link to itself)
|
2019-08-31 11:15:32 +02:00
|
|
|
while (targetPath.length > 1 && sourcePath.length > 1 && targetPath[0] === sourcePath[0]) {
|
|
|
|
targetPath.shift();
|
|
|
|
sourcePath.shift();
|
|
|
|
}
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2019-08-31 11:15:32 +02:00
|
|
|
let url = "../".repeat(sourcePath.length - 1);
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2019-08-31 11:15:32 +02:00
|
|
|
for (let i = 0; i < targetPath.length - 1; i++) {
|
|
|
|
const meta = noteIdToMeta[targetPath[i]];
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
url += `${encodeURIComponent(meta.dirFileName)}/`;
|
2019-08-31 11:15:32 +02:00
|
|
|
}
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2019-08-31 11:15:32 +02:00
|
|
|
const meta = noteIdToMeta[targetPath[targetPath.length - 1]];
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
// link can target note which is only "folder-note" and as such, will not have a file in an export
|
2023-02-17 14:49:45 +01:00
|
|
|
url += encodeURIComponent(meta.dataFileName || meta.dirFileName);
|
2019-08-31 11:15:32 +02:00
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {string} content
|
|
|
|
* @param {NoteMeta} noteMeta
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2023-05-06 22:50:28 +02:00
|
|
|
function rewriteLinks(content, noteMeta) {
|
2022-12-29 10:25:49 +01:00
|
|
|
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/[^"]*"/g, (match, targetNoteId) => {
|
2023-05-06 22:50:28 +02:00
|
|
|
const url = getNoteTargetUrl(targetNoteId, noteMeta);
|
|
|
|
|
|
|
|
return url ? `src="${url}"` : match;
|
|
|
|
});
|
|
|
|
|
|
|
|
content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/[^"]*"/g, (match, targetAttachmentId) => {
|
2023-05-29 23:42:08 +02:00
|
|
|
const url = findAttachment(targetAttachmentId);
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2019-12-25 11:56:55 +01:00
|
|
|
return url ? `src="${url}"` : match;
|
2019-08-31 11:15:32 +02:00
|
|
|
});
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
content = content.replace(/href="[^"]*#root[^"]*attachmentId=([a-zA-Z0-9_]+)\/?"/g, (match, targetAttachmentId) => {
|
|
|
|
const url = findAttachment(targetAttachmentId);
|
|
|
|
|
|
|
|
return url ? `href="${url}"` : match;
|
|
|
|
});
|
|
|
|
|
|
|
|
content = content.replace(/href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)[^"]*"/g, (match, targetNoteId) => {
|
2023-05-06 22:50:28 +02:00
|
|
|
const url = getNoteTargetUrl(targetNoteId, noteMeta);
|
2019-08-31 10:12:42 +02:00
|
|
|
|
2019-12-25 11:56:55 +01:00
|
|
|
return url ? `href="${url}"` : match;
|
2019-08-31 11:15:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return content;
|
2023-05-29 23:42:08 +02:00
|
|
|
|
|
|
|
function findAttachment(targetAttachmentId) {
|
|
|
|
let url;
|
|
|
|
|
|
|
|
const attachmentMeta = noteMeta.attachments.find(attMeta => attMeta.attachmentId === targetAttachmentId);
|
|
|
|
if (attachmentMeta) {
|
|
|
|
// easy job here, because attachment will be in the same directory as the note's data file.
|
|
|
|
url = attachmentMeta.dataFileName;
|
|
|
|
} else {
|
|
|
|
log.info(`Could not find attachment meta object for attachmentId '${targetAttachmentId}'`);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2019-08-31 10:12:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string|Buffer} content
|
|
|
|
* @param {NoteMeta} noteMeta
|
|
|
|
* @return {string|Buffer}
|
|
|
|
*/
|
2019-09-01 16:46:36 +02:00
|
|
|
function prepareContent(title, content, noteMeta) {
|
2019-08-31 10:12:42 +02:00
|
|
|
if (['html', 'markdown'].includes(noteMeta.format)) {
|
|
|
|
content = content.toString();
|
|
|
|
|
2023-05-06 22:50:28 +02:00
|
|
|
content = rewriteLinks(content, noteMeta);
|
2019-08-31 10:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (noteMeta.format === 'html') {
|
2019-08-31 11:15:32 +02:00
|
|
|
if (!content.substr(0, 100).toLowerCase().includes("<html")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
const cssUrl = `${"../".repeat(noteMeta.notePath.length - 1)}style.css`;
|
2023-05-03 16:19:28 +03:00
|
|
|
const htmlTitle = utils.escapeHtml(title);
|
2019-12-25 11:34:45 +01:00
|
|
|
|
2020-10-06 21:30:41 +02:00
|
|
|
// <base> element will make sure external links are openable - https://github.com/zadam/trilium/issues/1289#issuecomment-704066809
|
2019-08-31 11:15:32 +02:00
|
|
|
content = `<html>
|
2019-12-25 11:34:45 +01:00
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
2020-10-05 22:23:03 +02:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
2019-12-25 11:34:45 +01:00
|
|
|
<link rel="stylesheet" href="${cssUrl}">
|
2020-10-06 21:30:41 +02:00
|
|
|
<base target="_parent">
|
2023-05-04 19:56:02 +03:00
|
|
|
<title data-trilium-title>${htmlTitle}</title>
|
2019-12-25 11:34:45 +01:00
|
|
|
</head>
|
2023-01-12 16:37:30 +01:00
|
|
|
<body>
|
2023-01-13 23:54:47 +01:00
|
|
|
<div class="content">
|
2023-05-04 19:56:02 +03:00
|
|
|
<h1 data-trilium-h1>${htmlTitle}</h1>
|
2023-01-13 23:54:47 +01:00
|
|
|
|
|
|
|
<div class="ck-content">${content}</div>
|
|
|
|
</div>
|
2019-08-31 11:15:32 +02:00
|
|
|
</body>
|
|
|
|
</html>`;
|
2019-01-25 21:34:14 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 23:42:08 +02:00
|
|
|
return content.length < 100_000
|
2022-02-09 22:39:37 +01:00
|
|
|
? html.prettyPrint(content, {indent_size: 2})
|
|
|
|
: content;
|
2023-05-06 14:38:45 +02:00
|
|
|
} else if (noteMeta.format === 'markdown') {
|
2019-09-30 20:56:32 +02:00
|
|
|
let markdownContent = mdService.toMarkdown(content);
|
2019-08-31 11:15:32 +02:00
|
|
|
|
|
|
|
if (markdownContent.trim().length > 0 && !markdownContent.startsWith("# ")) {
|
2022-12-21 15:19:05 +01:00
|
|
|
markdownContent = `# ${title}\r
|
|
|
|
${markdownContent}`;
|
2019-08-31 11:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return markdownContent;
|
2023-05-06 14:38:45 +02:00
|
|
|
} else {
|
2019-02-06 21:29:23 +01:00
|
|
|
return content;
|
2018-11-25 10:26:45 +01:00
|
|
|
}
|
2018-11-25 22:09:52 +01:00
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {NoteMeta} noteMeta
|
|
|
|
* @param {string} filePathPrefix
|
|
|
|
*/
|
2020-06-20 12:31:38 +02:00
|
|
|
function saveNote(noteMeta, filePathPrefix) {
|
2023-05-06 14:38:45 +02:00
|
|
|
log.info(`Exporting note '${noteMeta.noteId}'`);
|
2022-02-09 22:39:37 +01:00
|
|
|
|
2018-11-26 14:47:46 +01:00
|
|
|
if (noteMeta.isClone) {
|
2023-05-06 22:50:28 +02:00
|
|
|
const targetUrl = getNoteTargetUrl(noteMeta.noteId, noteMeta);
|
2019-09-01 16:46:36 +02:00
|
|
|
|
2019-12-25 11:56:55 +01:00
|
|
|
let content = `<p>This is a clone of a note. Go to its <a href="${targetUrl}">primary location</a>.</p>`;
|
2019-09-01 16:46:36 +02:00
|
|
|
|
|
|
|
content = prepareContent(noteMeta.title, content, noteMeta);
|
2018-11-25 22:09:52 +01:00
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
archive.append(content, { name: filePathPrefix + noteMeta.dataFileName });
|
2020-03-19 20:07:27 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
return;
|
2018-11-24 14:44:56 +01:00
|
|
|
}
|
|
|
|
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(noteMeta.noteId);
|
2018-11-25 15:17:28 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
if (noteMeta.dataFileName) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const content = prepareContent(noteMeta.title, note.getContent(), noteMeta);
|
2018-11-24 14:44:56 +01:00
|
|
|
|
2023-01-25 09:55:29 +01:00
|
|
|
archive.append(content, {
|
|
|
|
name: filePathPrefix + noteMeta.dataFileName,
|
|
|
|
date: dateUtils.parseDateTime(note.utcDateModified)
|
|
|
|
});
|
2018-11-24 14:44:56 +01:00
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-10-18 22:27:38 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-10 22:30:55 +01:00
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
for (const attachmentMeta of noteMeta.attachments) {
|
|
|
|
const attachment = note.getAttachmentById(attachmentMeta.attachmentId);
|
2023-03-16 12:17:55 +01:00
|
|
|
const content = attachment.getContent();
|
2023-03-08 09:01:23 +01:00
|
|
|
|
|
|
|
archive.append(content, {
|
2023-03-16 11:03:28 +01:00
|
|
|
name: filePathPrefix + attachmentMeta.dataFileName,
|
2023-03-08 09:01:23 +01:00
|
|
|
date: dateUtils.parseDateTime(note.utcDateModified)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
if (noteMeta.children?.length > 0) {
|
2019-08-31 10:12:42 +02:00
|
|
|
const directoryPath = filePathPrefix + noteMeta.dirFileName;
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
// create directory
|
2022-12-21 15:19:05 +01:00
|
|
|
archive.append('', { name: `${directoryPath}/`, date: dateUtils.parseDateTime(note.utcDateModified) });
|
2018-11-25 15:17:28 +01:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
for (const childMeta of noteMeta.children) {
|
2022-12-21 15:19:05 +01:00
|
|
|
saveNote(childMeta, `${directoryPath}/`);
|
2018-11-25 22:09:52 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {NoteMeta} rootMeta
|
|
|
|
* @param {NoteMeta} navigationMeta
|
|
|
|
*/
|
2020-06-20 12:31:38 +02:00
|
|
|
function saveNavigation(rootMeta, navigationMeta) {
|
2019-10-20 19:02:48 +02:00
|
|
|
function saveNavigationInner(meta) {
|
|
|
|
let html = '<li>';
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const escapedTitle = utils.escapeHtml(`${meta.prefix ? `${meta.prefix} - ` : ''}${meta.title}`);
|
2019-10-20 19:02:48 +02:00
|
|
|
|
|
|
|
if (meta.dataFileName) {
|
2023-05-06 22:50:28 +02:00
|
|
|
const targetUrl = getNoteTargetUrl(meta.noteId, rootMeta);
|
2019-10-20 19:02:48 +02:00
|
|
|
|
2019-10-21 21:00:27 +02:00
|
|
|
html += `<a href="${targetUrl}" target="detail">${escapedTitle}</a>`;
|
2019-10-20 19:02:48 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
html += escapedTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meta.children && meta.children.length > 0) {
|
|
|
|
html += '<ul>';
|
|
|
|
|
|
|
|
for (const child of meta.children) {
|
|
|
|
html += saveNavigationInner(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
html += '</ul>'
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
return `${html}</li>`;
|
2019-10-20 19:02:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 11:34:45 +01:00
|
|
|
const fullHtml = `<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<link rel="stylesheet" href="style.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<ul>${saveNavigationInner(rootMeta)}</ul>
|
|
|
|
</body>
|
|
|
|
</html>`;
|
2023-05-29 23:42:08 +02:00
|
|
|
const prettyHtml = fullHtml.length < 100_000
|
|
|
|
? html.prettyPrint(fullHtml, {indent_size: 2})
|
|
|
|
: fullHtml;
|
2019-10-20 19:02:48 +02:00
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
archive.append(prettyHtml, { name: navigationMeta.dataFileName });
|
2019-10-20 19:02:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {NoteMeta} rootMeta
|
|
|
|
* @param {NoteMeta} indexMeta
|
|
|
|
*/
|
2020-06-20 12:31:38 +02:00
|
|
|
function saveIndex(rootMeta, indexMeta) {
|
2019-10-21 21:00:27 +02:00
|
|
|
let firstNonEmptyNote;
|
|
|
|
let curMeta = rootMeta;
|
2019-10-20 19:02:48 +02:00
|
|
|
|
2019-10-21 21:00:27 +02:00
|
|
|
while (!firstNonEmptyNote) {
|
|
|
|
if (curMeta.dataFileName) {
|
2023-05-06 22:50:28 +02:00
|
|
|
firstNonEmptyNote = getNoteTargetUrl(curMeta.noteId, rootMeta);
|
2019-10-21 21:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (curMeta.children && curMeta.children.length > 0) {
|
|
|
|
curMeta = curMeta.children[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fullHtml = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
2020-10-05 22:23:03 +02:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
2019-10-21 21:00:27 +02:00
|
|
|
</head>
|
|
|
|
<frameset cols="25%,75%">
|
|
|
|
<frame name="navigation" src="navigation.html">
|
|
|
|
<frame name="detail" src="${firstNonEmptyNote}">
|
|
|
|
</frameset>
|
|
|
|
</html>`;
|
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
archive.append(fullHtml, { name: indexMeta.dataFileName });
|
2019-10-21 21:00:27 +02:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:38:45 +02:00
|
|
|
/**
|
|
|
|
* @param {NoteMeta} rootMeta
|
|
|
|
* @param {NoteMeta} cssMeta
|
|
|
|
*/
|
2020-06-20 12:31:38 +02:00
|
|
|
function saveCss(rootMeta, cssMeta) {
|
2022-12-21 15:19:05 +01:00
|
|
|
const cssContent = fs.readFileSync(`${RESOURCE_DIR}/libraries/ckeditor/ckeditor-content.css`);
|
2019-12-25 11:34:45 +01:00
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
archive.append(cssContent, { name: cssMeta.dataFileName });
|
2019-12-25 11:34:45 +01:00
|
|
|
}
|
|
|
|
|
2019-10-21 21:00:27 +02:00
|
|
|
const existingFileNames = format === 'html' ? ['navigation', 'index'] : [];
|
2023-05-06 22:50:28 +02:00
|
|
|
const rootMeta = createNoteMeta(branch, { notePath: [] }, existingFileNames);
|
2019-10-20 19:02:48 +02:00
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
const metaFile = {
|
2023-05-03 18:49:46 +03:00
|
|
|
formatVersion: 2,
|
2018-11-27 10:31:55 +01:00
|
|
|
appVersion: packageInfo.version,
|
2019-10-21 21:00:27 +02:00
|
|
|
files: [ rootMeta ]
|
2018-11-25 22:09:52 +01:00
|
|
|
};
|
|
|
|
|
2019-12-25 11:34:45 +01:00
|
|
|
let navigationMeta, indexMeta, cssMeta;
|
2019-10-21 21:00:27 +02:00
|
|
|
|
|
|
|
if (format === 'html') {
|
|
|
|
navigationMeta = {
|
|
|
|
noImport: true,
|
|
|
|
dataFileName: "navigation.html"
|
|
|
|
};
|
|
|
|
|
|
|
|
metaFile.files.push(navigationMeta);
|
|
|
|
|
|
|
|
indexMeta = {
|
|
|
|
noImport: true,
|
|
|
|
dataFileName: "index.html"
|
|
|
|
};
|
|
|
|
|
|
|
|
metaFile.files.push(indexMeta);
|
2019-12-25 11:34:45 +01:00
|
|
|
|
|
|
|
cssMeta = {
|
|
|
|
noImport: true,
|
|
|
|
dataFileName: "style.css"
|
|
|
|
};
|
|
|
|
|
|
|
|
metaFile.files.push(cssMeta);
|
2019-10-21 21:00:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-25 22:38:09 +01:00
|
|
|
for (const noteMeta of Object.values(noteIdToMeta)) {
|
2019-08-19 20:12:00 +02:00
|
|
|
// filter out relations which are not inside this export
|
2022-12-23 23:08:30 +01:00
|
|
|
noteMeta.attributes = noteMeta.attributes.filter(attr => {
|
|
|
|
if (attr.type !== 'relation') {
|
|
|
|
return true;
|
|
|
|
} else if (attr.value in noteIdToMeta) {
|
|
|
|
return true;
|
|
|
|
} else if (attr.value === 'root' || attr.value?.startsWith("_")) {
|
|
|
|
// relations to "named" noteIds can be preserved
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2018-11-25 22:38:09 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 19:02:48 +02:00
|
|
|
if (!rootMeta) { // corner case of disabled export for exported note
|
2018-11-25 22:09:52 +01:00
|
|
|
res.sendStatus(400);
|
|
|
|
return;
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-25 22:09:52 +01:00
|
|
|
const metaFileJson = JSON.stringify(metaFile, null, '\t');
|
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
archive.append(metaFileJson, { name: "!!!meta.json" });
|
2018-11-25 22:09:52 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
saveNote(rootMeta, '');
|
2019-10-20 19:02:48 +02:00
|
|
|
|
2019-10-21 21:00:27 +02:00
|
|
|
if (format === 'html') {
|
2020-06-20 12:31:38 +02:00
|
|
|
saveNavigation(rootMeta, navigationMeta);
|
|
|
|
saveIndex(rootMeta, indexMeta);
|
|
|
|
saveCss(rootMeta, cssMeta);
|
2019-10-21 21:00:27 +02:00
|
|
|
}
|
2018-11-25 22:09:52 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = branch.getNote();
|
2022-12-21 15:19:05 +01:00
|
|
|
const zipFileName = `${branch.prefix ? `${branch.prefix} - ` : ""}${note.getTitleOrProtected()}.zip`;
|
2018-11-25 22:09:52 +01:00
|
|
|
|
2022-12-29 21:15:34 +01:00
|
|
|
if (setHeaders) {
|
|
|
|
res.setHeader('Content-Disposition', utils.getContentDisposition(zipFileName));
|
|
|
|
res.setHeader('Content-Type', 'application/zip');
|
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2021-05-14 22:57:44 +02:00
|
|
|
archive.pipe(res);
|
2022-12-29 23:12:38 +01:00
|
|
|
await archive.finalize();
|
2019-02-10 22:30:55 +01:00
|
|
|
|
2019-10-18 22:27:38 +02:00
|
|
|
taskContext.taskSucceeded();
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2022-12-29 23:12:38 +01:00
|
|
|
async function exportToZipFile(noteId, format, zipFilePath) {
|
2022-12-29 21:15:34 +01:00
|
|
|
const fileOutputStream = fs.createWriteStream(zipFilePath);
|
|
|
|
const taskContext = new TaskContext('no-progress-reporting');
|
|
|
|
|
|
|
|
const note = becca.getNote(noteId);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
throw new ValidationError(`Note ${noteId} not found.`);
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:12:38 +01:00
|
|
|
await exportToZip(taskContext, note.getParentBranches()[0], format, fileOutputStream, false);
|
|
|
|
|
|
|
|
log.info(`Exported '${noteId}' with format '${format}' to '${zipFilePath}'`);
|
2022-12-29 21:15:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 12:12:04 +01:00
|
|
|
module.exports = {
|
2022-12-29 21:15:34 +01:00
|
|
|
exportToZip,
|
|
|
|
exportToZipFile
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|