rob-revisited/ide/ui/js/board-md.test.mjs

52 lines
3.9 KiB
JavaScript

// The Board's Markdown renderer (board-md.js): GFM as the trackers write it, line breaks kept, nothing dangerous.
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/board-md.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { parse, parseInline, plainText, safeHref, safeSrc } from './board-md.js';
// the tree as compact HTML-like text, to compare
const html = (nodes) => nodes.map((n) => (typeof n === 'string' ? n : n.tag === 'br' || n.tag === 'hr' || n.tag === 'img' || n.tag === 'input'
? `<${n.tag}${Object.entries(n.attrs).map(([k, v]) => ` ${k}=${v}`).join('')}>`
: `<${n.tag}${Object.entries(n.attrs).map(([k, v]) => ` ${k}=${v}`).join('')}>${html(n.kids)}</${n.tag}>`)).join('');
test('paragraphs keep their line breaks', () => {
assert.equal(html(parse('The boss jumps twice.\nThen it throws fire.\n\nNew paragraph')), '<p>The boss jumps twice.<br>Then it throws fire.</p><p>New paragraph</p>');
});
test('headings and inline formatting', () => {
assert.equal(html(parse('## Boss *fight*\n**bold** _it_ ~~gone~~ <u>under</u> <mark>hi</mark> <span style="color:#ff0000">red</span> `a*b*` snake_case_name \\*star\\*')),
'<h2>Boss <i>fight</i></h2><p><b>bold</b> <i>it</i> <s>gone</s> <u>under</u> <mark>hi</mark> <span color=#ff0000>red</span> <code>a*b*</code> snake_case_name *star*</p>');
});
test('nested lists, tasks, quotes, code and rules', () => {
assert.equal(html(parse('- One\n - One a\n - One b\n 1. deep\n- [x] done\n- [ ] todo\n\n3. three\n4. four')),
'<ul><li>One<ul><li>One a</li><li>One b<ol><li>deep</li></ol></li></ul></li><li><input type=checkbox checked=true>done</li><li><input type=checkbox checked=false>todo</li></ul><ol start=3><li>three</li><li>four</li></ol>');
assert.equal(html(parse('> Quoted\n>\n> > twice\n\n```php\nif (a) {\n <b>\n}\n```\n---')),
'<blockquote><p>Quoted</p><blockquote><p>twice</p></blockquote></blockquote><pre><code lang=php>if (a) {\n <b>\n}</code></pre><hr>');
});
test('tables, aligned, with escaped pipes and breaks', () => {
assert.equal(html(parse('| Level | Boss |\n| :--- | ---: |\n| **1** | Crab \\| big |\n| two<br>lines |')),
'<table><thead><tr><th align=left>Level</th><th align=right>Boss</th></tr></thead><tbody><tr><td align=left><b>1</b></td><td align=right>Crab | big</td></tr><tr><td align=left>two<br>lines</td><td align=right></td></tr></tbody></table>');
});
test('links and pictures: only safe addresses', () => {
assert.equal(html(parseInline('[doc](https://x.org/a) [js](javascript:void) [data](data:text/html,x) <https://y.org> see https://z.org/p.')),
'<a href=https://x.org/a>doc</a> js data <a href=https://y.org>https://y.org</a> see <a href=https://z.org/p>https://z.org/p</a>.');
assert.equal(html(parseInline('![shot](attachment:shot.png) ![r](https://cdn/x.png) ![bad](javascript:x) ![svg](data:image/svg+xml;base64,PHN2Zz4=) ![p](attachment:../x)')),
'<img src=attachment:shot.png alt=shot> <img src=https://cdn/x.png alt=r> bad svg p');
assert.equal(safeHref(' JAVASCRIPT:alert(1)'), null);
assert.equal(safeSrc('data:image/png;base64,AAAA'), 'data:image/png;base64,AAAA');
});
test('raw HTML is text, never markup', () => {
assert.equal(html(parse('<script>alert(1)</script> <img src=x onerror=alert(1)> <span style="background:url(x)">x</span> <u onclick="x()">u</u>')),
'<p><script>alert(1)</script> <img src=x onerror=alert(1)> <span style="background:url(x)">x</span> <u onclick="x()">u</u></p>');
// only strings and allowed nodes come out
const walk = (ns) => ns.every((n) => typeof n === 'string' || (['p', 'b', 'i', 'u', 'span', 'img', 'a', 'br'].includes(n.tag) && walk(n.kids)));
assert.ok(walk(parse('<b>x</b> <i>y</i>')));
});
test('a plain text of the description for the tiles', () => {
assert.equal(plainText('# Title\nThe **boss** ![x](attachment:a.png)\n\n- one\n- two'), 'Title\nThe boss \n• one\n• two');
});