({
code,
lang,
initial,
preHighlighted,
}: Props)
| 27 | }; |
| 28 | |
| 29 | export default function CodeBlock({ |
| 30 | code, |
| 31 | lang, |
| 32 | initial, |
| 33 | preHighlighted, |
| 34 | }: Props) { |
| 35 | const [content, setContent] = useState<JSX.Element | null>( |
| 36 | preHighlighted || initial || null, |
| 37 | ); |
| 38 | |
| 39 | useLayoutEffect(() => { |
| 40 | // If we have pre-highlighted content, use that |
| 41 | if (preHighlighted) { |
| 42 | setContent(preHighlighted); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | let isMounted = true; |
| 47 | |
| 48 | if (code) { |
| 49 | highlight(code, lang).then((result) => { |
| 50 | if (isMounted) setContent(result); |
| 51 | }); |
| 52 | } else { |
| 53 | setContent( |
| 54 | <pre className="rounded-md bg-zinc-950 p-4">No code available</pre>, |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | return () => { |
| 59 | isMounted = false; |
| 60 | }; |
| 61 | }, [code, lang, preHighlighted]); |
| 62 | |
| 63 | return content ? ( |
| 64 | <div className="[&_code]:font-mono [&_code]:text-[13px] [&_pre]:max-h-[450px] [&_pre]:overflow-auto [&_pre]:rounded-md [&_pre]:bg-zinc-950! [&_pre]:p-4 [&_pre]:leading-snug dark:[&_pre]:bg-zinc-900!"> |
| 65 | {content} |
| 66 | </div> |
| 67 | ) : ( |
| 68 | <pre className="rounded-md bg-zinc-950 p-4">Loading...</pre> |
| 69 | ); |
| 70 | } |
nothing calls this directly
no test coverage detected