File size: 1,245 Bytes
275f68e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useEffect, useRef } from 'react'
import hljs from 'highlight.js/lib/core'
import python from 'highlight.js/lib/languages/python'
import json from 'highlight.js/lib/languages/json'
import 'highlight.js/styles/atom-one-dark.css'

hljs.registerLanguage('python', python)
hljs.registerLanguage('json', json)

/**
 * CodeBlock — a syntax-highlighted <pre><code> block.
 *
 * Dark, compact styling tuned to match the Claude-ish palette. Uses hljs's
 * atom-one-dark theme then overrides the background with our canvas so it
 * blends with the card surface.
 */
export function CodeBlock({ code, language = 'python', className = '' }) {
  const ref = useRef(null)

  useEffect(() => {
    if (ref.current) {
      // Remove the hljs "highlighted" marker so reruns pick up the new code
      delete ref.current.dataset.highlighted
      hljs.highlightElement(ref.current)
    }
  }, [code, language])

  return (
    <pre className={`rounded-lg border border-border overflow-x-auto
                     bg-[#14120f] my-1 ${className}`}>
      <code ref={ref} className={`language-${language} block p-4 text-[0.82rem]
                                  leading-relaxed font-mono !bg-transparent`}>
        {code}
      </code>
    </pre>
  )
}