Files
wekan/models/lib/opmlOutlineFormat.js
T
Lauri Ojansivu 95b88095c4 OPML keeps created, category, links and an outliner's own attributes.
A card outline's created (OPML 2.0's creation date), its category (its
tags), and any attribute of the outliner's own - OmniOutliner writes its
columns as attributes - were dropped without a word, and a url / htmlUrl /
xmlUrl link was only reported. Now created is the creation date, each
comma-separated category a label (its leading "/" taken off), a link goes at
the end of the description, and any other attribute is a custom field. The
export writes created (RFC 822) and the labels as category. An attribute of
a list or a checklist item, and a created that is not a date, are reported;
type, isComment and isBreakpoint only say how a row is drawn.

tests/opmlOutline.test.cjs: both ways and the negative cases. The
round-trip spec's KEEPS table now has OPML keep labels; that Playwright
spec was not run here.

Thanks to xet7 !
2026-11-20 17:16:47 +02:00

71 lines
3.5 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;');
import { pagingOf, listTitlesOf, byListOrder, outlineMarks, joinPage, markKey } from './exportPaging.js';
// The same terminal-list heuristic the markdown and Leo formatters use.
const isClosed = listTitle => /done|closed|complete|archiv|finished/i.test(listTitle || '');
const outline = (text, { note, done, created, category, kids = [] }, indent) => {
const attrs = `text="${escapeAttr(text)}"${note ? ` _note="${escapeAttr(note)}"` : ''}${done ? ' _complete="true"' : ''}`
+ `${created ? ` created="${escapeAttr(created)}"` : ''}${category ? ` category="${escapeAttr(category)}"` : ''}`;
return kids.length
? `${indent}<outline ${attrs}>\n${kids.map(k => k(`${indent} `)).join('\n')}\n${indent}</outline>`
: `${indent}<outline ${attrs}/>`;
};
// The whole board, or one page of it (models/lib/exportPaging.js: the pages
// come list by list, and together are the whole file). A list's outline opens
// before its first card and closes after its last; a list with no cards is
// written where it stands, between the cards of the lists around it.
export function formatOpml(collected) {
const { board, lists } = collected;
const { items, all, first, last } = pagingOf(collected);
const titles = listTitlesOf(lists, collected.page ? all : items);
const ordered = collected.page ? items : byListOrder(items, titles);
const { marks, lastKnown } = outlineMarks(collected.page ? all : ordered, titles);
const empty = index => outline(titles[index], {}, ' ');
const lines = first ? [
'<?xml version="1.0" encoding="UTF-8"?>',
'<opml version="2.0">',
' <head>',
` <title>${escapeAttr(board && board.title)}</title>`,
' </head>',
' <body>',
] : [];
for (const i of ordered) {
const mark = marks.get(markKey(i));
if (mark.first) {
for (let k = mark.before + 1; k < mark.list; k += 1) lines.push(empty(k));
lines.push(` <outline text="${escapeAttr(titles[mark.list])}">`);
}
lines.push(outline(i.title, {
note: i.description || '',
done: isClosed(i.listTitle),
// OPML 2.0: created is an RFC 822 date, category comma-separated.
created: i.createdAt && !Number.isNaN(new Date(i.createdAt).getTime()) ? new Date(i.createdAt).toUTCString() : '',
category: (Array.isArray(i.labels) ? i.labels : []).filter(name => name && name !== 'done')
.map(name => String(name).replace(/,/g, ' ').trim()).filter(Boolean).join(','),
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)),
}, ' '));
if (mark.last) lines.push(' </outline>');
}
if (last) {
for (let k = lastKnown + 1; k < titles.length; k += 1) lines.push(empty(k));
lines.push(' </body>', '</opml>', '');
}
return joinPage(lines, first);
}