import { CopyButton } from './CopyButton'; import { cn } from '@/lib/utils/utils'; interface JsonHighlightProps { json: string; textColor?: string; className?: string; } export function JsonHighlight({ json, textColor, className }: JsonHighlightProps) { const highlightJson = (str: string, textColor?: string) => { // Tokenize JSON string const tokens = str.split(/("(?:[^"\\]|\\.)*")|(\s+)|([:,{}[\]])/g).filter(Boolean); return tokens.map((token, index) => { // String values (including keys) if (token.startsWith('"') && token.endsWith('"')) { // Check if this is a key (next non-whitespace token is ':') let isKey = false; for (let i = index + 1; i < tokens.length; i++) { if (tokens[i].trim()) { isKey = tokens[i] === ':'; break; } } if (isKey) { return ( {token} ); } else { return ( {token} ); } } // Punctuation if (/^[:,{}[\]]$/.test(token)) { return ( {token} ); } // Whitespace and other return ( {token} ); }); }; return (
        {highlightJson(json, textColor)}
      
); }