blob: 16ff84e5e6181ae550ed81e0edae40e033cbfba1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import { useEffect, useState } from "react";
import { codeToHtml } from "../../shiki.bundle";
type Props = {
code: string;
language: string;
};
export default function CodeBlock({ code, language }: Props) {
const [highlightedCode, setHighlightedCode] = useState<string | null>(null);
useEffect(() => {
let isMounted = true;
(async () => {
const highlighted = await codeToHtml(code, {
lang: language,
theme: "github-light",
});
if (isMounted) {
setHighlightedCode(highlighted);
}
})();
return () => {
isMounted = false;
};
}, [code, language]);
return (
<pre className="h-full w-full p-2 bg-gray-50 rounded-lg border border-gray-300 whitespace-pre-wrap break-words">
{highlightedCode === null ? (
<code>{code}</code>
) : (
<code dangerouslySetInnerHTML={{ __html: highlightedCode }} />
)}
</pre>
);
}
|