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 | } |