mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 19:05:59 +01:00
import/export logic refactored into separate files per format, closes #237
This commit is contained in:
67
src/services/export/opml.js
Normal file
67
src/services/export/opml.js
Normal file
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
|
||||
const sanitize = require("sanitize-filename");
|
||||
const repository = require("../../services/repository");
|
||||
const utils = require('../../services/utils');
|
||||
|
||||
async function exportToOpml(branch, res) {
|
||||
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>`);
|
||||
|
||||
await exportNoteInner(branch.branchId);
|
||||
|
||||
res.write(`</body>
|
||||
</opml>`);
|
||||
res.end();
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
Reference in New Issue
Block a user