2017-12-03 10:48:22 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-12-03 22:19:48 +08:00
|
|
|
const html = require('html');
|
2018-02-25 23:55:21 +08:00
|
|
|
const tar = require('tar-stream');
|
|
|
|
const sanitize = require("sanitize-filename");
|
2018-03-31 21:07:58 +08:00
|
|
|
const repository = require("../../services/repository");
|
2018-05-28 00:26:34 +08:00
|
|
|
const utils = require('../../services/utils');
|
2018-09-03 05:39:10 +08:00
|
|
|
const TurndownService = require('turndown');
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2018-03-31 03:34:07 +08:00
|
|
|
async function exportNote(req, res) {
|
2018-09-03 15:40:22 +08:00
|
|
|
// entityId maybe either noteId or branchId depending on format
|
|
|
|
const entityId = req.params.entityId;
|
2018-05-28 00:26:34 +08:00
|
|
|
const format = req.params.format;
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2018-05-28 00:26:34 +08:00
|
|
|
if (format === 'tar') {
|
2018-09-03 15:40:22 +08:00
|
|
|
await exportToTar(await repository.getBranch(entityId), res);
|
2018-05-28 00:26:34 +08:00
|
|
|
}
|
|
|
|
else if (format === 'opml') {
|
2018-09-03 15:40:22 +08:00
|
|
|
await exportToOpml(await repository.getBranch(entityId), res);
|
2018-05-28 00:26:34 +08:00
|
|
|
}
|
2018-09-03 05:39:10 +08:00
|
|
|
else if (format === 'markdown') {
|
2018-09-03 15:40:22 +08:00
|
|
|
await exportToMarkdown(await repository.getBranch(entityId), res);
|
|
|
|
}
|
|
|
|
// export single note without subtree
|
|
|
|
else if (format === 'markdown-single') {
|
|
|
|
await exportSingleMarkdown(await repository.getNote(entityId), res);
|
2018-09-03 05:39:10 +08:00
|
|
|
}
|
2018-05-28 00:26:34 +08:00
|
|
|
else {
|
|
|
|
return [404, "Unrecognized export format " + format];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeXmlAttribute(text) {
|
|
|
|
return text.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareText(text) {
|
|
|
|
const newLines = text.replace(/(<p[^>]*>|<br\s*\/?>)/g, '\n')
|
|
|
|
.replace(/ /g, ' '); // nbsp isn't in XML standard (only HTML)
|
|
|
|
|
|
|
|
const stripped = utils.stripTags(newLines);
|
|
|
|
|
|
|
|
const escaped = escapeXmlAttribute(stripped);
|
|
|
|
|
|
|
|
return escaped.replace(/\n/g, ' ');
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
async function exportToOpml(branch, res) {
|
2018-05-28 00:26:34 +08:00
|
|
|
const note = await branch.getNote();
|
|
|
|
const title = (branch.prefix ? (branch.prefix + ' - ') : '') + note.title;
|
|
|
|
const sanitizedTitle = sanitize(title);
|
|
|
|
|
|
|
|
async function exportNoteInner(branchId) {
|
|
|
|
const branch = await repository.getBranch(branchId);
|
|
|
|
const note = await branch.getNote();
|
|
|
|
const title = (branch.prefix ? (branch.prefix + ' - ') : '') + note.title;
|
|
|
|
|
|
|
|
const preparedTitle = prepareText(title);
|
|
|
|
const preparedContent = prepareText(note.content);
|
|
|
|
|
|
|
|
res.write(`<outline title="${preparedTitle}" text="${preparedContent}">\n`);
|
|
|
|
|
|
|
|
for (const child of await note.getChildBranches()) {
|
|
|
|
await exportNoteInner(child.branchId);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.write('</outline>');
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Content-Disposition', 'file; filename="' + sanitizedTitle + '.opml"');
|
|
|
|
res.setHeader('Content-Type', 'text/x-opml');
|
|
|
|
|
|
|
|
res.write(`<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<opml version="1.0">
|
|
|
|
<head>
|
|
|
|
<title>Trilium export</title>
|
|
|
|
</head>
|
|
|
|
<body>`);
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
await exportNoteInner(branch.branchId);
|
2018-05-28 00:26:34 +08:00
|
|
|
|
|
|
|
res.write(`</body>
|
|
|
|
</opml>`);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
async function exportToTar(branch, res) {
|
2018-02-25 23:55:21 +08:00
|
|
|
const pack = tar.pack();
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
const exportedNoteIds = [];
|
2018-09-03 18:05:44 +08:00
|
|
|
const name = await exportNoteInner(branch, '');
|
2018-04-09 01:14:30 +08:00
|
|
|
|
2018-09-03 18:05:44 +08:00
|
|
|
async function exportNoteInner(branch, directory) {
|
2018-04-09 01:14:30 +08:00
|
|
|
const note = await branch.getNote();
|
|
|
|
const childFileName = directory + sanitize(note.title);
|
|
|
|
|
|
|
|
if (exportedNoteIds.includes(note.noteId)) {
|
|
|
|
saveMetadataFile(childFileName, {
|
|
|
|
version: 1,
|
|
|
|
clone: true,
|
|
|
|
noteId: note.noteId,
|
|
|
|
prefix: branch.prefix
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const metadata = {
|
|
|
|
version: 1,
|
|
|
|
clone: false,
|
|
|
|
noteId: note.noteId,
|
|
|
|
title: note.title,
|
|
|
|
prefix: branch.prefix,
|
|
|
|
type: note.type,
|
|
|
|
mime: note.mime,
|
2018-08-07 19:33:10 +08:00
|
|
|
attributes: (await note.getOwnedAttributes()).map(attribute => {
|
2018-04-09 01:14:30 +08:00
|
|
|
return {
|
2018-08-07 19:33:10 +08:00
|
|
|
type: attribute.type,
|
|
|
|
name: attribute.name,
|
|
|
|
value: attribute.value,
|
|
|
|
isInheritable: attribute.isInheritable,
|
|
|
|
position: attribute.position
|
2018-04-09 01:14:30 +08:00
|
|
|
};
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-09-03 05:39:10 +08:00
|
|
|
if (await note.hasLabel('excludeFromExport')) {
|
2018-04-09 01:14:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveMetadataFile(childFileName, metadata);
|
|
|
|
saveDataFile(childFileName, note);
|
|
|
|
|
|
|
|
exportedNoteIds.push(note.noteId);
|
|
|
|
|
2018-09-03 18:05:44 +08:00
|
|
|
const childBranches = await note.getChildBranches();
|
|
|
|
|
|
|
|
if (childBranches.length > 0) {
|
|
|
|
saveDirectory(childFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const childBranch of childBranches) {
|
|
|
|
await exportNoteInner(childBranch, childFileName + "/");
|
2018-04-09 01:14:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return childFileName;
|
2018-02-26 13:07:43 +08:00
|
|
|
}
|
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
function saveDataFile(childFileName, note) {
|
|
|
|
const content = note.type === 'text' ? html.prettyPrint(note.content, {indent_size: 2}) : note.content;
|
2018-02-27 09:47:34 +08:00
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
pack.entry({name: childFileName + ".dat", size: content.length}, content);
|
2018-02-27 09:47:34 +08:00
|
|
|
}
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
function saveMetadataFile(childFileName, metadata) {
|
|
|
|
const metadataJson = JSON.stringify(metadata, null, '\t');
|
2017-12-03 10:48:22 +08:00
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
pack.entry({name: childFileName + ".meta", size: metadataJson.length}, metadataJson);
|
2017-12-03 10:48:22 +08:00
|
|
|
}
|
2018-02-25 23:55:21 +08:00
|
|
|
|
2018-09-03 18:05:44 +08:00
|
|
|
function saveDirectory(childFileName) {
|
|
|
|
pack.entry({name: childFileName, type: 'directory'});
|
|
|
|
}
|
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
pack.finalize();
|
2018-02-25 23:55:21 +08:00
|
|
|
|
2018-04-09 01:14:30 +08:00
|
|
|
res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"');
|
|
|
|
res.setHeader('Content-Type', 'application/tar');
|
|
|
|
|
|
|
|
pack.pipe(res);
|
2017-12-03 10:48:22 +08:00
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
async function exportToMarkdown(branch, res) {
|
|
|
|
const note = await branch.getNote();
|
|
|
|
|
|
|
|
if (!await note.hasChildren()) {
|
|
|
|
await exportSingleMarkdown(note, res);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-03 05:39:10 +08:00
|
|
|
const turndownService = new TurndownService();
|
|
|
|
const pack = tar.pack();
|
2018-09-03 15:40:22 +08:00
|
|
|
const name = await exportNoteInner(note, '');
|
2018-09-03 05:39:10 +08:00
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
async function exportNoteInner(note, directory) {
|
2018-09-03 05:39:10 +08:00
|
|
|
const childFileName = directory + sanitize(note.title);
|
|
|
|
|
|
|
|
if (await note.hasLabel('excludeFromExport')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveDataFile(childFileName, note);
|
|
|
|
|
2018-09-03 18:05:44 +08:00
|
|
|
const childNotes = await note.getChildNotes();
|
|
|
|
|
|
|
|
if (childNotes.length > 0) {
|
|
|
|
saveDirectory(childFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const childNote of childNotes) {
|
2018-09-03 15:40:22 +08:00
|
|
|
await exportNoteInner(childNote, childFileName + "/");
|
2018-09-03 05:39:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return childFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveDataFile(childFileName, note) {
|
|
|
|
if (note.type !== 'text' && note.type !== 'code') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.content.trim().length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let markdown;
|
|
|
|
|
|
|
|
if (note.type === 'code') {
|
|
|
|
markdown = '```\n' + note.content + "\n```";
|
|
|
|
}
|
|
|
|
else if (note.type === 'text') {
|
|
|
|
markdown = turndownService.turndown(note.content);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// other note types are not supported
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pack.entry({name: childFileName + ".md", size: markdown.length}, markdown);
|
|
|
|
}
|
|
|
|
|
2018-09-03 18:05:44 +08:00
|
|
|
function saveDirectory(childFileName) {
|
|
|
|
pack.entry({name: childFileName, type: 'directory'});
|
|
|
|
}
|
|
|
|
|
2018-09-03 05:39:10 +08:00
|
|
|
pack.finalize();
|
|
|
|
|
|
|
|
res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"');
|
|
|
|
res.setHeader('Content-Type', 'application/tar');
|
|
|
|
|
|
|
|
pack.pipe(res);
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:40:22 +08:00
|
|
|
async function exportSingleMarkdown(note, res) {
|
|
|
|
const turndownService = new TurndownService();
|
|
|
|
const markdown = turndownService.turndown(note.content);
|
|
|
|
const name = sanitize(note.title);
|
|
|
|
|
|
|
|
res.setHeader('Content-Disposition', 'file; filename="' + name + '.md"');
|
|
|
|
res.setHeader('Content-Type', 'text/markdown; charset=UTF-8');
|
|
|
|
|
|
|
|
res.send(markdown);
|
|
|
|
}
|
|
|
|
|
2018-03-31 03:34:07 +08:00
|
|
|
module.exports = {
|
|
|
|
exportNote
|
|
|
|
};
|