-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcode-block.tsx
More file actions
49 lines (44 loc) · 1.18 KB
/
code-block.tsx
File metadata and controls
49 lines (44 loc) · 1.18 KB
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
40
41
42
43
44
45
46
47
48
49
"use client";
import { useEffect, useState } from "react";
import { codeToHtml } from "shiki";
export function CodeBlock({
code,
lang = "javascript",
}: {
code: string;
lang?: string;
}) {
const [html, setHtml] = useState<string>("");
useEffect(() => {
let cancelled = false;
codeToHtml(code, {
lang,
theme: "github-dark-default",
}).then((result) => {
if (!cancelled) setHtml(result);
});
return () => {
cancelled = true;
};
}, [code, lang]);
if (!html) {
return (
<pre className="line-numbers bg-black p-4 font-mono text-sm leading-relaxed text-[#e6edf3]">
{code.split("\n").map((line, i) => (
<div key={i} className="table-row">
<span className="table-cell w-8 select-none pr-4 text-right tabular-nums text-[#484f58]">
{i + 1}
</span>
<span className="table-cell">{line}</span>
</div>
))}
</pre>
);
}
return (
<div
className="code-block [&_pre]:!bg-black [&_pre]:p-4 [&_pre]:leading-relaxed [&_pre]:text-sm [&_code]:text-sm [&_code]:leading-relaxed"
dangerouslySetInnerHTML={{ __html: html }}
/>
);
}