2018-11-16 12:12:04 +01:00
|
|
|
"use strict";
|
|
|
|
|
2019-01-13 10:22:17 +01:00
|
|
|
const utils = require('../utils');
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require("../../becca/becca");
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function exportToOpml(taskContext, branch, version, res) {
|
2019-02-16 23:58:42 +01:00
|
|
|
if (!['1.0', '2.0'].includes(version)) {
|
2022-12-21 15:19:05 +01:00
|
|
|
throw new Error(`Unrecognized OPML version ${version}`);
|
2019-02-16 23:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const opmlVersion = parseInt(version);
|
2019-02-16 23:33:40 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = branch.getNote();
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function exportNoteInner(branchId) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const branch = becca.getBranch(branchId);
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = branch.getNote();
|
2018-11-19 09:34:05 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (note.hasOwnedLabel('excludeFromExport')) {
|
2018-11-19 09:34:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const title = `${branch.prefix ? (`${branch.prefix} - `) : ''}${note.title}`;
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-02-16 23:58:42 +01:00
|
|
|
if (opmlVersion === 1) {
|
|
|
|
const preparedTitle = escapeXmlAttribute(title);
|
2020-06-20 12:31:38 +02:00
|
|
|
const preparedContent = note.isStringNote() ? prepareText(note.getContent()) : '';
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-02-16 23:58:42 +01:00
|
|
|
res.write(`<outline title="${preparedTitle}" text="${preparedContent}">\n`);
|
|
|
|
}
|
|
|
|
else if (opmlVersion === 2) {
|
|
|
|
const preparedTitle = escapeXmlAttribute(title);
|
2020-06-20 12:31:38 +02:00
|
|
|
const preparedContent = note.isStringNote() ? escapeXmlAttribute(note.getContent()) : '';
|
2019-02-16 23:58:42 +01:00
|
|
|
|
|
|
|
res.write(`<outline text="${preparedTitle}" _note="${preparedContent}">\n`);
|
|
|
|
}
|
|
|
|
else {
|
2022-12-21 15:19:05 +01:00
|
|
|
throw new Error(`Unrecognized OPML version ${opmlVersion}`);
|
2019-02-16 23:58:42 +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
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
for (const child of note.getChildBranches()) {
|
|
|
|
exportNoteInner(child.branchId);
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.write('</outline>');
|
|
|
|
}
|
|
|
|
|
2019-02-16 23:58:42 +01:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const filename = `${branch.prefix ? (`${branch.prefix} - `) : ''}${note.title}.opml`;
|
2019-01-13 10:22:17 +01:00
|
|
|
|
|
|
|
res.setHeader('Content-Disposition', utils.getContentDisposition(filename));
|
2018-11-16 12:12:04 +01:00
|
|
|
res.setHeader('Content-Type', 'text/x-opml');
|
|
|
|
|
|
|
|
res.write(`<?xml version="1.0" encoding="UTF-8"?>
|
2019-03-03 19:43:30 +01:00
|
|
|
<opml version="${version}">
|
2018-11-16 12:12:04 +01:00
|
|
|
<head>
|
|
|
|
<title>Trilium export</title>
|
|
|
|
</head>
|
|
|
|
<body>`);
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
exportNoteInner(branch.branchId);
|
2018-11-16 12:12:04 +01:00
|
|
|
|
|
|
|
res.write(`</body>
|
|
|
|
</opml>`);
|
|
|
|
res.end();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function escapeXmlAttribute(text) {
|
|
|
|
return text.replace(/&/g, '&')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
exportToOpml
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|