// Pure, DOM-free CommonMark subset for assistant prose (C7), ported from the // Android client's ui/chat/Markdown.kt so the browser renders exactly the same // blocks and inline spans. The brokered protocol carries assistant text as // markdown, and the reference experience renders it: // paragraphs, `#`..`######` headings, fenced code (``` / ~~~, language label // else `code`), block quotes, bullet and ordered lists, `|` tables with a // `---` delimiter row, thematic rules, and inline code / bold / italic / // links / bare http(s) autolinks. // // Anything outside the subset renders as plain text and is never dropped. There // is deliberately no HTML, no reference links, no nested lists and no // strikethrough — the same subset the app implements. // // The parser is pure (no DOM, no transport) so it is unit-testable; the caller // turns the same blocks into text nodes with textContent, never innerHTML. /** * Decodes the HTML entities an assistant message can carry. The server's own * session export escapes text with Go's `html.EscapeString`, so a message that * quotes an export arrives with `'`, `"`, `&` and friends still in * it. The common named entities plus numeric decimal and hex references are * decoded; an unknown or malformed reference is left exactly as written so no * visible text is invented. A single left-to-right pass never re-decodes an * entity produced by decoding another (`&lt;` stays `<`). */ const NAMED_ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: "\u00A0", hellip: "\u2026", mdash: "\u2014", ndash: "\u2013", lsquo: "\u2018", rsquo: "\u2019", ldquo: "\u201C", rdquo: "\u201D", }; const HTML_ENTITY = /&(#x[0-9A-Fa-f]+|#[0-9]+|[A-Za-z][A-Za-z0-9]*);/g; export function decodeHtmlEntities(text) { if (!text.includes("&")) return text; return text.replace(HTML_ENTITY, (match, body) => { if (Object.prototype.hasOwnProperty.call(NAMED_ENTITIES, body)) return NAMED_ENTITIES[body]; return numericReference(body) ?? match; }); } /** The character one numeric entity's body (`#27`/`#x1b`) names, or null. */ function numericReference(body) { let codePoint = null; if (body.startsWith("#x") || body.startsWith("#X")) { if (/^#x[0-9A-Fa-f]+$/.test(body)) codePoint = parseInt(body.slice(2), 16); } else if (body.startsWith("#")) { if (/^#[0-9]+$/.test(body)) codePoint = parseInt(body.slice(1), 10); } if (codePoint == null || !Number.isFinite(codePoint)) return null; if (codePoint < 1 || codePoint > 0x10ffff) return null; return String.fromCodePoint(codePoint); } // ---- Blocks ----------------------------------------------------------------- /** * Parses [source] into the block list the renderer walks. Never throws. Blocks: * {kind:"heading", level, text} {kind:"paragraph", text} * {kind:"code", language, code} {kind:"quote", lines} * {kind:"list", ordered, items} {kind:"table", header, rows} * {kind:"rule"} */ export function parseMarkdown(source) { const lines = decodeHtmlEntities(String(source ?? "")) .replace(/\r\n/g, "\n") .replace(/\r/g, "\n") .split("\n"); const blocks = []; let i = 0; while (i < lines.length) { const line = lines[i]; const trimmed = line.trim(); if (trimmed === "") { i += 1; continue; } const fence = fenceRun(trimmed); if (fence != null) { const language = trimmed.slice(fence.length).trim().split(" ")[0]; const code = []; i += 1; while (i < lines.length && fenceRun(lines[i].trim()) == null) { code.push(lines[i]); i += 1; } if (i < lines.length) i += 1; // consume the closing fence blocks.push({ kind: "code", language, code: code.join("\n") }); continue; } const level = headingLevel(trimmed); if (level > 0) { blocks.push({ kind: "heading", level, text: trimEndHashes(trimmed.slice(level).trim()) }); i += 1; continue; } if (isRule(trimmed)) { blocks.push({ kind: "rule" }); i += 1; continue; } if (trimmed.startsWith(">")) { const quote = []; while (i < lines.length && lines[i].trim().startsWith(">")) { quote.push(lines[i].trim().replace(/^>/, "").replace(/^ /, "").replace(/\s+$/, "")); i += 1; } blocks.push({ kind: "quote", lines: quote }); continue; } if (isTableStart(lines, i)) { const header = splitRow(lines[i]); i += 2; // header + delimiter row const rows = []; while (i < lines.length && lines[i].includes("|") && lines[i].trim() !== "") { rows.push(fitRow(splitRow(lines[i]), header.length)); i += 1; } blocks.push({ kind: "table", header, rows }); continue; } const marker = listMarker(line); if (marker != null) { const ordered = marker.ordered; const items = []; while (i < lines.length && listMarker(lines[i])?.ordered === ordered) { items.push(listMarker(lines[i]).text); i += 1; } blocks.push({ kind: "list", ordered, items }); continue; } const paragraph = [line.trim()]; i += 1; while (i < lines.length && lines[i].trim() !== "" && !startsBlock(lines, i)) { paragraph.push(lines[i].trim()); i += 1; } blocks.push({ kind: "paragraph", text: paragraph.join(" ") }); } return blocks; } /** The fence run (` ``` ` / `~~~`) a line opens or closes with, or null. */ function fenceRun(trimmed) { const first = trimmed[0]; if (first !== "`" && first !== "~") return null; let run = ""; for (const char of trimmed) { if (char !== first) break; run += char; } return run.length >= 3 ? run : null; } function headingLevel(trimmed) { let hashes = 0; while (hashes < trimmed.length && trimmed[hashes] === "#") hashes += 1; if (hashes === 0 || hashes > 6) return 0; return trimmed.length === hashes || trimmed[hashes] === " " ? hashes : 0; } function trimEndHashes(text) { return text.replace(/#+$/, ""); } function isRule(trimmed) { if (trimmed.length < 3) return false; const first = trimmed[0]; if (!"-*_".includes(first)) return false; for (const char of trimmed) if (char !== first) return false; return true; } /** True when [lines] at [index] is a table header followed by a `---` delimiter row. */ function isTableStart(lines, index) { if (index + 1 >= lines.length || !lines[index].includes("|")) return false; const delimiter = splitRow(lines[index + 1]); if (delimiter.length === 0 || splitRow(lines[index]).length < 2) return false; return delimiter.every( (cell) => cell.length > 0 && [...cell].every((char) => char === "-" || char === ":") && cell.includes("-"), ); } /** Splits a table row on unescaped `|`, dropping the leading/trailing empty cell. */ function splitRow(line) { return line .trim() .replace(/^\|/, "") .replace(/\|$/, "") .split("|") .map((cell) => cell.replace(/\\\|/g, "|").trim()); } function fitRow(row, size) { return Array.from({ length: size }, (_, index) => row[index] ?? ""); } const ORDERED_ITEM = /^(\d{1,9})[.)]\s+(.*)$/; const BULLET_ITEM = /^([-*+])\s+(.*)$/; /** The ordered flag and item text of a list line, or null for a non-item line. */ function listMarker(line) { const trimmed = line.replace(/^\s+/, ""); const orderedMatch = ORDERED_ITEM.exec(trimmed); if (orderedMatch) return { ordered: true, text: orderedMatch[2] }; const bulletMatch = BULLET_ITEM.exec(trimmed); if (bulletMatch) return { ordered: false, text: bulletMatch[2] }; return null; } /** True when [lines] at [index] begins a block that must interrupt a paragraph. */ function startsBlock(lines, index) { const trimmed = lines[index].trim(); return ( fenceRun(trimmed) != null || headingLevel(trimmed) > 0 || isRule(trimmed) || trimmed.startsWith(">") || listMarker(lines[index]) != null || isTableStart(lines, index) ); } // ---- Inline ----------------------------------------------------------------- const WORD_CHAR = /[\p{L}\p{N}]/u; /** * Parses inline markup in [text] into a flat span tree. Handles `` `code` ``, * `**bold**` / `__bold__`, `*italic*` / `_italic_`, `[label](url)` and bare * `http(s)://` autolinks; everything else stays plain text. Underscore emphasis * requires word boundaries so `snake_case` is never italicised. */ export function parseInline(text) { const source = String(text ?? ""); const spans = []; let buffer = ""; let i = 0; const flush = () => { if (buffer.length > 0) { spans.push({ kind: "plain", text: buffer }); buffer = ""; } }; while (i < source.length) { const c = source[i]; if (c === "`") { const end = source.indexOf("`", i + 1); if (end > i) { flush(); spans.push({ kind: "code", text: source.slice(i + 1, end) }); i = end + 1; } else { buffer += c; i += 1; } } else if (c === "*" || c === "_") { const doubled = i + 1 < source.length && source[i + 1] === c; const marker = doubled ? c + c : c; const boundOk = c !== "_" || i === 0 || !WORD_CHAR.test(source[i - 1]); const end = boundOk ? source.indexOf(marker, i + marker.length) : -1; const closes = end > i + marker.length && (c !== "_" || end + 1 >= source.length || !WORD_CHAR.test(source[end + 1])); if (closes) { flush(); const inner = parseInline(source.slice(i + marker.length, end)); spans.push(doubled ? { kind: "strong", spans: inner } : { kind: "emphasis", spans: inner }); i = end + marker.length; } else { buffer += c; i += 1; } } else if (c === "[") { const close = source.indexOf("]", i + 1); if (close > i && close + 1 < source.length && source[close + 1] === "(") { const urlEnd = source.indexOf(")", close + 2); const url = urlEnd > close ? source.slice(close + 2, urlEnd).trim() : ""; if (url !== "") { flush(); spans.push({ kind: "link", spans: parseInline(source.slice(i + 1, close)), url }); i = urlEnd + 1; } else { buffer += c; i += 1; } } else { buffer += c; i += 1; } } else { const url = autolinkAt(source, i); if (url != null) { flush(); spans.push({ kind: "link", spans: [{ kind: "plain", text: url }], url }); i += url.length; } else { buffer += c; i += 1; } } } flush(); return spans; } /** A bare `http(s)://` URL starting exactly at [index], trailing punctuation trimmed. */ function autolinkAt(text, index) { if (!text.startsWith("http://", index) && !text.startsWith("https://", index)) return null; let end = index; while (end < text.length && !/\s/.test(text[end]) && !"<>\"'".includes(text[end])) end += 1; let url = text.slice(index, end); while (url.length > 0 && ".,;:!?)]}".includes(url[url.length - 1])) url = url.slice(0, -1); return url.length > "https://".length ? url : null; } /** The label a fenced code block shows: its language, or `code` when unnamed. */ export function codeBlockLabel(language) { const value = String(language ?? "").trim(); return value === "" ? "code" : value; }