Code Block
inferencesh/code-blockFreeBlock
Syntax-highlighted code block with line numbers and copy button.
Live preview
live · rendered by the registry
Rendered by inferencesh on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.inference.sh/r/code-block.jsonSource
1'use client'23import { cn } from '@/lib/utils'4import { Check, Copy } from 'lucide-react'5import React, { memo, useMemo, useState } from 'react'67import { tokenize, type TokenizeContext } from '@/components/infsh/code-block/tokenizer'8import { tokenStyles } from '@/components/infsh/code-block/styles'9import { normalizeLanguage } from '@/components/infsh/code-block/languages'10import { getTextContent, splitLines, copyToClipboard } from '@/components/infsh/code-block/utils'1112// Re-export types for consumers13export type { Token, TokenType, LanguageDefinition } from '@/components/infsh/code-block/types'14export { getLanguage, normalizeLanguage } from '@/components/infsh/code-block/languages'1516/**17 * Tokenize all lines with context passing between lines18 * This handles multiline constructs like template literals19 */20function tokenizeLines(lines: string[], language: string) {21 const tokenizedLines: { tokens: ReturnType<typeof tokenize>['tokens'] }[] = []22 let context: TokenizeContext = {}2324 for (const line of lines) {25 const result = tokenize(line, language, context)26 tokenizedLines.push({ tokens: result.tokens })27 context = result.context28 }2930 return tokenizedLines31}3233/**34 * Renders a single line with pre-computed tokens35 */36const HighlightedLine = memo(function HighlightedLine({37 tokens,38}: {39 tokens: ReturnType<typeof tokenize>['tokens']40}) {41 return (42 <>43 {tokens.map((token, i) =>44 token.type ? (45 <span key={i} className={tokenStyles[token.type]}>46 {token.content}47 </span>48 ) : (49 <span key={i}>{token.content}</span>50 )51 )}52 </>53 )54})5556export interface CodeBlockProps {57 children: React.ReactNode58 language?: string59 className?: string60 /** Show line numbers (default: true) */61 showLineNumbers?: boolean62 /** Show copy button (default: true) */63 showCopyButton?: boolean64 /** Show language header (default: true) */65 showHeader?: boolean66 /** Enable syntax highlighting (default: true) */67 enableHighlighting?: boolean68 /** Custom text size class */69 textSize?: string70}7172/**73 * Full-featured code block with syntax highlighting, line numbers, and copy button74 */75export const CodeBlock = memo(function CodeBlock({76 children,77 language,78 className,79 showLineNumbers = true,80 showCopyButton = true,81 showHeader = true,82 enableHighlighting = true,83 textSize,84}: CodeBlockProps) {85 const [copied, setCopied] = useState(false)8687// … truncated
Files it writes
More from inferencesh
All 14 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Agent Chatagent | inferencesh | Blocks | Free | 1 dep | |
| Chat UI Primitiveschat | inferencesh | Blocks | Free | 1 dep · 15 files | |
| Markdown Renderer (legacy)markdown | inferencesh | Blocks | Free | 2 deps · 2 files | |
| pretext-mdpretext-md | inferencesh | Blocks | Free | 4 deps · 10 files | |
| Sidebar Lightsidebar-light | inferencesh | Blocks | Free | no deps | |
| Stepssteps | inferencesh | Blocks | Free | no deps · 2 files | |
| Table of Contentstable-of-contents | inferencesh | Blocks | Free | no deps | |
| Tasktask | inferencesh | Blocks | Free | 1 dep · 9 files |
