import React from 'react'; import { Check, Copy } from 'lucide-react'; interface CodeBlockProps { code: string; language?: string; filename?: string; } const CodeBlock: React.FC = ({ code, language = 'python', filename }) => { const [copied, setCopied] = React.useState(false); const handleCopy = () => { navigator.clipboard.writeText(code); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (
{/* Header */}
{filename && ( {filename} )}
{/* Code */}
                    
                        {code.split('\n').map((line, i) => (
                            
{i + 1} {highlightSyntax(line)}
))}
); }; // Simple syntax highlighting for Python/JSON const highlightSyntax = (line: string) => { // Comments if (line.trim().startsWith('#') || line.trim().startsWith('//')) { return {line}; } // Strings const stringRegex = /(['"])(.*?)\1/g; const parts = line.split(stringRegex); if (parts.length > 1) { return ( <> {parts.map((part, i) => { // Every 3rd part is the quote, then content, then quote again // This is a very naive implementation but works for simple marketing snippets if (i % 3 === 1) return "{part}"; // Content if (i % 3 === 2) return null; // Closing quote (handled by regex split logic usually, but here we just color content) // Keywords return {highlightKeywords(part)}; })} ); } return highlightKeywords(line); }; const highlightKeywords = (text: string) => { const keywords = ['def', 'class', 'return', 'import', 'from', 'if', 'else', 'for', 'in', 'True', 'False', 'None']; const words = text.split(' '); return ( <> {words.map((word, i) => { const isKeyword = keywords.includes(word.trim()); const isFunction = word.includes('('); return ( {isKeyword ? ( {word} ) : isFunction ? ( {word} ) : ( word )} {' '} ); })} ); }; export default CodeBlock;