trilium/src/services/export/tar.js

314 lines
9.8 KiB
JavaScript
Raw Normal View History

"use strict";
const html = require('html');
const repository = require('../repository');
2018-11-25 03:58:38 +08:00
const tar = require('tar-stream');
2018-12-01 05:43:03 +08:00
const path = require('path');
2018-11-24 21:44:56 +08:00
const mimeTypes = require('mime-types');
const TurndownService = require('turndown');
const packageInfo = require('../../../package.json');
const utils = require('../utils');
2019-08-31 16:12:42 +08:00
const log = require('../log');
const sanitize = require("sanitize-filename");
2018-11-24 21:44:56 +08:00
/**
2019-02-11 05:30:55 +08:00
* @param {ExportContext} exportContext
* @param {Branch} branch
* @param {string} format - 'html' or 'markdown'
2018-11-24 21:44:56 +08:00
*/
2019-02-11 05:30:55 +08:00
async function exportToTar(exportContext, branch, format, res) {
let turndownService = format === 'markdown' ? new TurndownService() : null;
2018-11-25 17:26:45 +08:00
2018-11-25 03:58:38 +08:00
const pack = tar.pack();
const noteIdToMeta = {};
function getUniqueFilename(existingFileNames, fileName) {
const lcFileName = fileName.toLowerCase();
2018-11-25 17:26:45 +08:00
if (lcFileName in existingFileNames) {
let index;
let newName;
do {
index = existingFileNames[lcFileName]++;
2019-07-11 05:01:30 +08:00
newName = index + "_" + lcFileName;
}
while (newName in existingFileNames);
2019-07-11 05:01:30 +08:00
return index + "_" + fileName;
}
else {
existingFileNames[lcFileName] = 1;
return fileName;
}
2018-11-25 17:26:45 +08:00
}
function getDataFileName(note, baseFileName, existingFileNames) {
const existingExtension = path.extname(baseFileName).toLowerCase();
let newExtension;
// following two are handled specifically since we always want to have these extensions no matter the automatic detection
// and/or existing detected extensions in the note name
if (note.type === 'text' && format === 'markdown') {
newExtension = 'md';
}
else if (note.type === 'text' && format === 'html') {
newExtension = 'html';
}
2018-12-01 05:43:03 +08:00
else if (note.mime === 'application/x-javascript' || note.mime === 'text/javascript') {
newExtension = 'js';
}
else if (existingExtension.length > 0) { // if the page already has an extension, then we'll just keep it
newExtension = null;
}
else {
newExtension = mimeTypes.extension(note.mime) || "dat";
}
let fileName = baseFileName;
2018-12-01 05:43:03 +08:00
// if the note is already named with extension (e.g. "jquery.js"), then it's silly to append exact same extension again
if (newExtension && existingExtension !== "." + newExtension.toLowerCase()) {
fileName += "." + newExtension;
}
return getUniqueFilename(existingFileNames, fileName);
}
2019-08-31 16:12:42 +08:00
async function getNoteMeta(branch, parentMeta, existingFileNames) {
const note = await branch.getNote();
if (await note.hasLabel('excludeFromExport')) {
return;
}
const baseFileName = sanitize(branch.prefix ? (branch.prefix + ' - ' + note.title) : note.title);
if (note.noteId in noteIdToMeta) {
const fileName = getUniqueFilename(existingFileNames, baseFileName + ".clone");
return {
2018-11-26 21:47:46 +08:00
isClone: true,
noteId: note.noteId,
prefix: branch.prefix,
dataFileName: fileName
};
}
const meta = {
2018-11-26 21:47:46 +08:00
isClone: false,
noteId: note.noteId,
2019-08-31 16:12:42 +08:00
notePath: parentMeta.notePath.concat([note.noteId]),
title: note.title,
notePosition: branch.notePosition,
prefix: branch.prefix,
isExpanded: branch.isExpanded,
type: note.type,
mime: note.mime,
2019-03-13 03:58:31 +08:00
// we don't export utcDateCreated and utcDateModified of any entity since that would be a bit misleading
attributes: (await note.getOwnedAttributes()).map(attribute => {
return {
type: attribute.type,
name: attribute.name,
value: attribute.value,
isInheritable: attribute.isInheritable,
position: attribute.position
};
})
};
2019-02-11 05:30:55 +08:00
exportContext.increaseProgressCount();
2018-11-24 21:44:56 +08:00
if (note.type === 'text') {
meta.format = format;
2018-11-24 21:44:56 +08:00
}
noteIdToMeta[note.noteId] = meta;
const childBranches = await note.getChildBranches();
// if it's a leaf then we'll export it even if it's empty
2019-02-07 04:29:23 +08:00
if ((await note.getContent()).length > 0 || childBranches.length === 0) {
meta.dataFileName = getDataFileName(note, baseFileName, existingFileNames);
}
if (childBranches.length > 0) {
meta.dirFileName = getUniqueFilename(existingFileNames, baseFileName);
meta.children = [];
2018-11-27 05:22:16 +08:00
// namespace is shared by children in the same note
const childExistingNames = {};
for (const childBranch of childBranches) {
2019-08-31 16:12:42 +08:00
const note = await getNoteMeta(childBranch, meta, childExistingNames);
2018-11-24 21:44:56 +08:00
// can be undefined if export is disabled for this note
if (note) {
meta.children.push(note);
}
2018-11-24 21:44:56 +08:00
}
}
return meta;
}
function getTargetUrl(targetNoteId, sourceMeta) {
const targetMeta = noteIdToMeta[targetNoteId];
2019-02-07 04:29:23 +08:00
if (!targetMeta) {
return null;
}
2019-08-31 16:12:42 +08:00
const targetPath = targetMeta.notePath.slice();
const sourcePath = sourceMeta.notePath.slice();
2019-08-31 16:12:42 +08:00
// > 1 for edge case that targetPath and sourcePath are exact same (link to itself)
while (targetPath.length > 1 && sourcePath.length > 1 && targetPath[0] === sourcePath[0]) {
targetPath.shift();
sourcePath.shift();
}
2019-08-31 16:12:42 +08:00
let url = "../".repeat(sourcePath.length - 1);
2019-08-31 16:12:42 +08:00
for (let i = 0; i < targetPath.length - 1; i++) {
const meta = noteIdToMeta[targetPath[i]];
2019-08-31 16:12:42 +08:00
url += meta.dirFileName + '/';
}
2019-08-31 16:12:42 +08:00
const meta = noteIdToMeta[targetPath[targetPath.length - 1]];
2019-08-31 16:12:42 +08:00
url += meta.dataFileName;
return url;
}
2019-08-31 16:12:42 +08:00
function findLinks(content, noteMeta) {
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9]+)\/[^"]*"/g, (_, targetNoteId) => {
const url = getTargetUrl(targetNoteId, noteMeta);
2019-08-31 16:12:42 +08:00
return `src="${url}"`;
});
2019-08-31 16:12:42 +08:00
content = content.replace(/href="[^"]*#root[a-zA-Z0-9\/]*\/([a-zA-Z0-9]+)\/?"/g, (_, targetNoteId) => {
const url = getTargetUrl(targetNoteId, noteMeta);
2019-08-31 16:12:42 +08:00
return `href="${url}"`;
});
return content;
2019-08-31 16:12:42 +08:00
}
async function prepareContent(note, noteMeta) {
let content = await note.getContent();
if (['html', 'markdown'].includes(noteMeta.format)) {
content = content.toString();
content = findLinks(content, noteMeta);
2019-08-31 16:12:42 +08:00
}
if (noteMeta.format === 'html') {
if (!content.substr(0, 100).toLowerCase().includes("<html")) {
content = `<html>
<head><meta charset="utf-8"></head>
<body>
<h1>${utils.escapeHtml(note.title)}</h1>
${content}
</body>
</html>`;
}
2019-02-07 04:29:23 +08:00
return html.prettyPrint(content, {indent_size: 2});
}
2019-08-31 16:12:42 +08:00
else if (noteMeta.format === 'markdown') {
let markdownContent = turndownService.turndown(content);
if (markdownContent.trim().length > 0 && !markdownContent.startsWith("# ")) {
markdownContent = '# ' + note.title + "\r\n" + markdownContent;
}
return markdownContent;
}
else {
2019-02-07 04:29:23 +08:00
return content;
2018-11-25 17:26:45 +08:00
}
}
// noteId => file path
const notePaths = {};
2019-08-31 16:12:42 +08:00
async function saveNote(noteMeta, filePathPrefix) {
2018-11-26 21:47:46 +08:00
if (noteMeta.isClone) {
const content = "Note is present at " + notePaths[noteMeta.noteId];
2019-08-31 16:12:42 +08:00
pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content);
return;
2018-11-24 21:44:56 +08:00
}
const note = await repository.getNote(noteMeta.noteId);
2019-08-31 16:12:42 +08:00
notePaths[note.noteId] = filePathPrefix + (noteMeta.dataFileName || noteMeta.dirFileName);
if (noteMeta.dataFileName) {
2019-08-31 16:12:42 +08:00
const content = await prepareContent(note, noteMeta);
2018-11-24 21:44:56 +08:00
2019-08-31 16:12:42 +08:00
pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content);
2018-11-24 21:44:56 +08:00
}
2019-02-11 05:30:55 +08:00
exportContext.increaseProgressCount();
if (noteMeta.children && noteMeta.children.length > 0) {
2019-08-31 16:12:42 +08:00
const directoryPath = filePathPrefix + noteMeta.dirFileName;
pack.entry({name: directoryPath, type: 'directory'});
for (const childMeta of noteMeta.children) {
2018-11-27 05:22:16 +08:00
await saveNote(childMeta, directoryPath + '/');
}
}
}
const metaFile = {
formatVersion: 1,
appVersion: packageInfo.version,
files: [
2019-08-31 16:12:42 +08:00
await getNoteMeta(branch, { notePath: [] }, [])
]
};
for (const noteMeta of Object.values(noteIdToMeta)) {
2019-08-20 02:12:00 +08:00
// filter out relations which are not inside this export
noteMeta.attributes = noteMeta.attributes.filter(attr => attr.type !== 'relation' || attr.value in noteIdToMeta);
}
if (!metaFile.files[0]) { // corner case of disabled export for exported note
res.sendStatus(400);
return;
}
const metaFileJson = JSON.stringify(metaFile, null, '\t');
pack.entry({name: "!!!meta.json", size: metaFileJson.length}, metaFileJson);
await saveNote(metaFile.files[0], '');
pack.finalize();
const note = await branch.getNote();
const tarFileName = (branch.prefix ? (branch.prefix + " - ") : "") + note.title + ".tar";
res.setHeader('Content-Disposition', utils.getContentDisposition(tarFileName));
res.setHeader('Content-Type', 'application/tar');
pack.pipe(res);
2019-02-11 05:30:55 +08:00
exportContext.exportFinished();
}
module.exports = {
exportToTar
};