|
| 1 | +import { useMemo } from "react"; |
| 2 | + |
| 3 | +interface Token { |
| 4 | + type: "key" | "string" | "number" | "boolean" | "null" | "punctuation"; |
| 5 | + text: string; |
| 6 | +} |
| 7 | + |
| 8 | +const TOKEN_COLORS: Record<Token["type"], string> = { |
| 9 | + key: "var(--info)", |
| 10 | + string: "var(--success)", |
| 11 | + number: "var(--warning)", |
| 12 | + boolean: "var(--accent)", |
| 13 | + null: "var(--accent)", |
| 14 | + punctuation: "var(--text-muted)", |
| 15 | +}; |
| 16 | + |
| 17 | +// Tokenize a pre-formatted JSON string into colored spans |
| 18 | +function tokenize(json: string): Token[] { |
| 19 | + const tokens: Token[] = []; |
| 20 | + // Match JSON tokens: strings, numbers, booleans, null, punctuation |
| 21 | + const re = /("(?:[^"\\]|\\.)*")\s*:|("(?:[^"\\]|\\.)*")|(-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)\b|(true|false)\b|(null)\b|([{}[\]:,])/g; |
| 22 | + let lastIndex = 0; |
| 23 | + let m: RegExpExecArray | null; |
| 24 | + |
| 25 | + while ((m = re.exec(json)) !== null) { |
| 26 | + // Whitespace between tokens |
| 27 | + if (m.index > lastIndex) { |
| 28 | + tokens.push({ type: "punctuation", text: json.slice(lastIndex, m.index) }); |
| 29 | + } |
| 30 | + |
| 31 | + if (m[1] !== undefined) { |
| 32 | + // Key (string followed by colon) |
| 33 | + tokens.push({ type: "key", text: m[1] }); |
| 34 | + // Find the colon after the key |
| 35 | + const colonIdx = json.indexOf(":", m.index + m[1].length); |
| 36 | + if (colonIdx !== -1) { |
| 37 | + // Add any whitespace between key and colon |
| 38 | + if (colonIdx > m.index + m[1].length) { |
| 39 | + tokens.push({ type: "punctuation", text: json.slice(m.index + m[1].length, colonIdx) }); |
| 40 | + } |
| 41 | + tokens.push({ type: "punctuation", text: ":" }); |
| 42 | + re.lastIndex = colonIdx + 1; |
| 43 | + } |
| 44 | + } else if (m[2] !== undefined) { |
| 45 | + tokens.push({ type: "string", text: m[2] }); |
| 46 | + } else if (m[3] !== undefined) { |
| 47 | + tokens.push({ type: "number", text: m[3] }); |
| 48 | + } else if (m[4] !== undefined) { |
| 49 | + tokens.push({ type: "boolean", text: m[4] }); |
| 50 | + } else if (m[5] !== undefined) { |
| 51 | + tokens.push({ type: "null", text: m[5] }); |
| 52 | + } else if (m[6] !== undefined) { |
| 53 | + tokens.push({ type: "punctuation", text: m[6] }); |
| 54 | + } |
| 55 | + |
| 56 | + lastIndex = re.lastIndex; |
| 57 | + } |
| 58 | + |
| 59 | + // Trailing whitespace |
| 60 | + if (lastIndex < json.length) { |
| 61 | + tokens.push({ type: "punctuation", text: json.slice(lastIndex) }); |
| 62 | + } |
| 63 | + |
| 64 | + return tokens; |
| 65 | +} |
| 66 | + |
| 67 | +interface Props { |
| 68 | + json: string; |
| 69 | + className?: string; |
| 70 | + style?: React.CSSProperties; |
| 71 | +} |
| 72 | + |
| 73 | +export default function JsonHighlight({ json, className, style }: Props) { |
| 74 | + const tokens = useMemo(() => tokenize(json), [json]); |
| 75 | + |
| 76 | + return ( |
| 77 | + <pre className={className} style={style}> |
| 78 | + {tokens.map((t, i) => ( |
| 79 | + <span key={i} style={{ color: TOKEN_COLORS[t.type] }}> |
| 80 | + {t.text} |
| 81 | + </span> |
| 82 | + ))} |
| 83 | + </pre> |
| 84 | + ); |
| 85 | +} |
0 commit comments