// Evernote .enex export files (https://evernote.com/blog/how-evernotes-xml-export-format-works), // which Evernote, Joplin, Apple Notes importers and many note apps read and // write. The XML is parsed on the server (server/lib/enexImport.js, with // htmlparser2); this module maps notes to cards and writes a board as .enex. // Plain JavaScript: tests/enexFormat.test.cjs runs both directions in Node. // // note title -> card title // note content (ENML) -> description, as text; each line // -> an item of the checklist "To-do" // created / updated -> creation date // tag -> label // note-attributes // reminder-time -> due date // reminder-done-time -> the card is in the list "Done", its end date // author, source-url -> appended to the description // resource (files, images) -> not imported; counted and reported // Every other note is in the list "Notes". export const MAX_ENEX_LENGTH = 256 * 1024 * 1024; export const MAX_ENEX_NOTES = 50000; export const ENEX_TODO_MARK = '\u0001'; // 20261116T100000Z -> ISO export function enexDate(value) { const m = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$/.exec(String(value || '').trim()); if (!m) return undefined; const d = new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6])); return Number.isNaN(d.getTime()) ? undefined : d.toISOString(); } // ISO -> 20261116T100000Z export function toEnexDate(value) { const d = new Date(value || 0); if (!value || Number.isNaN(d.getTime())) return ''; return d.toISOString().replace(/[-:]/g, '').replace(/\.\d{3}/, ''); } // The card of one note, from what the server read out of its XML: // { title, text (the content's text, with ENEX_TODO_MARK + 'x' or ' ' before // each to-do), created, updated, tags, reminderTime, reminderDoneTime, // author, sourceUrl, resources }. export function enexNoteToTask(note) { const description = []; const items = []; for (const line of String(note.text || '').split('\n')) { if (line.startsWith(ENEX_TODO_MARK)) { const title = line.slice(2).trim(); if (title) items.push({ title, done: line[1] === 'x' }); } else description.push(line); } const extra = [note.author && `Author: ${note.author}`, note.sourceUrl].filter(Boolean); const done = Boolean(enexDate(note.reminderDoneTime)); return { title: String(note.title || '').trim() || 'Untitled note', description: [description.join('\n').replace(/\n{3,}/g, '\n\n').trim(), ...extra].filter(Boolean).join('\n\n'), column_name: done ? 'Done' : 'Notes', swimlane_name: 'Default', date_creation: enexDate(note.created) || enexDate(note.updated), date_due: enexDate(note.reminderTime), date_end: done ? enexDate(note.reminderDoneTime) : undefined, due_complete: done && enexDate(note.reminderTime) ? true : undefined, tags: [...new Set((note.tags || []).map(t => String(t).trim()).filter(Boolean))], checklists: items.length ? [{ title: 'To-do', items }] : [], }; } export function enexBoard(notes, { name = 'Imported Evernote', cutOff = false } = {}) { const tasks = notes.map(enexNoteToTask); const resources = notes.reduce((n, note) => n + (Number(note.resources) || 0), 0); const unsupported = []; if (cutOff) unsupported.push({ path: '/en-export/note', reason: `only the first ${MAX_ENEX_NOTES} notes are imported` }); if (resources) unsupported.push({ path: '/en-export/note/resource', reason: `${resources} attached file(s) and image(s) are not imported` }); const lists = ['Notes', ...(tasks.some(t => t.column_name === 'Done') ? ['Done'] : [])]; return { board: { name }, columns: lists.map(title => ({ title })), swimlanes: [{ name: 'Default' }], tasks, warnings: [], unsupported }; } // ----- export -------------------------------------------------------------- const xml = value => String(value == null ? '' : value).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"') // eslint-disable-next-line no-control-regex .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, ''); // CDATA cannot hold "]]>"; it is split across two sections. const cdata = text => `/g, ']]]]>')}]]>`; function enml(item) { const blocks = []; for (const line of String(item.description || '').replace(/\r\n|\r/g, '\n').split('\n')) { blocks.push(line.trim() ? `
${xml(line)}
` : '

'); } for (const checklist of item.checklists || []) { blocks.push(`
${xml(checklist.title)}
`); for (const it of checklist.items || []) { blocks.push(`
${xml(it.title)}
`); } } if ((item.comments || []).length) { blocks.push('
'); for (const c of item.comments) blocks.push(`
${xml(c.author ? `${c.author}: ${c.text}` : c.text)}
`); } return '' + '' + `${blocks.join('')}`; } // The parts of an .enex, so a large board can be written a page at a time // (server/lib/renderExternalExport.js): the head, the notes of some cards, the // tail. formatEnex is the three together. export const enexHead = (now = new Date()) => '\n' + '\n' + `\n`; export const ENEX_TAIL = '\n'; export function enexNotes(items = []) { return items.map(item => { const created = toEnexDate(item.createdAt); const attributes = [ item.dueAt ? `${toEnexDate(item.dueAt)}` : '', item.dueAt && item.dueComplete ? `${toEnexDate(item.endAt || item.dueAt)}` : '', item.creator ? `${xml(item.creator)}` : '', ].join(''); return [ ' ', ` ${xml(String(item.title || 'Untitled').slice(0, 255))}`, ` ${cdata(enml(item))}`, created ? ` ${created}` : '', created ? ` ${created}` : '', ...[...new Set([...(item.labels || []), item.listTitle].filter(Boolean))].map(tag => ` ${xml(tag)}`), attributes ? ` ${attributes}` : '', ' ', ].filter(Boolean).join('\n'); }).map(note => `${note}\n`).join(''); } export function formatEnex({ items = [] } = {}, { now = new Date() } = {}) { return enexHead(now) + enexNotes(items) + ENEX_TAIL; }