mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Of an Org heading's property drawer only :CREATED: was read, and the rest of it - :ID:, :EFFORT:, any property of the user's own - and the whole :LOGBOOK: drawer were dropped without a word in the import loss report. Now :ID: / :CUSTOM_ID: is the card's source reference, every other property is a custom field of that name (:NAME+: appends), and the closed CLOCK lines add up to the spent time. The export writes the custom fields back as properties. An open clock, a property line that is not :NAME: value, a :CREATED: that is not a date and any other drawer are reported. tests/orgMode.test.cjs: the round trip and the negative cases. Thanks to xet7 !
169 lines
10 KiB
JavaScript
169 lines
10 KiB
JavaScript
'use strict';
|
|
|
|
// Org mode outline import/export (models/lib/orgModeFormat.js), following
|
|
// https://orgmode.org/manual/ (headlines, TODO keywords, priorities, tags,
|
|
// planning lines, property drawers, checkboxes). Run: node tests/orgMode.test.cjs
|
|
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const read = file => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
|
|
|
async function main() {
|
|
const { parseOrgMode, formatOrgMode, orgTimestamp, MAX_ORG_HEADINGS } = await import('../models/lib/orgModeFormat.js');
|
|
const { EXTERNAL_PARSERS } = await import('../models/lib/externalParsers.js');
|
|
const { formatters } = await import('../models/lib/externalExportFormatters.js');
|
|
const { validateImportSourceShape } = await import('../models/lib/importSourceShape.js');
|
|
const { planImportedTask } = await import('../models/lib/importedTaskPlan.js');
|
|
let passed = 0;
|
|
const test = (name, fn) => { fn(); passed += 1; console.log(' ok -', name); };
|
|
|
|
test('an Org file maps to lists, cards, keywords, priorities, tags, dates, body, checkboxes and sub-headings', () => {
|
|
const { board, columns, tasks, unsupported } = parseOrgMode([
|
|
'#+TITLE: Plant',
|
|
'#+TODO: TODO(t) NEXT(n) | DONE(d) CANCELLED(c)',
|
|
'Loose preamble text.',
|
|
'* To Do',
|
|
'** NEXT [#A] Order valves :shop:urgent:',
|
|
' SCHEDULED: <2026-10-01 Thu> DEADLINE: <2026-10-10 Sat 14:30>',
|
|
' :PROPERTIES:',
|
|
' :CREATED: [2026-09-01 Tue 12:00]',
|
|
' :END:',
|
|
' Two of them.',
|
|
' Not a *heading*.',
|
|
' - [X] Measure',
|
|
' - [ ] Call the vendor',
|
|
'*** Fitting',
|
|
'**** DONE Seal',
|
|
'**** Bolts',
|
|
'*** TODO Paint',
|
|
'** CANCELLED Old idea',
|
|
'* Done',
|
|
'** DONE Replace pump',
|
|
' CLOSED: [2026-10-09 Fri 12:00]',
|
|
'** TODO Weekly check',
|
|
' SCHEDULED: <2026-10-05 Mon +1w>',
|
|
].join('\n'));
|
|
assert.equal(board.name, 'Plant');
|
|
assert.deepEqual(columns.map(column => column.title), ['To Do', 'Done']);
|
|
const [valves, idea, pump, weekly] = tasks;
|
|
assert.equal(valves.title, 'Order valves', 'keyword, priority and tags leave the title');
|
|
assert.deepEqual(valves.tags, ['priority:A', 'shop', 'urgent'], 'NEXT is a to-do keyword from #+TODO');
|
|
assert.deepEqual([valves.date_started, valves.date_due, valves.date_creation],
|
|
['2026-10-01T00:00:00.000Z', '2026-10-10T14:30:00.000Z', '2026-09-01T12:00:00.000Z']);
|
|
assert.equal(valves.description, 'Two of them.\nNot a *heading*.');
|
|
assert.deepEqual(valves.checklists, [
|
|
{ title: 'Order valves', items: [{ title: 'Measure', done: true }, { title: 'Call the vendor', done: false }, { title: 'Paint', done: false }] },
|
|
{ title: 'Fitting', items: [{ title: 'Seal', done: true }, { title: 'Bolts', done: false }] },
|
|
]);
|
|
assert.deepEqual(idea.tags, ['done'], 'CANCELLED is a done keyword from #+TODO');
|
|
assert.deepEqual([pump.tags, pump.date_end], [['done'], '2026-10-09T12:00:00.000Z']);
|
|
assert.equal(weekly.date_started, '2026-10-05T00:00:00.000Z');
|
|
// A +1w repeater makes the card repeat weekly now (models/lib/recurrenceMap.js).
|
|
assert.equal(weekly.recurrence, 'weekly');
|
|
assert.deepEqual(unsupported.map(u => u.reason), [
|
|
'text before the first heading has no WeKan field',
|
|
]);
|
|
const plan = planImportedTask(valves);
|
|
assert.equal(plan.card.dueAt.toISOString(), '2026-10-10T14:30:00.000Z');
|
|
assert.equal(plan.checklists.length, 2);
|
|
});
|
|
|
|
test('timestamps: active and inactive, with and without a time, and invalid ones', () => {
|
|
assert.deepEqual(orgTimestamp('<2026-10-10 Sat>'), { iso: '2026-10-10T00:00:00.000Z', extra: '' });
|
|
assert.deepEqual(orgTimestamp('[2026-10-10 la 9:05]'), { iso: '2026-10-10T09:05:00.000Z', extra: '' }, 'a localized day name');
|
|
assert.equal(orgTimestamp('<2026-02-30 Mon>'), null);
|
|
assert.equal(orgTimestamp('tomorrow'), null);
|
|
});
|
|
|
|
test('a board exported to Org imports back with lists, keywords, priority, tags, dates, body and checklists', () => {
|
|
const collected = { board: { title: 'Plant' }, lists: [{ title: 'Doing' }, { title: 'Done' }], items: [
|
|
{ cardId: 'a', listTitle: 'Doing', title: 'Order\nvalves', description: 'Two of them\n* not a heading', labels: ['priority:B', 'shop floor'],
|
|
startAt: '2026-10-01T00:00:00.000Z', dueAt: '2026-10-10T14:30:00.000Z', createdAt: '2026-09-01T12:00:00.000Z',
|
|
checklists: [{ title: 'Steps', items: [{ title: 'Open', done: true }, { title: 'Close', done: false }] }] },
|
|
{ cardId: 'b', listTitle: 'Done', title: 'Ship it', labels: [], endAt: '2026-10-09T12:00:00.000Z' },
|
|
] };
|
|
const org = formatters.orgmode(collected);
|
|
assert.match(org, /^#\+TITLE: Plant\n#\+TODO: TODO \| DONE\n/);
|
|
assert.match(org, /^\*\* TODO \[#B\] Order valves :shop_floor:$/m);
|
|
assert.match(org, /^ {3}SCHEDULED: <2026-10-01 Thu> DEADLINE: <2026-10-10 Sat 14:30>$/m);
|
|
const back = EXTERNAL_PARSERS.orgmode(org);
|
|
assert.deepEqual(back.columns.map(c => c.title), ['Doing', 'Done']);
|
|
const [valves, ship] = back.tasks;
|
|
assert.equal(valves.title, 'Order valves');
|
|
assert.deepEqual(valves.tags, ['priority:B', 'shop_floor']);
|
|
assert.equal(valves.description, 'Two of them\n* not a heading', 'an indented star line stays body text');
|
|
assert.deepEqual([valves.date_started, valves.date_due, valves.date_creation],
|
|
['2026-10-01T00:00:00.000Z', '2026-10-10T14:30:00.000Z', '2026-09-01T12:00:00.000Z']);
|
|
assert.deepEqual(valves.checklists, [{ title: 'Steps', items: [{ title: 'Open', done: true }, { title: 'Close', done: false }] }]);
|
|
assert.deepEqual([ship.tags, ship.date_end], [['done'], '2026-10-09T12:00:00.000Z']);
|
|
assert.deepEqual(back.unsupported, []);
|
|
assert.equal(formatOrgMode(collected), org);
|
|
});
|
|
|
|
test('negative: not Org, oversized or too many headings is refused; list text is reported', () => {
|
|
assert.throws(() => parseOrgMode('just some notes\nwithout headings'), /no headings/);
|
|
assert.throws(() => parseOrgMode('x'.repeat(16 * 1024 * 1024 + 1)), /too large/);
|
|
assert.throws(() => parseOrgMode('* x\n'.repeat(MAX_ORG_HEADINGS + 1)), /too many headings/);
|
|
assert.deepEqual(parseOrgMode('* List\nSome list text\n** Card\n').unsupported.map(u => u.reason),
|
|
['text under a list heading has no WeKan field']);
|
|
assert.throws(() => validateImportSourceShape('orgmode', ' '), /Invalid orgmode/);
|
|
});
|
|
|
|
test('wired in: import page, server import, export menu and route', () => {
|
|
assert.match(read('models/lib/importSources.js'), /\{ key: 'orgmode', name: 'Org mode'[,}]/); // the one list of sources
|
|
// The page reads every source through one path described in
|
|
// models/lib/importSources.js, not a branch of its own.
|
|
assert.match(read('models/lib/importSources.js'), /\{ key: 'orgmode', name: '[^']+', \.\.\.TEXT/, 'read by the one import path');
|
|
assert.match(read('models/import.js'), /case 'orgmode':\s*\/\/[^\n]*\n\s*check\(board, String\);/);
|
|
assert.match(read('models/import.js'), /new KanboardCreator\(data, 'orgmode'\)/);
|
|
assert.match(read('client/components/boards/exportScope.js'), /key: 'orgmode'[^\n]*path: 'export\/orgmode', ext: 'org'/);
|
|
assert.match(read('server/lib/renderExternalExport.js'), /orgmode: 'text\/x-org'/);
|
|
const en = JSON.parse(read('imports/i18n/data/en.i18n.json'));
|
|
assert.match(en['import-board-instruction-orgmode'], /Org mode/);
|
|
});
|
|
|
|
test('properties are custom fields, ID the reference, LOGBOOK clocks the spent time; custom fields are written back', () => {
|
|
const board = parseOrgMode([
|
|
'* L', '** TODO A',
|
|
' :PROPERTIES:', ' :ID: abc', ' :EFFORT: 1:30', ' :Owner: ann', ' :Owner+: bob', ' :END:',
|
|
' :LOGBOOK:',
|
|
' CLOCK: [2026-10-01 Thu 10:00]--[2026-10-01 Thu 11:30] => 1:30',
|
|
' CLOCK: [2026-10-02 Fri 10:00]--[2026-10-02 Fri 10:15] => 0:15',
|
|
' :END:', ' Body', ''].join('\n'));
|
|
const [card] = board.tasks;
|
|
assert.equal(card.ref, 'abc');
|
|
assert.deepEqual(card.custom_fields, { EFFORT: '1:30', Owner: 'ann bob' });
|
|
assert.equal(card.spent_hours, 1.75);
|
|
assert.equal(card.description, 'Body');
|
|
assert.deepEqual(board.unsupported, []);
|
|
const plan = planImportedTask(card);
|
|
assert.equal(plan.card.spentTime, 1.75);
|
|
const org = formatOrgMode({ board: { title: 'B' }, items: [
|
|
{ cardId: 'c', title: 'A', listTitle: 'L', createdAt: '2026-09-01T12:00:00.000Z', customFields: { 'Time estimate': 2, EFFORT: '1:30' } }] });
|
|
assert.match(org, /^ {3}:PROPERTIES:\n {3}:CREATED: {2}\[2026-09-01 Tue 12:00\]\n {3}:Time_estimate: 2\n {3}:EFFORT: 1:30\n {3}:END:$/m);
|
|
assert.deepEqual(parseOrgMode(org).tasks[0].custom_fields, { Time_estimate: '2', EFFORT: '1:30' });
|
|
});
|
|
|
|
test('negative: an open clock, another drawer and a bad property line are reported, not lost silently', () => {
|
|
const board = parseOrgMode(['* L', '** TODO A',
|
|
' :PROPERTIES:', ' not a property', ' :CREATED: yesterday', ' :END:',
|
|
' :LOGBOOK:', ' CLOCK: [2026-10-03 Sat 10:00]', ' :END:',
|
|
' :NOTES:', ' kept nowhere', ' :END:', ''].join('\n'));
|
|
const reasons = board.unsupported.map(u => u.reason);
|
|
assert.ok(reasons.some(r => /property line "not a property"/.test(r)), reasons.join('|'));
|
|
assert.ok(reasons.some(r => /CREATED yesterday is not a date/.test(r)));
|
|
assert.ok(reasons.some(r => /open Org clock/.test(r)));
|
|
assert.ok(reasons.some(r => /drawer :NOTES:/.test(r)));
|
|
assert.equal(board.tasks[0].spent_hours, undefined);
|
|
assert.equal(board.tasks[0].description, '', 'drawer text is not the description');
|
|
// A custom field cannot write the reference or the creation date over.
|
|
const org = formatOrgMode({ board: { title: 'B' }, items: [{ cardId: 'c', title: 'A', listTitle: 'L', customFields: { ID: 'x', CREATED: 'y' } }] });
|
|
assert.doesNotMatch(org, /:ID:|:CREATED:/);
|
|
});
|
|
|
|
console.log(`\norgMode: ${passed} tests passed`);
|
|
}
|
|
|
|
main().catch(error => { console.error(error); process.exitCode = 1; });
|