Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
import type { Response } from "../../http_interface";
import becca from "../../becca/becca.js";
import type BBranch from "../../becca/entities/bbranch.js";
import type TaskContext from "../task_context.js";
import { getContentDisposition } from "../utils/index.js";
function exportToOpml(taskContext: TaskContext<"export">, branch: BBranch, res: Response) {
const note = branch.getNote();
function exportNoteInner(branchId: string) {
const branch = becca.getBranch(branchId);
if (!branch) {
throw new Error("Unable to find branch.");
}
const note = branch.getNote();
/* v8 ignore next 3 -- unreachable: BBranch.getNote() resolves through becca's childNote
getter, which materialises a skeleton note rather than ever returning a falsy value. */
if (!note) {
throw new Error("Unable to find note.");
}
if (note.hasOwnedLabel("excludeFromExport")) {
return;
}
const title = `${branch.prefix ? `${branch.prefix} - ` : ""}${note.title}`;
const preparedTitle = escapeXmlAttribute(title);
const preparedContent = note.hasStringContent() ? escapeXmlAttribute(note.getContent() as string) : "";
res.write(`<outline text="${preparedTitle}" _note="${preparedContent}">\n`);
taskContext.increaseProgressCount();
for (const child of note.getChildBranches()) {
if (child?.branchId) {
exportNoteInner(child.branchId);
}
}
res.write("</outline>");
}
const filename = `${branch.prefix ? `${branch.prefix} - ` : ""}${note.title}.opml`;
res.setHeader("Content-Disposition", getContentDisposition(filename));
res.setHeader("Content-Type", "text/x-opml");
res.write(`<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>Trilium export</title>
</head>
<body>`);
if (branch.branchId) {
exportNoteInner(branch.branchId);
}
res.write(`</body>
</opml>`);
res.end();
taskContext.taskSucceeded(null);
}
function escapeXmlAttribute(text: string) {
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
}
export default {
exportToOpml
};