|
1 | 1 | 'use client' |
2 | 2 |
|
3 | | -import { Code } from '@/components/emcn' |
| 3 | +import { useCallback, useState } from 'react' |
| 4 | +import { Check, Copy } from 'lucide-react' |
| 5 | +import { highlight, languages } from 'prismjs' |
| 6 | +import 'prismjs/components/prism-typescript' |
| 7 | +import 'prismjs/components/prism-javascript' |
| 8 | +import 'prismjs/components/prism-json' |
| 9 | +import 'prismjs/components/prism-python' |
| 10 | +import 'prismjs/components/prism-bash' |
| 11 | +import 'prismjs/components/prism-css' |
| 12 | +import 'prismjs/components/prism-yaml' |
| 13 | +import 'prismjs/components/prism-markdown' |
4 | 14 |
|
5 | 15 | interface CodeBlockProps { |
6 | 16 | code: string |
7 | | - language: 'javascript' | 'json' | 'python' |
| 17 | + language: string |
| 18 | + filename?: string |
8 | 19 | } |
9 | 20 |
|
10 | | -export function CodeBlock({ code, language }: CodeBlockProps) { |
| 21 | +const LANG_MAP: Record<string, string> = { |
| 22 | + js: 'javascript', |
| 23 | + jsx: 'javascript', |
| 24 | + ts: 'typescript', |
| 25 | + tsx: 'typescript', |
| 26 | + typescript: 'typescript', |
| 27 | + javascript: 'javascript', |
| 28 | + json: 'json', |
| 29 | + python: 'python', |
| 30 | + py: 'python', |
| 31 | + bash: 'bash', |
| 32 | + sh: 'bash', |
| 33 | + shell: 'bash', |
| 34 | + css: 'css', |
| 35 | + yaml: 'yaml', |
| 36 | + yml: 'yaml', |
| 37 | + md: 'markdown', |
| 38 | + markdown: 'markdown', |
| 39 | + markup: 'markup', |
| 40 | + html: 'markup', |
| 41 | + xml: 'markup', |
| 42 | + php: 'php', |
| 43 | + rust: 'rust', |
| 44 | + go: 'go', |
| 45 | + java: 'java', |
| 46 | + kotlin: 'kotlin', |
| 47 | + scala: 'scala', |
| 48 | + swift: 'swift', |
| 49 | + dart: 'dart', |
| 50 | +} |
| 51 | + |
| 52 | +const LANG_LABEL: Record<string, string> = { |
| 53 | + javascript: 'JavaScript', |
| 54 | + typescript: 'TypeScript', |
| 55 | + json: 'JSON', |
| 56 | + python: 'Python', |
| 57 | + bash: 'Bash', |
| 58 | + css: 'CSS', |
| 59 | + yaml: 'YAML', |
| 60 | + markdown: 'Markdown', |
| 61 | +} |
| 62 | + |
| 63 | +export function CodeBlock({ code, language, filename }: CodeBlockProps) { |
| 64 | + const [copied, setCopied] = useState(false) |
| 65 | + |
| 66 | + const lang = LANG_MAP[language.toLowerCase()] || 'javascript' |
| 67 | + const grammar = languages[lang] || languages.javascript |
| 68 | + const highlighted = highlight(code, grammar, lang) |
| 69 | + const label = filename || LANG_LABEL[lang] || language |
| 70 | + |
| 71 | + const handleCopy = useCallback(async () => { |
| 72 | + try { |
| 73 | + await navigator.clipboard.writeText(code) |
| 74 | + setCopied(true) |
| 75 | + setTimeout(() => setCopied(false), 1500) |
| 76 | + } catch { |
| 77 | + /* clipboard unavailable */ |
| 78 | + } |
| 79 | + }, [code]) |
| 80 | + |
11 | 81 | return ( |
12 | | - <div className='dark w-full overflow-hidden rounded-md border border-[#2a2a2a] bg-[#1F1F1F] text-sm'> |
13 | | - <Code.Viewer |
14 | | - code={code} |
15 | | - showGutter |
16 | | - language={language} |
17 | | - className='[&_pre]:!pb-0 m-0 rounded-none border-0 bg-transparent' |
18 | | - /> |
| 82 | + <div |
| 83 | + className='my-8 overflow-hidden border border-[#2A2A2A] bg-[#111111]' |
| 84 | + style={{ borderRadius: '5px' }} |
| 85 | + > |
| 86 | + <div className='flex items-center justify-between border-b border-[#2A2A2A] bg-[#232323] px-4 py-2'> |
| 87 | + <div className='flex items-center gap-2'> |
| 88 | + <span className='inline-block h-2 w-2 bg-[#00F701]' aria-hidden='true' /> |
| 89 | + <span className='inline-block h-2 w-2 bg-[#2ABBF8]' aria-hidden='true' /> |
| 90 | + <span className='inline-block h-2 w-2 bg-[#FA4EDF]' aria-hidden='true' /> |
| 91 | + </div> |
| 92 | + <span className='font-mono text-[10px] uppercase tracking-widest text-[#ECECEC]'> |
| 93 | + {label} |
| 94 | + </span> |
| 95 | + <button |
| 96 | + type='button' |
| 97 | + onClick={handleCopy} |
| 98 | + className='flex items-center text-[#666] transition-colors hover:text-[#ECECEC]' |
| 99 | + aria-label={copied ? 'Copied' : 'Copy code'} |
| 100 | + > |
| 101 | + {copied ? ( |
| 102 | + <Check className='h-3.5 w-3.5 text-[#00F701]' aria-hidden='true' /> |
| 103 | + ) : ( |
| 104 | + <Copy className='h-3.5 w-3.5' aria-hidden='true' /> |
| 105 | + )} |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + <div className='overflow-x-auto p-4'> |
| 109 | + <pre className='m-0 border-0 bg-transparent p-0 text-[13px] leading-relaxed'> |
| 110 | + <code |
| 111 | + className='font-mono text-[#d4d4d8]' |
| 112 | + dangerouslySetInnerHTML={{ __html: highlighted }} |
| 113 | + /> |
| 114 | + </pre> |
| 115 | + </div> |
19 | 116 | </div> |
20 | 117 | ) |
21 | 118 | } |
0 commit comments