feat(wiki): add the reading site and its editor

The renderer escapes HTML before any transformation and drops
`javascript:` links. Pages are written by the team, but a renderer that
relies on that is one mistake away from being a hole.

The editor previews as you type: legal pages are long, and writing Markdown
blind is how broken tables end up published.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-23 22:31:42 +02:00
1 parent ce1c0f2910
commit 748a8c19e1
7 files changed
+1031

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
/**
* Markdown renderer for the wiki (M17).
*
* Deliberately small: headings, lists, tables, code, quotes, links, emphasis.
* Everything is HTML-escaped **before** any transformation runs, so a page can
* never inject markup — the pages are written by the team, but a renderer that
* relies on that is one mistake away from being a hole.
*/
const escape = (text) =>
String(text ?? '')
.replaceAll('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;');
export const slugify = (text) =>
String(text)
.toLowerCase()
.normalize('NFD')
.replace(/[̀-ͯ]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
/** Inline pass, applied to text that is already escaped. */
function inline(text) {
return text
.replace(/`([^`]+)`/g, '<code>$1</code>')
.replace(
/!\[([^\]]*)\]\(([^)\s]+)\)/g,
(_, alt, src) => `<img src="${src}" alt="${alt}" loading="lazy" />`,
)
.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, label, href) => {
// Only http(s) and in-page anchors: no `javascript:` sneaking through.
const safe = /^(https?:\/\/|\/|#)/.test(href) ? href : '#';
const external = safe.startsWith('http');
return `<a href="${safe}"${external ? ' target="_blank" rel="noreferrer noopener"' : ''}>${label}</a>`;
})
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
.replace(/(^|[^*])\*([^*\n]+)\*/g, '$1<em>$2</em>')
.replace(/~~([^~]+)~~/g, '<del>$1</del>');
}
const cells = (line) =>
line
.trim()
.replace(/^\||\|$/g, '')
.split('|')
.map((cell) => inline(cell.trim()));
/**
* Renders a page and returns `{ html, headings }`, the headings feeding the
* table of contents.
*/
export function render(markdown) {
const lines = escape(markdown).split('\n');
const out = [];
const headings = [];
let index = 0;
const closeList = (stack) => {
while (stack.length > 0) out.push(`</${stack.pop()}>`);
};
const listStack = [];
while (index < lines.length) {
const line = lines[index];
// Fenced code, kept verbatim.
if (/^\s*```/.test(line)) {
closeList(listStack);
const body = [];
index += 1;
while (index < lines.length && !/^\s*```/.test(lines[index])) body.push(lines[index++]);
index += 1;
out.push(`<pre><code>${body.join('\n')}</code></pre>`);
continue;
}
// Table: a header row followed by a separator row.
if (line.includes('|') && /^\s*\|?[\s:-]+\|[\s:|-]*$/.test(lines[index + 1] ?? '')) {
closeList(listStack);
const header = cells(line);
index += 2;
const rows = [];
while (index < lines.length && lines[index].includes('|') && lines[index].trim() !== '') {
rows.push(cells(lines[index++]));
}
out.push(
`<div class="table-wrap"><table class="table"><thead><tr>${header
.map((cell) => `<th>${cell}</th>`)
.join('')}</tr></thead><tbody>${rows
.map((row) => `<tr>${row.map((cell) => `<td>${cell}</td>`).join('')}</tr>`)
.join('')}</tbody></table></div>`,
);
continue;
}
const heading = /^(#{1,4})\s+(.*)$/.exec(line);
if (heading) {
closeList(listStack);
const level = heading[1].length;
const text = heading[2].trim();
const id = slugify(text);
if (level <= 3) headings.push({ id, text, level });
out.push(`<h${level} id="${id}">${inline(text)}</h${level}>`);
index += 1;
continue;
}
if (/^\s*(---|\*\*\*)\s*$/.test(line)) {
closeList(listStack);
out.push('<hr />');
index += 1;
continue;
}
const quote = /^&gt;\s?(.*)$/.exec(line);
if (quote) {
closeList(listStack);
const body = [quote[1]];
index += 1;
while (index < lines.length && /^&gt;\s?/.test(lines[index])) {
body.push(lines[index++].replace(/^&gt;\s?/, ''));
}
out.push(`<blockquote>${inline(body.join(' '))}</blockquote>`);
continue;
}
const bullet = /^\s*[-*]\s+(.*)$/.exec(line);
const ordered = /^\s*\d+\.\s+(.*)$/.exec(line);
if (bullet || ordered) {
const tag = bullet ? 'ul' : 'ol';
if (listStack.at(-1) !== tag) {
closeList(listStack);
out.push(`<${tag}>`);
listStack.push(tag);
}
out.push(`<li>${inline((bullet ?? ordered)[1])}</li>`);
index += 1;
continue;
}
if (line.trim() === '') {
closeList(listStack);
index += 1;
continue;
}
// Paragraph: consecutive non-empty lines are joined.
closeList(listStack);
const paragraph = [line];
index += 1;
while (
index < lines.length &&
lines[index].trim() !== '' &&
!/^\s*([-*]\s|\d+\.\s|#{1,4}\s|&gt;|```)/.test(lines[index]) &&
!lines[index].includes('|')
) {
paragraph.push(lines[index++]);
}
out.push(`<p>${inline(paragraph.join(' '))}</p>`);
}
closeList(listStack);
return { html: out.join('\n'), headings };
}