import * as React from 'react'; type DiffLine = | { type: 'add'; text: string } | { type: 'remove'; text: string } | { type: 'context'; text: string } | { type: 'hunk'; text: string }; interface DiffProps { /** Optional file path shown above the diff (e.g. "src/foo.ts") */ filename?: string; /** Either provide pre-tokenized lines... */ lines?: DiffLine[]; /** ...or a raw unified-diff-style string with leading +/- / space markers */ children?: string; } const styles: Record = { add: { background: 'hsl(var(--add-bg))', color: 'hsl(var(--add-fg))', borderLeft: '3px solid hsl(var(--add-rule))', }, remove: { background: 'hsl(var(--remove-bg))', color: 'hsl(var(--remove-fg))', borderLeft: '3px solid hsl(var(--remove-rule))', }, context: { background: 'transparent', color: 'hsl(var(--muted-fg))', borderLeft: '3px solid transparent', }, hunk: { background: 'hsl(var(--accent))', color: 'hsl(var(--muted-fg))', borderLeft: '3px solid transparent', fontStyle: 'italic', }, }; const markers: Record = { add: '+', remove: '-', context: ' ', hunk: '@', }; function parseRaw(raw: string): DiffLine[] { // Trim only leading/trailing blank lines; preserve internal whitespace. const trimmed = raw.replace(/^\n+|\n+$/g, ''); return trimmed.split('\n').map((line): DiffLine => { if (line.startsWith('@@')) return { type: 'hunk', text: line }; if (line.startsWith('+')) return { type: 'add', text: line.slice(1) }; if (line.startsWith('-')) return { type: 'remove', text: line.slice(1) }; if (line.startsWith(' ')) return { type: 'context', text: line.slice(1) }; return { type: 'context', text: line }; }); } export function Diff({ filename, lines, children }: DiffProps) { const resolved: DiffLine[] = lines ?? (children ? parseRaw(children) : []); return (
{filename ? (
{filename}
) : null}
        {resolved.map((line, i) => (
          
{markers[line.type]} {line.text || '\u00a0'}
))}
); }