atagen

malice

public
Star0
Watch0
719f7cdd974e47e6ab6bdad3523014a8d0a64771
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/src/lib/code.ts

df3c0f2b3da4atagen2026-10-22 13:07 UTC1
export type CodeSegment =
df3c0f2b3da4atagen2026-10-22 13:07 UTC2
  | { kind: 'text'; text: string }
df3c0f2b3da4atagen2026-10-22 13:07 UTC3
  | { kind: 'inline'; text: string }
df3c0f2b3da4atagen2026-10-22 13:07 UTC4
  | { kind: 'block'; text: string; lang: string | null };
df3c0f2b3da4atagen2026-10-22 13:07 UTC5
df3c0f2b3da4atagen2026-10-22 13:07 UTC6
const FENCE = /```([^\s`]*)[^\S\n]*\n?([\s\S]*?)\n?```/g;
df3c0f2b3da4atagen2026-10-22 13:07 UTC7
const INLINE = /`([^`\n]+)`/g;
df3c0f2b3da4atagen2026-10-22 13:07 UTC8
df3c0f2b3da4atagen2026-10-22 13:07 UTC9
function splitInline(text: string, out: CodeSegment[]): void {
df3c0f2b3da4atagen2026-10-22 13:07 UTC10
  let last = 0;
df3c0f2b3da4atagen2026-10-22 13:07 UTC11
  for (const m of text.matchAll(INLINE)) {
df3c0f2b3da4atagen2026-10-22 13:07 UTC12
    if (m.index > last) out.push({ kind: 'text', text: text.slice(last, m.index) });
df3c0f2b3da4atagen2026-10-22 13:07 UTC13
    out.push({ kind: 'inline', text: m[1] });
df3c0f2b3da4atagen2026-10-22 13:07 UTC14
    last = m.index + m[0].length;
df3c0f2b3da4atagen2026-10-22 13:07 UTC15
  }
df3c0f2b3da4atagen2026-10-22 13:07 UTC16
  if (last < text.length) out.push({ kind: 'text', text: text.slice(last) });
df3c0f2b3da4atagen2026-10-22 13:07 UTC17
}
df3c0f2b3da4atagen2026-10-22 13:07 UTC18
df3c0f2b3da4atagen2026-10-22 13:07 UTC19
export function parseCode(text: string): CodeSegment[] {
df3c0f2b3da4atagen2026-10-22 13:07 UTC20
  const out: CodeSegment[] = [];
df3c0f2b3da4atagen2026-10-22 13:07 UTC21
  if (!text.includes('`')) {
df3c0f2b3da4atagen2026-10-22 13:07 UTC22
    if (text) out.push({ kind: 'text', text });
df3c0f2b3da4atagen2026-10-22 13:07 UTC23
    return out;
df3c0f2b3da4atagen2026-10-22 13:07 UTC24
  }
df3c0f2b3da4atagen2026-10-22 13:07 UTC25
  let last = 0;
df3c0f2b3da4atagen2026-10-22 13:07 UTC26
  for (const m of text.matchAll(FENCE)) {
df3c0f2b3da4atagen2026-10-22 13:07 UTC27
    splitInline(text.slice(last, m.index), out);
df3c0f2b3da4atagen2026-10-22 13:07 UTC28
    out.push({ kind: 'block', text: m[2], lang: m[1] || null });
df3c0f2b3da4atagen2026-10-22 13:07 UTC29
    last = m.index + m[0].length;
df3c0f2b3da4atagen2026-10-22 13:07 UTC30
    if (text[last] === '\n') last += 1;
df3c0f2b3da4atagen2026-10-22 13:07 UTC31
  }
df3c0f2b3da4atagen2026-10-22 13:07 UTC32
  splitInline(text.slice(last), out);
df3c0f2b3da4atagen2026-10-22 13:07 UTC33
  return out;
df3c0f2b3da4atagen2026-10-22 13:07 UTC34
}
df3c0f2b3da4atagen2026-10-22 13:07 UTC35
df3c0f2b3da4atagen2026-10-22 13:07 UTC36
export function hasCode(segments: CodeSegment[]): boolean {
df3c0f2b3da4atagen2026-10-22 13:07 UTC37
  return segments.some((s) => s.kind !== 'text');
df3c0f2b3da4atagen2026-10-22 13:07 UTC38
}