"use client"; import { tokenize, type TokenKind } from "@/lib/coding/highlight"; import type { CodingLanguage } from "@/lib/coding/types"; const KIND_COLOR: Record = { keyword: "#c792ea", string: "#c3e88d", comment: "#677a8e", number: "#f78c6c", plain: "var(--text-primary)", }; interface LineFragment { text: string; kind: TokenKind; } /** Split the token stream into per-line fragment arrays (keeps line numbers). */ function toLines(code: string, language: CodingLanguage): LineFragment[][] { const tokens = tokenize(code, language); const lines: LineFragment[][] = [[]]; for (const tok of tokens) { const parts = tok.text.split("\n"); parts.forEach((part, i) => { if (i > 0) lines.push([]); if (part.length > 0) { lines[lines.length - 1].push({ text: part, kind: tok.kind }); } }); } return lines; } export function CodeBlock({ code, language, testId = "code-block", }: { code: string; language: CodingLanguage; testId?: string; }) { const lines = toLines(code.replace(/\n$/, ""), language); return (
        
          {lines.map((frags, i) => (
            
{i + 1} {frags.length === 0 ? ( " " ) : ( frags.map((f, j) => ( {f.text} )) )}
))}
); }