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/turndown": "^5.0.4",
"@types/ws": "^8.5.10",
"@types/xml2js": "^0.4.14",
"cross-env": "7.0.3",
"electron": "25.9.8",
"electron-builder": "24.6.4",
@ -1811,6 +1812,15 @@
"@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": {
"version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",
@ -16550,6 +16560,15 @@
"@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": {
"version": "2.9.2",
"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/turndown": "^5.0.4",
"@types/ws": "^8.5.10",
"@types/xml2js": "^0.4.14",
"cross-env": "7.0.3",
"electron": "25.9.8",
"electron-builder": "24.6.4",

View File

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