({
code: rawCode,
language: rawLang,
}: {
code: string
language: string
})
| 24 | * @example view `src/components/code/CodeGroupFromFile.tsx` |
| 25 | */ |
| 26 | export default function Code({ |
| 27 | code: rawCode, |
| 28 | language: rawLang, |
| 29 | }: { |
| 30 | code: string |
| 31 | language: string |
| 32 | }) { |
| 33 | const code = useMemo(() => (rawCode ?? '').trim(), [rawCode]) |
| 34 | const { inCodeGroup, hasTitle: rounded } = useCodeGroup() |
| 35 | const inline = useMemo(() => { |
| 36 | return code.trim().split('\n').length === 1 && !inCodeGroup |
| 37 | }, [inCodeGroup, code]) |
| 38 | |
| 39 | const lang = useMemo( |
| 40 | () => (rawLang ? rawLang.replace('language-', '') : 'plaintext'), |
| 41 | [rawLang], |
| 42 | ) |
| 43 | |
| 44 | if (inline) { |
| 45 | return ( |
| 46 | <pre className="inline-block rounded-lg bg-zinc-800 px-2 text-sm text-white"> |
| 47 | {rawCode} |
| 48 | </pre> |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | return ( |
| 53 | <div |
| 54 | className={cn( |
| 55 | 'prose overflow-x-auto border border-gray-700 bg-zinc-900 pb-0 dark:border-gray-700', |
| 56 | rounded ? 'rounded-b-lg rounded-t-none' : 'rounded-lg', |
| 57 | inCodeGroup && 'border-0', |
| 58 | )} |
| 59 | > |
| 60 | <CopyButton value={code} className="z-10 border-gray-600" /> |
| 61 | <SyntaxHighlighter language={lang} wrapLines wrapLongLines style={theme}> |
| 62 | {code} |
| 63 | </SyntaxHighlighter> |
| 64 | </div> |
| 65 | ) |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * This is a raw code block component that accepts code as `className: string` |
nothing calls this directly
no test coverage detected