mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Port services/export/opml
This commit is contained in:
parent
ec4bd6659a
commit
cfeb0cc6f7
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const zipExportService = require('../../services/export/zip.js');
|
const zipExportService = require('../../services/export/zip.js');
|
||||||
const singleExportService = require('../../services/export/single.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 becca = require('../../becca/becca');
|
||||||
const TaskContext = require('../../services/task_context');
|
const TaskContext = require('../../services/task_context');
|
||||||
const log = require('../../services/log');
|
const log = require('../../services/log');
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const enexImportService = require('../../services/import/enex.js');
|
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 zipImportService = require('../../services/import/zip.js');
|
||||||
const singleImportService = require('../../services/import/single.js');
|
const singleImportService = require('../../services/import/single.js');
|
||||||
const cls = require('../../services/cls');
|
const cls = require('../../services/cls');
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const utils = require('../utils');
|
import utils = require('../utils');
|
||||||
const becca = require('../../becca/becca');
|
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)) {
|
if (!['1.0', '2.0'].includes(version)) {
|
||||||
throw new Error(`Unrecognized OPML version ${version}`);
|
throw new Error(`Unrecognized OPML version ${version}`);
|
||||||
}
|
}
|
||||||
@ -12,9 +15,12 @@ function exportToOpml(taskContext, branch, version, res) {
|
|||||||
|
|
||||||
const note = branch.getNote();
|
const note = branch.getNote();
|
||||||
|
|
||||||
function exportNoteInner(branchId) {
|
function exportNoteInner(branchId: string) {
|
||||||
const branch = becca.getBranch(branchId);
|
const branch = becca.getBranch(branchId);
|
||||||
|
if (!branch) { throw new Error("Unable to find branch."); }
|
||||||
|
|
||||||
const note = branch.getNote();
|
const note = branch.getNote();
|
||||||
|
if (!note) { throw new Error("Unable to find note."); }
|
||||||
|
|
||||||
if (note.hasOwnedLabel('excludeFromExport')) {
|
if (note.hasOwnedLabel('excludeFromExport')) {
|
||||||
return;
|
return;
|
||||||
@ -24,13 +30,13 @@ function exportToOpml(taskContext, branch, version, res) {
|
|||||||
|
|
||||||
if (opmlVersion === 1) {
|
if (opmlVersion === 1) {
|
||||||
const preparedTitle = escapeXmlAttribute(title);
|
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`);
|
res.write(`<outline title="${preparedTitle}" text="${preparedContent}">\n`);
|
||||||
}
|
}
|
||||||
else if (opmlVersion === 2) {
|
else if (opmlVersion === 2) {
|
||||||
const preparedTitle = escapeXmlAttribute(title);
|
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`);
|
res.write(`<outline text="${preparedTitle}" _note="${preparedContent}">\n`);
|
||||||
}
|
}
|
||||||
@ -41,7 +47,9 @@ function exportToOpml(taskContext, branch, version, res) {
|
|||||||
taskContext.increaseProgressCount();
|
taskContext.increaseProgressCount();
|
||||||
|
|
||||||
for (const child of note.getChildBranches()) {
|
for (const child of note.getChildBranches()) {
|
||||||
exportNoteInner(child.branchId);
|
if (child?.branchId) {
|
||||||
|
exportNoteInner(child.branchId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.write('</outline>');
|
res.write('</outline>');
|
||||||
@ -60,7 +68,9 @@ function exportToOpml(taskContext, branch, version, res) {
|
|||||||
</head>
|
</head>
|
||||||
<body>`);
|
<body>`);
|
||||||
|
|
||||||
exportNoteInner(branch.branchId);
|
if (branch.branchId) {
|
||||||
|
exportNoteInner(branch.branchId);
|
||||||
|
}
|
||||||
|
|
||||||
res.write(`</body>
|
res.write(`</body>
|
||||||
</opml>`);
|
</opml>`);
|
||||||
@ -69,7 +79,7 @@ function exportToOpml(taskContext, branch, version, res) {
|
|||||||
taskContext.taskSucceeded();
|
taskContext.taskSucceeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
function prepareText(text) {
|
function prepareText(text: string) {
|
||||||
const newLines = text.replace(/(<p[^>]*>|<br\s*\/?>)/g, '\n')
|
const newLines = text.replace(/(<p[^>]*>|<br\s*\/?>)/g, '\n')
|
||||||
.replace(/ /g, ' '); // nbsp isn't in XML standard (only HTML)
|
.replace(/ /g, ' '); // nbsp isn't in XML standard (only HTML)
|
||||||
|
|
||||||
@ -80,7 +90,7 @@ function prepareText(text) {
|
|||||||
return escaped.replace(/\n/g, ' ');
|
return escaped.replace(/\n/g, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeXmlAttribute(text) {
|
function escapeXmlAttribute(text: string) {
|
||||||
return text.replace(/&/g, '&')
|
return text.replace(/&/g, '&')
|
||||||
.replace(/</g, '<')
|
.replace(/</g, '<')
|
||||||
.replace(/>/g, '>')
|
.replace(/>/g, '>')
|
||||||
@ -88,6 +98,6 @@ function escapeXmlAttribute(text) {
|
|||||||
.replace(/'/g, ''');
|
.replace(/'/g, ''');
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
exportToOpml
|
exportToOpml
|
||||||
};
|
};
|
@ -65,7 +65,7 @@ class TaskContext {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
taskSucceeded(result: string) {
|
taskSucceeded(result?: string) {
|
||||||
ws.sendMessageToAllClients({
|
ws.sendMessageToAllClients({
|
||||||
type: 'taskSucceeded',
|
type: 'taskSucceeded',
|
||||||
taskId: this.taskId,
|
taskId: this.taskId,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user