2018-11-16 12:12:04 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const noteService = require('../../services/notes');
|
|
|
|
|
const parseString = require('xml2js').parseString;
|
2019-02-25 21:22:57 +01:00
|
|
|
const protectedSessionService = require('../protected_session');
|
2020-06-30 23:37:06 +02:00
|
|
|
const htmlSanitizer = require('../html_sanitizer');
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-02-10 19:36:03 +01:00
|
|
|
/**
|
2019-10-17 21:11:35 +02:00
|
|
|
* @param {TaskContext} taskContext
|
2019-02-10 19:36:03 +01:00
|
|
|
* @param {Buffer} fileBuffer
|
|
|
|
|
* @param {Note} parentNote
|
|
|
|
|
* @return {Promise<*[]|*>}
|
|
|
|
|
*/
|
2020-10-26 19:02:33 +01:00
|
|
|
async function importOpml(taskContext, fileBuffer, parentNote) {
|
|
|
|
|
const xml = await new Promise(function(resolve, reject)
|
2018-11-16 12:12:04 +01:00
|
|
|
{
|
2018-11-16 14:36:50 +01:00
|
|
|
parseString(fileBuffer, function (err, result) {
|
2018-11-16 12:12:04 +01:00
|
|
|
if (err) {
|
|
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
resolve(result);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-16 22:13:29 +01:00
|
|
|
if (!['1.0', '1.1', '2.0'].includes(xml.opml.$.version)) {
|
|
|
|
|
return [400, 'Unsupported OPML version ' + xml.opml.$.version + ', 1.0, 1.1 or 2.0 expected instead.'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const opmlVersion = parseInt(xml.opml.$.version);
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function importOutline(outline, parentNoteId) {
|
2019-02-16 22:13:29 +01:00
|
|
|
let title, content;
|
|
|
|
|
|
|
|
|
|
if (opmlVersion === 1) {
|
|
|
|
|
title = outline.$.title;
|
|
|
|
|
content = toHtml(outline.$.text);
|
2021-04-19 22:33:58 +02:00
|
|
|
|
|
|
|
|
if (!title || !title.trim()) {
|
|
|
|
|
// https://github.com/zadam/trilium/issues/1862
|
|
|
|
|
title = outline.$.text;
|
|
|
|
|
content = '';
|
|
|
|
|
}
|
2019-02-16 22:13:29 +01:00
|
|
|
}
|
|
|
|
|
else if (opmlVersion === 2) {
|
|
|
|
|
title = outline.$.text;
|
|
|
|
|
content = outline.$._note; // _note is already HTML
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
throw new Error("Unrecognized OPML version " + opmlVersion);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 23:37:06 +02:00
|
|
|
content = htmlSanitizer.sanitize(content);
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const {note} = noteService.createNewNote({
|
2019-11-16 11:09:52 +01:00
|
|
|
parentNoteId,
|
|
|
|
|
title,
|
|
|
|
|
content,
|
2019-11-16 18:04:13 +01:00
|
|
|
type: 'text',
|
2019-11-16 11:09:52 +01:00
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
|
2019-02-25 21:22:57 +01:00
|
|
|
});
|
2019-02-16 22:13:29 +01:00
|
|
|
|
2019-10-17 21:11:35 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-16 22:13:29 +01:00
|
|
|
|
|
|
|
|
for (const childOutline of (outline.outline || [])) {
|
2020-06-20 12:31:38 +02:00
|
|
|
importOutline(childOutline, note.noteId);
|
2019-02-16 22:13:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return note;
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const outlines = xml.opml.body[0].outline || [];
|
|
|
|
|
let returnNote = null;
|
|
|
|
|
|
|
|
|
|
for (const outline of outlines) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = importOutline(outline, parentNote.noteId);
|
2018-11-16 12:12:04 +01:00
|
|
|
|
|
|
|
|
// first created note will be activated after import
|
|
|
|
|
returnNote = returnNote || note;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnNote;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHtml(text) {
|
|
|
|
|
if (!text) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '<p>' + text.replace(/(?:\r\n|\r|\n)/g, '</p><p>') + '</p>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
importOpml
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|