|
| 1 | +import { HTMLAttributes, useState } from 'react'; |
| 2 | +import { motion, AnimatePresence } from 'framer-motion'; |
| 3 | +import { cn } from '@/lib/utils'; |
| 4 | +import { syntax, codeBg } from '@/lib/python-theme'; |
| 5 | +import { lineHighlightVariants } from '@/lib/animations'; |
| 6 | +import { Check, Copy, ExternalLink } from 'lucide-react'; |
| 7 | + |
| 8 | +interface CodeBlockProps extends HTMLAttributes<HTMLDivElement> { |
| 9 | + code: string; |
| 10 | + language?: 'python' | 'text'; |
| 11 | + filename?: string; |
| 12 | + lineStart?: number; |
| 13 | + highlightLines?: number[]; |
| 14 | + showLineNumbers?: boolean; |
| 15 | + maxHeight?: string; |
| 16 | + onCopy?: () => void; |
| 17 | + githubUrl?: string; |
| 18 | +} |
| 19 | + |
| 20 | +type Token = { type: string; value: string }; |
| 21 | + |
| 22 | +// Dead simple tokenizer - not trying to be a full parser, just good enough for display |
| 23 | +function tokenizePython(code: string): Token[] { |
| 24 | + const tokens: Token[] = []; |
| 25 | + |
| 26 | + // Order matters here - more specific patterns first |
| 27 | + const patterns: [string, RegExp][] = [ |
| 28 | + ['comment', /^#.*/], |
| 29 | + ['docstring', /^("""[\s\S]*?"""|'''[\s\S]*?''')/], |
| 30 | + ['fstring', /^f(['"])((?:\\.|(?!\1)[^\\])*)\1/], |
| 31 | + ['string', /^(['"])((?:\\.|(?!\1)[^\\])*)\1/], |
| 32 | + ['decorator', /^@[\w.]+/], |
| 33 | + ['keyword', /^\b(def|class|import|from|return|if|elif|else|for|while|try|except|finally|with|as|yield|lambda|pass|break|continue|raise|assert|global|nonlocal|del|in|not|and|or|is|True|False|None|async|await)\b/], |
| 34 | + ['builtin', /^\b(print|len|range|str|int|float|list|dict|set|tuple|bool|type|isinstance|hasattr|getattr|setattr|open|input|super|self|cls)\b/], |
| 35 | + ['number', /^\b\d+(\.\d+)?\b/], |
| 36 | + ['function', /^\b([a-zA-Z_][a-zA-Z0-9_]*)(?=\s*\()/], |
| 37 | + ['className', /^\b([A-Z][a-zA-Z0-9_]*)\b/], |
| 38 | + ['parameter', /^\b([a-zA-Z_][a-zA-Z0-9_]*)(?=\s*[=:])/], |
| 39 | + ['operator', /^(==|!=|<=|>=|<|>|\+|-|\*|\/|%|\*\*|=|\+=|-=|\|\||&&)/], |
| 40 | + ['punctuation', /^[()[\]{}:,.;]/], |
| 41 | + ['variable', /^[a-zA-Z_][a-zA-Z0-9_]*/], |
| 42 | + ['whitespace', /^\s+/], |
| 43 | + ]; |
| 44 | + |
| 45 | + let remaining = code; |
| 46 | + while (remaining.length > 0) { |
| 47 | + let matched = false; |
| 48 | + for (const [type, pattern] of patterns) { |
| 49 | + const match = remaining.match(pattern); |
| 50 | + if (match) { |
| 51 | + tokens.push({ type, value: match[0] }); |
| 52 | + remaining = remaining.slice(match[0].length); |
| 53 | + matched = true; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + if (!matched) { |
| 58 | + tokens.push({ type: 'text', value: remaining[0] }); |
| 59 | + remaining = remaining.slice(1); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return tokens; |
| 64 | +} |
| 65 | + |
| 66 | +const tokenColors: Record<string, string> = { |
| 67 | + keyword: syntax.keyword, |
| 68 | + builtin: syntax.builtin, |
| 69 | + function: syntax.function, |
| 70 | + className: syntax.className, |
| 71 | + decorator: syntax.decorator, |
| 72 | + string: syntax.string, |
| 73 | + fstring: syntax.string, |
| 74 | + docstring: syntax.docstring, |
| 75 | + comment: syntax.comment, |
| 76 | + number: syntax.number, |
| 77 | + parameter: syntax.parameter, |
| 78 | + operator: syntax.operator, |
| 79 | + punctuation: syntax.punctuation, |
| 80 | + variable: syntax.variable, |
| 81 | +}; |
| 82 | + |
| 83 | +// Python code block with syntax highlighting and copy button |
| 84 | +export function CodeBlock({ |
| 85 | + code, |
| 86 | + language = 'python', |
| 87 | + filename, |
| 88 | + lineStart = 1, |
| 89 | + highlightLines = [], |
| 90 | + showLineNumbers = true, |
| 91 | + maxHeight = '400px', |
| 92 | + onCopy, |
| 93 | + githubUrl, |
| 94 | + className, |
| 95 | + ...props |
| 96 | +}: CodeBlockProps) { |
| 97 | + const [copied, setCopied] = useState(false); |
| 98 | + const lines = code.split('\n'); |
| 99 | + |
| 100 | + const handleCopy = async () => { |
| 101 | + await navigator.clipboard.writeText(code); |
| 102 | + setCopied(true); |
| 103 | + onCopy?.(); |
| 104 | + setTimeout(() => setCopied(false), 2000); |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div |
| 109 | + className={cn('rounded-lg overflow-hidden border border-white/[0.08]', className)} |
| 110 | + style={{ backgroundColor: codeBg.elevated }} |
| 111 | + {...props} |
| 112 | + > |
| 113 | + {(filename || githubUrl) && ( |
| 114 | + <div |
| 115 | + className="px-4 py-2 flex items-center justify-between border-b border-white/[0.08]" |
| 116 | + style={{ backgroundColor: codeBg.primary }} |
| 117 | + > |
| 118 | + {filename && ( |
| 119 | + <span className="text-sm text-text-secondary font-mono">{filename}</span> |
| 120 | + )} |
| 121 | + <div className="flex items-center gap-2"> |
| 122 | + <button |
| 123 | + onClick={handleCopy} |
| 124 | + className="p-1.5 rounded hover:bg-white/[0.05] transition-colors text-text-muted hover:text-text-primary" |
| 125 | + title="Copy code" |
| 126 | + > |
| 127 | + <AnimatePresence mode="wait"> |
| 128 | + <motion.div |
| 129 | + key={copied ? 'check' : 'copy'} |
| 130 | + initial={{ scale: 0.5, opacity: 0 }} |
| 131 | + animate={{ scale: 1, opacity: 1 }} |
| 132 | + exit={{ scale: 0.5, opacity: 0 }} |
| 133 | + > |
| 134 | + {copied ? ( |
| 135 | + <Check size={16} className="text-green-500" /> |
| 136 | + ) : ( |
| 137 | + <Copy size={16} /> |
| 138 | + )} |
| 139 | + </motion.div> |
| 140 | + </AnimatePresence> |
| 141 | + </button> |
| 142 | + {githubUrl && ( |
| 143 | + <a |
| 144 | + href={githubUrl} |
| 145 | + target="_blank" |
| 146 | + rel="noopener noreferrer" |
| 147 | + className="p-1.5 rounded hover:bg-white/[0.05] transition-colors text-text-muted hover:text-text-primary" |
| 148 | + title="View on GitHub" |
| 149 | + > |
| 150 | + <ExternalLink size={16} /> |
| 151 | + </a> |
| 152 | + )} |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + )} |
| 156 | + |
| 157 | + <div className="overflow-auto scrollbar-thin" style={{ maxHeight }}> |
| 158 | + <pre className="p-4 text-sm font-mono leading-relaxed"> |
| 159 | + <code> |
| 160 | + {lines.map((line, index) => { |
| 161 | + const lineNumber = lineStart + index; |
| 162 | + const isHighlighted = highlightLines.includes(lineNumber); |
| 163 | + const tokens = language === 'python' |
| 164 | + ? tokenizePython(line) |
| 165 | + : [{ type: 'text', value: line }]; |
| 166 | + |
| 167 | + return ( |
| 168 | + <motion.div |
| 169 | + key={index} |
| 170 | + className={cn('flex', isHighlighted && 'rounded')} |
| 171 | + variants={lineHighlightVariants} |
| 172 | + initial="idle" |
| 173 | + whileHover="hover" |
| 174 | + style={{ |
| 175 | + backgroundColor: isHighlighted ? syntax.matchHighlight : undefined, |
| 176 | + }} |
| 177 | + > |
| 178 | + {showLineNumbers && ( |
| 179 | + <span |
| 180 | + className="select-none pr-4 text-right min-w-[3rem]" |
| 181 | + style={{ |
| 182 | + color: isHighlighted ? syntax.lineNumberActive : syntax.lineNumber, |
| 183 | + }} |
| 184 | + > |
| 185 | + {lineNumber} |
| 186 | + </span> |
| 187 | + )} |
| 188 | + <span className="flex-1"> |
| 189 | + {tokens.map((token, i) => ( |
| 190 | + <span key={i} style={{ color: tokenColors[token.type] || syntax.variable }}> |
| 191 | + {token.value} |
| 192 | + </span> |
| 193 | + ))} |
| 194 | + {line === '' && '\u200B'} |
| 195 | + </span> |
| 196 | + </motion.div> |
| 197 | + ); |
| 198 | + })} |
| 199 | + </code> |
| 200 | + </pre> |
| 201 | + </div> |
| 202 | + </div> |
| 203 | + ); |
| 204 | +} |
| 205 | + |
| 206 | +export default CodeBlock; |
0 commit comments