({
code,
language,
showLineNumbers = false,
className,
children,
...props
}: CodeBlockProps)
| 73 | } |
| 74 | |
| 75 | export const CodeBlock = ({ |
| 76 | code, |
| 77 | language, |
| 78 | showLineNumbers = false, |
| 79 | className, |
| 80 | children, |
| 81 | ...props |
| 82 | }: CodeBlockProps) => { |
| 83 | const [html, setHtml] = useState<string>(""); |
| 84 | const [darkHtml, setDarkHtml] = useState<string>(""); |
| 85 | const mounted = useRef(false); |
| 86 | |
| 87 | useEffect(() => { |
| 88 | highlightCode(code, language, showLineNumbers).then(([light, dark]) => { |
| 89 | if (!mounted.current) { |
| 90 | setHtml(light); |
| 91 | setDarkHtml(dark); |
| 92 | mounted.current = true; |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | return () => { |
| 97 | mounted.current = false; |
| 98 | }; |
| 99 | }, [code, language, showLineNumbers]); |
| 100 | |
| 101 | return ( |
| 102 | <CodeBlockContext.Provider value={{ code }}> |
| 103 | <div |
| 104 | className={cn( |
| 105 | "group relative w-full overflow-hidden rounded-md border bg-background text-foreground", |
| 106 | className |
| 107 | )} |
| 108 | {...props} |
| 109 | > |
| 110 | <div className="relative"> |
| 111 | <div |
| 112 | className="overflow-hidden dark:hidden [&>pre]:m-0 [&>pre]:bg-background! [&>pre]:p-4 [&>pre]:text-foreground! [&>pre]:text-sm [&_code]:font-mono [&_code]:text-sm" |
| 113 | // biome-ignore lint/security/noDangerouslySetInnerHtml: "this is needed." |
| 114 | dangerouslySetInnerHTML={{ __html: html }} |
| 115 | /> |
| 116 | <div |
| 117 | className="hidden overflow-hidden dark:block [&>pre]:m-0 [&>pre]:bg-background! [&>pre]:p-4 [&>pre]:text-foreground! [&>pre]:text-sm [&_code]:font-mono [&_code]:text-sm" |
| 118 | // biome-ignore lint/security/noDangerouslySetInnerHtml: "this is needed." |
| 119 | dangerouslySetInnerHTML={{ __html: darkHtml }} |
| 120 | /> |
| 121 | {children && ( |
| 122 | <div className="absolute top-2 right-2 flex items-center gap-2"> |
| 123 | {children} |
| 124 | </div> |
| 125 | )} |
| 126 | </div> |
| 127 | </div> |
| 128 | </CodeBlockContext.Provider> |
| 129 | ); |
| 130 | }; |
| 131 | |
| 132 | export type CodeBlockCopyButtonProps = ComponentProps<typeof Button> & { |
nothing calls this directly
no test coverage detected