df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 1 | export type CodeSegment = |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 2 | | { kind: 'text'; text: string } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 3 | | { kind: 'inline'; text: string } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 4 | | { kind: 'block'; text: string; lang: string | null }; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 5 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 6 | const FENCE = /```([^\s`]*)[^\S\n]*\n?([\s\S]*?)\n?```/g; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 7 | const INLINE = /`([^`\n]+)`/g; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 8 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 9 | function splitInline(text: string, out: CodeSegment[]): void { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 10 | let last = 0; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 11 | for (const m of text.matchAll(INLINE)) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 12 | if (m.index > last) out.push({ kind: 'text', text: text.slice(last, m.index) }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 13 | out.push({ kind: 'inline', text: m[1] }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 14 | last = m.index + m[0].length; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 15 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 16 | if (last < text.length) out.push({ kind: 'text', text: text.slice(last) }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 17 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 18 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 19 | export function parseCode(text: string): CodeSegment[] { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 20 | const out: CodeSegment[] = []; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 21 | if (!text.includes('`')) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 22 | if (text) out.push({ kind: 'text', text }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 23 | return out; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 24 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 25 | let last = 0; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 26 | for (const m of text.matchAll(FENCE)) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 27 | splitInline(text.slice(last, m.index), out); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 28 | out.push({ kind: 'block', text: m[2], lang: m[1] || null }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 29 | last = m.index + m[0].length; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 30 | if (text[last] === '\n') last += 1; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 31 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 32 | splitInline(text.slice(last), out); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 33 | return out; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 34 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 35 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 36 | export function hasCode(segments: CodeSegment[]): boolean { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 37 | return segments.some((s) => s.kind !== 'text'); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 38 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 39 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 40 | function escapeHtml(text: string): string { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 41 | return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 42 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 43 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 44 | /** Matrix `formatted_body` for a plain body, or null when it has no code to format. */ |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 45 | export function codeToHtml(body: string): string | null { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 46 | const segments = parseCode(body); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 47 | if (!hasCode(segments)) return null; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 48 | return segments |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 49 | .map((s) => { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 50 | if (s.kind === 'inline') return `<code>${escapeHtml(s.text)}</code>`; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 51 | if (s.kind === 'block') { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 52 | const cls = s.lang ? ` class="language-${escapeHtml(s.lang)}"` : ''; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 53 | return `<pre><code${cls}>${escapeHtml(s.text)}</code></pre>`; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 54 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 55 | return escapeHtml(s.text).replace(/\n/g, '<br>'); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 56 | }) |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 57 | .join(''); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 58 | } |