server-ts: Port services/import/opml

This commit is contained in:
Elian Doran 2024-02-25 08:07:17 +02:00
parent fd37fd3a45
commit cc1a545e13
No known key found for this signature in database
3 changed files with 53 additions and 16 deletions

19
package-lock.json generated
View File

@ -101,6 +101,7 @@
"@types/sanitize-html": "^2.11.0", "@types/sanitize-html": "^2.11.0",
"@types/turndown": "^5.0.4", "@types/turndown": "^5.0.4",
"@types/ws": "^8.5.10", "@types/ws": "^8.5.10",
"@types/xml2js": "^0.4.14",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"electron": "25.9.8", "electron": "25.9.8",
"electron-builder": "24.6.4", "electron-builder": "24.6.4",
@ -1811,6 +1812,15 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/@types/xml2js": {
"version": "0.4.14",
"resolved": "https://registry.npmjs.org/@types/xml2js/-/xml2js-0.4.14.tgz",
"integrity": "sha512-4YnrRemBShWRO2QjvUin8ESA41rH+9nQGLUGZV/1IDhi3SL9OhdpNC/MrulTWuptXKwhx/aDxE7toV0f/ypIXQ==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/yauzl": { "node_modules/@types/yauzl": {
"version": "2.9.2", "version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",
@ -16550,6 +16560,15 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/xml2js": {
"version": "0.4.14",
"resolved": "https://registry.npmjs.org/@types/xml2js/-/xml2js-0.4.14.tgz",
"integrity": "sha512-4YnrRemBShWRO2QjvUin8ESA41rH+9nQGLUGZV/1IDhi3SL9OhdpNC/MrulTWuptXKwhx/aDxE7toV0f/ypIXQ==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/yauzl": { "@types/yauzl": {
"version": "2.9.2", "version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",

View File

@ -124,6 +124,7 @@
"@types/sanitize-html": "^2.11.0", "@types/sanitize-html": "^2.11.0",
"@types/turndown": "^5.0.4", "@types/turndown": "^5.0.4",
"@types/ws": "^8.5.10", "@types/ws": "^8.5.10",
"@types/xml2js": "^0.4.14",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"electron": "25.9.8", "electron": "25.9.8",
"electron-builder": "24.6.4", "electron-builder": "24.6.4",

View File

@ -1,20 +1,37 @@
"use strict"; "use strict";
const noteService = require('../../services/notes'); import noteService = require('../../services/notes');
const parseString = require('xml2js').parseString; import xml2js = require("xml2js");
const protectedSessionService = require('../protected_session'); import protectedSessionService = require('../protected_session');
const htmlSanitizer = require('../html_sanitizer'); import htmlSanitizer = require('../html_sanitizer');
import TaskContext = require('../task_context');
import BNote = require('../../becca/entities/bnote');
const parseString = xml2js.parseString;
/** interface OpmlXml {
* @param {TaskContext} taskContext opml: OpmlBody;
* @param {Buffer} fileBuffer }
* @param {BNote} parentNote
* @returns {Promise<*[]|*>} interface OpmlBody {
*/ $: {
async function importOpml(taskContext, fileBuffer, parentNote) { version: string
const xml = await new Promise(function(resolve, reject) }
body: OpmlOutline[]
}
interface OpmlOutline {
$: {
title: string;
text: string;
_note: string;
};
outline: OpmlOutline[];
}
async function importOpml(taskContext: TaskContext, fileBuffer: Buffer, parentNote: BNote) {
const xml = await new Promise<OpmlXml>(function(resolve, reject)
{ {
parseString(fileBuffer, function (err, result) { parseString(fileBuffer, function (err: any, result: OpmlXml) {
if (err) { if (err) {
reject(err); reject(err);
} }
@ -30,7 +47,7 @@ async function importOpml(taskContext, fileBuffer, parentNote) {
const opmlVersion = parseInt(xml.opml.$.version); const opmlVersion = parseInt(xml.opml.$.version);
function importOutline(outline, parentNoteId) { function importOutline(outline: OpmlOutline, parentNoteId: string) {
let title, content; let title, content;
if (opmlVersion === 1) { if (opmlVersion === 1) {
@ -83,7 +100,7 @@ async function importOpml(taskContext, fileBuffer, parentNote) {
return returnNote; return returnNote;
} }
function toHtml(text) { function toHtml(text: string) {
if (!text) { if (!text) {
return ''; return '';
} }
@ -91,6 +108,6 @@ function toHtml(text) {
return `<p>${text.replace(/(?:\r\n|\r|\n)/g, '</p><p>')}</p>`; return `<p>${text.replace(/(?:\r\n|\r|\n)/g, '</p><p>')}</p>`;
} }
module.exports = { export = {
importOpml importOpml
}; };