'use strict';
// ---- A small, safe Markdown renderer for notes: headings, bold, italic, strike, code, links, quotes, bullet and numbered lists, and "- [ ]" tick boxes.
// Everything is escaped first, and only http(s) links are made clickable, so a note can never inject a script or a strange link.
function ntMd(src) {
const inline = t => esc(t)
.replace(/`([^`]+)`/g, '$1')
.replace(/\*\*([^*]+)\*\*/g, '$1').replace(/(^|[\s(])\*([^*\s][^*]*)\*/g, '$1$2').replace(/~~([^~]+)~~/g, '$1')
.replace(/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g, '$1')
.replace(/(^|[\s(])(https?:\/\/[^\s<)]+)/g, '$1$2');
const lines = String(src || '').replace(/\r/g, '').split('\n'), out = []; let list = null, code = false, para = [];
const endPara = () => { if (para.length) { out.push('
' + para.map(inline).join('
') + '
'); code = !code; return; }
if (code) { out.push(esc(raw) + '\n'); return; }
let m;
if ((m = /^(\s*)[-*+] \[( |x|X)\] (.*)$/.exec(raw))) { endPara(); if (list !== 'ul') { endList(); out.push(''); list = 'ul'; } out.push(`- ${inline(m[3])}
`); return; }
if ((m = /^\s*[-*+] (.*)$/.exec(raw))) { endPara(); if (list !== 'ul') { endList(); out.push(''); list = 'ul'; } out.push('- ' + inline(m[1]) + '
'); return; }
if ((m = /^\s*\d+[.)] (.*)$/.exec(raw))) { endPara(); if (list !== 'ol') { endList(); out.push(''); list = 'ol'; } out.push('- ' + inline(m[1]) + '
'); return; }
endList();
if ((m = /^(#{1,3}) (.*)$/.exec(raw))) { endPara(); out.push(`${inline(m[2])} `); return; }
if (/^\s*(---|\*\*\*)\s*$/.test(raw)) { endPara(); out.push('
'); return; }
if ((m = /^> ?(.*)$/.exec(raw))) { endPara(); out.push('' + inline(m[1]) + '
'); return; }
if (!raw.trim()) { endPara(); return; }
para.push(raw);
});
endPara(); endList(); if (code) out.push('
');
return out.join('');
}
/** The text of a note without the Markdown marks, for the small preview on a card. */
const ntPlain = s => String(s || '').replace(/```/g, '').replace(/^#{1,3} /gm, '').replace(/^\s*[-*+] \[( |x|X)\] /gm, (m, x) => x === ' ' ? '☐ ' : '☑ ').replace(/^\s*[-*+] /gm, '• ').replace(/^> ?/gm, '').replace(/\*\*([^*]+)\*\*/g, '$1').replace(/~~([^~]+)~~/g, '$1').replace(/\[([^\]]+)\]\([^)]+\)/g, '$1').replace(/`([^`]+)`/g, '$1');