2018-11-16 12:12:04 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
2024-02-25 08:07:17 +02:00
|
|
|
import noteService = require('../../services/notes');
|
2024-07-18 21:28:51 +03:00
|
|
|
import xml2js = require('xml2js');
|
2024-02-25 08:07:17 +02:00
|
|
|
import protectedSessionService = require('../protected_session');
|
|
|
|
|
import htmlSanitizer = require('../html_sanitizer');
|
|
|
|
|
import TaskContext = require('../task_context');
|
|
|
|
|
import BNote = require('../../becca/entities/bnote');
|
|
|
|
|
const parseString = xml2js.parseString;
|
|
|
|
|
|
|
|
|
|
interface OpmlXml {
|
|
|
|
|
opml: OpmlBody;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface OpmlBody {
|
|
|
|
|
$: {
|
|
|
|
|
version: string
|
|
|
|
|
}
|
|
|
|
|
body: OpmlOutline[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface OpmlOutline {
|
|
|
|
|
$: {
|
|
|
|
|
title: string;
|
|
|
|
|
text: string;
|
|
|
|
|
_note: string;
|
|
|
|
|
};
|
|
|
|
|
outline: OpmlOutline[];
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 14:29:08 +03:00
|
|
|
async function importOpml(taskContext: TaskContext, fileBuffer: string | Buffer, parentNote: BNote) {
|
2024-02-25 08:07:17 +02:00
|
|
|
const xml = await new Promise<OpmlXml>(function(resolve, reject)
|
2018-11-16 12:12:04 +01:00
|
|
|
{
|
2024-02-25 08:07:17 +02:00
|
|
|
parseString(fileBuffer, function (err: any, result: OpmlXml) {
|
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)) {
|
2022-12-21 15:19:05 +01:00
|
|
|
return [400, `Unsupported OPML version ${xml.opml.$.version}, 1.0, 1.1 or 2.0 expected instead.`];
|
2019-02-16 22:13:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const opmlVersion = parseInt(xml.opml.$.version);
|
|
|
|
|
|
2024-02-25 08:07:17 +02:00
|
|
|
function importOutline(outline: OpmlOutline, parentNoteId: string) {
|
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 {
|
2022-12-21 15:19:05 +01:00
|
|
|
throw new Error(`Unrecognized OPML version ${opmlVersion}`);
|
2019-02-16 22:13:29 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-30 21:06:45 +01:00
|
|
|
content = htmlSanitizer.sanitize(content || "");
|
2020-06-30 23:37:06 +02:00
|
|
|
|
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
|
|
|
|
2023-06-30 11:18:34 +02:00
|
|
|
// the first created note will be activated after import
|
2018-11-16 12:12:04 +01:00
|
|
|
returnNote = returnNote || note;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return returnNote;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 08:07:17 +02:00
|
|
|
function toHtml(text: string) {
|
2018-11-16 12:12:04 +01:00
|
|
|
if (!text) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
return `<p>${text.replace(/(?:\r\n|\r|\n)/g, '</p><p>')}</p>`;
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-25 08:07:17 +02:00
|
|
|
export = {
|
2018-11-16 12:12:04 +01:00
|
|
|
importOpml
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|