Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/export/opml

Elian Dorancfeb0cc6f78b
diff --git a/src/routes/api/export.js b/src/routes/api/export.js
index 440eecb8d3..a69462e9dc 100644
--- a/src/routes/api/export.js
+++ b/src/routes/api/export.js
@@ -2,7 +2,7 @@
const zipExportService = require('../../services/export/zip.js');
const singleExportService = require('../../services/export/single.js');
-const opmlExportService = require('../../services/export/opml.js');
+const opmlExportService = require('../../services/export/opml');
const becca = require('../../becca/becca');
const TaskContext = require('../../services/task_context');
const log = require('../../services/log');
diff --git a/src/routes/api/import.js b/src/routes/api/import.js
index 19b54e8e9e..5d296ac886 100644
--- a/src/routes/api/import.js
+++ b/src/routes/api/import.js
@@ -1,7 +1,7 @@
"use strict";
const enexImportService = require('../../services/import/enex.js');
-const opmlImportService = require('../../services/import/opml.js');
+const opmlImportService = require('../../services/import/opml');
const zipImportService = require('../../services/import/zip.js');
const singleImportService = require('../../services/import/single.js');
const cls = require('../../services/cls');
diff --git a/src/services/export/opml.js b/src/services/export/opml.ts
similarity index 71%
rename from src/services/export/opml.js
rename to src/services/export/opml.ts
index 2faa6d5138..63bdf14fd5 100644
--- a/src/services/export/opml.js
+++ b/src/services/export/opml.ts
@@ -1,9 +1,12 @@
"use strict";
-const utils = require('../utils');
-const becca = require('../../becca/becca');
+import utils = require('../utils');
+import becca = require('../../becca/becca');
+import TaskContext = require('../task_context');
+import BBranch = require('../../becca/entities/bbranch');
+import { Response } from 'express';
-function exportToOpml(taskContext, branch, version, res) {
+function exportToOpml(taskContext: TaskContext, branch: BBranch, version: string, res: Response) {
if (!['1.0', '2.0'].includes(version)) {
throw new Error(`Unrecognized OPML version ${version}`);
}
@@ -12,9 +15,12 @@ function exportToOpml(taskContext, branch, version, res) {
const note = branch.getNote();
- function exportNoteInner(branchId) {
+ function exportNoteInner(branchId: string) {
const branch = becca.getBranch(branchId);
+ if (!branch) { throw new Error("Unable to find branch."); }
+
const note = branch.getNote();
+ if (!note) { throw new Error("Unable to find note."); }
if (note.hasOwnedLabel('excludeFromExport')) {
return;
@@ -24,13 +30,13 @@ function exportToOpml(taskContext, branch, version, res) {
if (opmlVersion === 1) {
const preparedTitle = escapeXmlAttribute(title);
- const preparedContent = note.hasStringContent() ? prepareText(note.getContent()) : '';
+ const preparedContent = note.hasStringContent() ? prepareText(note.getContent() as string) : '';
res.write(`<outline title="${preparedTitle}" text="${preparedContent}">\n`);
}
else if (opmlVersion === 2) {
const preparedTitle = escapeXmlAttribute(title);
- const preparedContent = note.hasStringContent() ? escapeXmlAttribute(note.getContent()) : '';
+ const preparedContent = note.hasStringContent() ? escapeXmlAttribute(note.getContent() as string) : '';
res.write(`<outline text="${preparedTitle}" _note="${preparedContent}">\n`);
}
@@ -41,7 +47,9 @@ function exportToOpml(taskContext, branch, version, res) {
taskContext.increaseProgressCount();
for (const child of note.getChildBranches()) {
- exportNoteInner(child.branchId);
+ if (child?.branchId) {
+ exportNoteInner(child.branchId);
+ }
}
res.write('</outline>');
@@ -60,7 +68,9 @@ function exportToOpml(taskContext, branch, version, res) {
</head>
<body>`);
- exportNoteInner(branch.branchId);
+ if (branch.branchId) {
+ exportNoteInner(branch.branchId);
+ }
res.write(`</body>
</opml>`);
@@ -69,7 +79,7 @@ function exportToOpml(taskContext, branch, version, res) {
taskContext.taskSucceeded();
}
-function prepareText(text) {
+function prepareText(text: string) {
const newLines = text.replace(/(<p[^>]*>|<br\s*\/?>)/g, '\n')
.replace(/&nbsp;/g, ' '); // nbsp isn't in XML standard (only HTML)
@@ -80,7 +90,7 @@ function prepareText(text) {
return escaped.replace(/\n/g, '&#10;');
}
-function escapeXmlAttribute(text) {
+function escapeXmlAttribute(text: string) {
return text.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
@@ -88,6 +98,6 @@ function escapeXmlAttribute(text) {
.replace(/'/g, '&apos;');
}
-module.exports = {
+export = {
exportToOpml
};
diff --git a/src/services/task_context.ts b/src/services/task_context.ts
index 78ab6b10c4..7d2cdc4487 100644
--- a/src/services/task_context.ts
+++ b/src/services/task_context.ts
@@ -65,7 +65,7 @@ class TaskContext {
});
}
- taskSucceeded(result: string) {
+ taskSucceeded(result?: string) {
ws.sendMessageToAllClients({
type: 'taskSucceeded',
taskId: this.taskId,