cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 1 | import type { Mention } from './mentions'; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 2 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 3 | export type CodeSegment = |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 4 | | { kind: 'text'; text: string } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 5 | | { kind: 'inline'; text: string } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 6 | | { kind: 'block'; text: string; lang: string | null }; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 7 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 8 | const FENCE = /```([^\s`]*)[^\S\n]*\n?([\s\S]*?)\n?```/g; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 9 | const INLINE = /`([^`\n]+)`/g; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 10 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 11 | function splitInline(text: string, out: CodeSegment[]): void { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 12 | let last = 0; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 13 | for (const m of text.matchAll(INLINE)) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 14 | if (m.index > last) out.push({ kind: 'text', text: text.slice(last, m.index) }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 15 | out.push({ kind: 'inline', text: m[1] }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 16 | last = m.index + m[0].length; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 17 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 18 | if (last < text.length) out.push({ kind: 'text', text: text.slice(last) }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 19 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 20 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 21 | export function parseCode(text: string): CodeSegment[] { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 22 | const out: CodeSegment[] = []; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 23 | if (!text.includes('`')) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 24 | if (text) out.push({ kind: 'text', text }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 25 | return out; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 26 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 27 | let last = 0; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 28 | for (const m of text.matchAll(FENCE)) { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 29 | splitInline(text.slice(last, m.index), out); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 30 | out.push({ kind: 'block', text: m[2], lang: m[1] || null }); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 31 | last = m.index + m[0].length; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 32 | if (text[last] === '\n') last += 1; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 33 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 34 | splitInline(text.slice(last), out); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 35 | return out; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 36 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 37 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 38 | export function hasCode(segments: CodeSegment[]): boolean { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 39 | return segments.some((s) => s.kind !== 'text'); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 40 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 41 | |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 42 | function escapeHtml(text: string): string { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 43 | return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 44 | } |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 45 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 46 | function escapeRegExp(text: string): string { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 47 | return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 48 | } |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 49 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 50 | function textToHtml(text: string, pattern: RegExp | null, byText: Map<string, string>): string { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 51 | if (!pattern) return escapeHtml(text).replace(/\n/g, '<br>'); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 52 | let out = ''; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 53 | let last = 0; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 54 | for (const m of text.matchAll(pattern)) { |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 55 | out += escapeHtml(text.slice(last, m.index)).replace(/\n/g, '<br>'); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 56 | const userId = byText.get(m[0]) ?? m[0]; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 57 | out += `<a href="https://matrix.to/#/${escapeHtml(userId)}">${escapeHtml(m[0])}</a>`; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 58 | last = m.index + m[0].length; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 59 | } |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 60 | return out + escapeHtml(text.slice(last)).replace(/\n/g, '<br>'); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 61 | } |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 62 | |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 63 | /** Matrix `formatted_body` for a plain body, or null when it has no code or mentions to format. */ |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 64 | export function codeToHtml(body: string, mentions: Mention[] = []): string | null { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 65 | const segments = parseCode(body); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 66 | const byText = new Map(mentions.map((m) => [m.text, m.user_id])); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 67 | const texts = [...byText.keys()].sort((a, b) => b.length - a.length); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 68 | const pattern = texts.length > 0 ? new RegExp(texts.map(escapeRegExp).join('|'), 'g') : null; |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 69 | const linked = pattern !== null && segments.some((s) => s.kind === 'text' && new RegExp(pattern.source).test(s.text)); |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 70 | if (!hasCode(segments) && !linked) return null; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 71 | return segments |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 72 | .map((s) => { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 73 | if (s.kind === 'inline') return `<code>${escapeHtml(s.text)}</code>`; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 74 | if (s.kind === 'block') { |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 75 | const cls = s.lang ? ` class="language-${escapeHtml(s.lang)}"` : ''; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 76 | return `<pre><code${cls}>${escapeHtml(s.text)}</code></pre>`; |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 77 | } |
cf23dbf4e77c | atagen | 2026-10-23 06:36 UTC | 78 | return textToHtml(s.text, pattern, byText); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 79 | }) |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 80 | .join(''); |
df3c0f2b3da4 | atagen | 2026-10-22 13:07 UTC | 81 | } |