MCPcopy
hub / github.com/21st-dev/1code / CodeBlock

Function CodeBlock

src/renderer/components/chat-markdown-renderer.tsx:39–137  ·  view source on GitHub ↗
({
  language,
  children,
  themeId,
  size = "md",
}: {
  language?: string
  children: string
  themeId: string
  size?: "sm" | "md" | "lg"
})

Source from the content-addressed store, hash-verified

37
38// Code block with copy button using Shiki
39function CodeBlock({
40 language,
41 children,
42 themeId,
43 size = "md",
44}: {
45 language?: string
46 children: string
47 themeId: string
48 size?: "sm" | "md" | "lg"
49}) {
50 const [copied, setCopied] = useState(false)
51 const [highlightedHtml, setHighlightedHtml] = useState<string | null>(null)
52
53 const handleCopy = useCallback(() => {
54 navigator.clipboard.writeText(children)
55 setCopied(true)
56 setTimeout(() => setCopied(false), 2000)
57 }, [children])
58
59 // Only use Shiki for known programming languages, not for plaintext/ASCII art
60 const shouldHighlight = language && language !== "plaintext" && language !== "text"
61
62 useEffect(() => {
63 if (!shouldHighlight) return
64
65 let cancelled = false
66
67 const highlight = async () => {
68 try {
69 const html = await highlightCode(children, language, themeId)
70 if (!cancelled) {
71 setHighlightedHtml(html)
72 }
73 } catch (error) {
74 console.error("Failed to highlight code:", error)
75 }
76 }
77
78 highlight()
79
80 return () => {
81 cancelled = true
82 }
83 }, [children, language, themeId, shouldHighlight])
84
85 // For plaintext/ASCII art, just escape and render directly (no Shiki)
86 // For code with syntax highlighting, use Shiki output when available
87 const htmlContent = shouldHighlight
88 ? (highlightedHtml ?? escapeHtml(children))
89 : escapeHtml(children)
90
91 return (
92 <div className="relative mt-2 mb-4 rounded-[10px] bg-muted/50 overflow-hidden">
93 <button
94 onClick={handleCopy}
95 tabIndex={-1}
96 className="absolute top-[6px] right-[6px] p-1 z-2"

Callers

nothing calls this directly

Calls 3

cnFunction · 0.90
highlightFunction · 0.85
escapeHtmlFunction · 0.85

Tested by

no test coverage detected