Files
wekan/models/lib/opmlOutlineFormat.js
T
Lauri Ojansivu ce40b56abd The Markdown, Obsidian, OPML and Org mode outlines and the Notion CSV are written a page of cards at a time.
The outlines group cards under their lists, so their pages come list by
list in the board's order (byList: 'lists', models/lib/exportPaging.js
byListOrder): a list's heading opens before its first card and closes
after its last, wherever the page boundary falls, and a list with no cards
is written where it stands, from the board's light first pass
(outlineMarks). Lines are joined with the separator before each line
(joinPage), so the pages together are byte for byte the whole file. The
Notion CSV counts its swimlane and custom field columns over the whole
board and writes its header on the first page.

Every page also passes the board's, lists' and swimlanes' titles through
the export boundary, as the whole-file export does; before, a paged
format's list titles came straight from the database.

Markdown and Obsidian write a list title once: two lists of one name used
to write their heading twice, each with the cards of both.

Leo stays whole: its card texts are a second section after the whole
outline. tests/exportPaging.test.cjs checks every page size against the
whole file, with empty lists and a card of a list the board does not have;
tests/playwright/specs/import-export-format-audit.e2e.js passes, 173/173.

Thanks to xet7 !
2026-11-20 09:43:18 +02:00

66 lines
3.0 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, 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}/>`;
};
// 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),
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);
}