|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { highlightCode } from "@/browser/utils/highlighting/highlightWorkerClient"; |
| 3 | +import { extractShikiLines } from "@/browser/utils/highlighting/shiki-shared"; |
| 4 | +import { useTheme } from "@/browser/contexts/ThemeContext"; |
| 5 | + |
| 6 | +interface HighlightedCodeProps { |
| 7 | + code: string; |
| 8 | + language: string; |
| 9 | + className?: string; |
| 10 | + showLineNumbers?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Renders code with syntax highlighting using Shiki (via web worker) |
| 15 | + * Falls back to plain text on first render or if highlighting fails |
| 16 | + */ |
| 17 | +export const HighlightedCode: React.FC<HighlightedCodeProps> = ({ |
| 18 | + code, |
| 19 | + language, |
| 20 | + className, |
| 21 | + showLineNumbers = false, |
| 22 | +}) => { |
| 23 | + const [highlightedLines, setHighlightedLines] = useState<string[] | null>(null); |
| 24 | + const { theme: themeMode } = useTheme(); |
| 25 | + |
| 26 | + const plainLines = code.split("\n").filter((line, i, arr) => i < arr.length - 1 || line !== ""); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + let cancelled = false; |
| 30 | + const theme = themeMode === "light" || themeMode === "solarized-light" ? "light" : "dark"; |
| 31 | + |
| 32 | + setHighlightedLines(null); |
| 33 | + |
| 34 | + async function highlight() { |
| 35 | + try { |
| 36 | + const html = await highlightCode(code, language, theme); |
| 37 | + if (!cancelled) { |
| 38 | + const lines = extractShikiLines(html); |
| 39 | + const filtered = lines.filter((l, i, a) => i < a.length - 1 || l.trim() !== ""); |
| 40 | + setHighlightedLines(filtered.length > 0 ? filtered : null); |
| 41 | + } |
| 42 | + } catch (error) { |
| 43 | + console.warn(`Failed to highlight ${language}:`, error); |
| 44 | + if (!cancelled) setHighlightedLines(null); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + void highlight(); |
| 49 | + return () => { |
| 50 | + cancelled = true; |
| 51 | + }; |
| 52 | + }, [code, language, themeMode]); |
| 53 | + |
| 54 | + const lines = highlightedLines ?? plainLines; |
| 55 | + |
| 56 | + if (showLineNumbers) { |
| 57 | + return ( |
| 58 | + <div className="code-block-container text-[11px]"> |
| 59 | + {lines.map((content, idx) => ( |
| 60 | + <React.Fragment key={idx}> |
| 61 | + <div className="line-number">{idx + 1}</div> |
| 62 | + {/* SECURITY AUDIT: dangerouslySetInnerHTML - Shiki escapes all content */} |
| 63 | + <div |
| 64 | + className="code-line" |
| 65 | + {...(highlightedLines |
| 66 | + ? { dangerouslySetInnerHTML: { __html: content } } |
| 67 | + : { children: content })} |
| 68 | + /> |
| 69 | + </React.Fragment> |
| 70 | + ))} |
| 71 | + </div> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (highlightedLines) { |
| 76 | + return ( |
| 77 | + <div |
| 78 | + className={className} |
| 79 | + dangerouslySetInnerHTML={{ __html: highlightedLines.join("\n") }} |
| 80 | + /> |
| 81 | + ); |
| 82 | + } |
| 83 | + return <div className={className}>{code}</div>; |
| 84 | +}; |
| 85 | + |
| 86 | +interface JsonHighlightProps { |
| 87 | + value: unknown; |
| 88 | + className?: string; |
| 89 | +} |
| 90 | + |
| 91 | +/** Renders a value as syntax-highlighted JSON with line numbers */ |
| 92 | +export const JsonHighlight: React.FC<JsonHighlightProps> = ({ value, className }) => { |
| 93 | + const jsonString = React.useMemo(() => { |
| 94 | + if (value === null || value === undefined) return "null"; |
| 95 | + if (typeof value === "string") { |
| 96 | + try { |
| 97 | + return JSON.stringify(JSON.parse(value), null, 2); |
| 98 | + } catch { |
| 99 | + return value; |
| 100 | + } |
| 101 | + } |
| 102 | + try { |
| 103 | + return JSON.stringify(value, null, 2); |
| 104 | + } catch { |
| 105 | + return "[Complex Object]"; |
| 106 | + } |
| 107 | + }, [value]); |
| 108 | + |
| 109 | + return ( |
| 110 | + <HighlightedCode code={jsonString} language="json" className={className} showLineNumbers /> |
| 111 | + ); |
| 112 | +}; |
0 commit comments