Files
wekan/models/lib/opmlOutlineFormat.js
T
Lauri Ojansivu e6c8f85518 Import and export boards as OPML outlines.
OPML is what Workflowy, Dynalist, OmniOutliner, Logseq and most other
outliners export and import. Top-level outlines become lists and their
children cards, with the outliners' _note as the description and a completed
item (Workflowy's _complete, Dynalist's complete) as the done tag. Deeper
outlines become checklists with their descendants as items, and the head
title becomes the board title. A list's note and url/htmlUrl/xmlUrl links are
reported in the loss report. Export writes the same with Workflowy's
attribute names; line breaks and tabs in notes are written as character
references so that they survive the XML parser.

Like the Leo outline, the XML is parsed on the server only, with htmlparser2
in XML mode: DTDs and external entities are never resolved, and size and node
counts are bounded. tests/opmlOutline.test.cjs covers a Workflowy/Dynalist
file, the round trip and the negatives (an external entity, a node bomb). A
Playwright case imports an outline through the page, and the export audit
fetches the new format. New instruction text is in English, pending
Transifex.

Thanks to xet7 !
2026-10-08 11:18:14 +03:00

48 lines
2.1 KiB
JavaScript

// Export a board as an OPML outline - the reverse of parseOpml in
// opmlOutline.js, which documents the mapping. Pure and dependency-free, so the
// shared export formatters can load it on both sides.
//
// Workflowy's attribute names are written (`_note`, `_complete`): Workflowy,
// Dynalist and Logseq all read them.
// Attribute values: a line break is written as 
 so that an XML parser,
// which turns a literal one into a space, gives it back.
const escapeAttr = value => String(value == null ? '' : value)
.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
.replace(/"/g, '&quot;').replace(/\r\n|\r|\n/g, '&#10;').replace(/\t/g, '&#9;');
// The same terminal-list heuristic the markdown and Leo formatters use.
const isClosed = listTitle => /done|closed|complete|archiv|finished/i.test(listTitle || '');
export function formatOpml({ board, lists, items }) {
const outline = (text, { note, done, kids = [] }, indent) => {
const attrs = `text="${escapeAttr(text)}"${note ? ` _note="${escapeAttr(note)}"` : ''}${done ? ' _complete="true"' : ''}`;
return kids.length
? `${indent}<outline ${attrs}>\n${kids.map(k => k(`${indent} `)).join('\n')}\n${indent}</outline>`
: `${indent}<outline ${attrs}/>`;
};
const listTitles = (lists || []).map(l => l.title);
for (const item of items || []) if (item.listTitle && !listTitles.includes(item.listTitle)) listTitles.push(item.listTitle);
const body = listTitles.map(title => outline(title, {
kids: (items || []).filter(i => i.listTitle === title).map(i => indent => outline(i.title, {
note: i.description || '',
done: isClosed(i.listTitle),
kids: (Array.isArray(i.checklists) ? i.checklists : []).map(c => ind => outline(c.title, {
kids: (Array.isArray(c.items) ? c.items : []).map(x => ii => outline(x.title, { done: Boolean(x.done) }, ii)),
}, ind)),
}, indent)),
}, ' '));
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<opml version="2.0">',
' <head>',
` <title>${escapeAttr(board && board.title)}</title>`,
' </head>',
' <body>',
...body,
' </body>',
'</opml>',
'',
].join('\n');
}