1
- "use client"
2
-
3
- import { readFileContent } from "@/lib/actions"
4
1
import { codeToHtml } from "@/lib/shiki"
5
- import { useTheme } from "next-themes"
6
- import { useEffect , useState } from "react"
7
2
import { ClientCodeWrapper } from "./client-code-wrapper"
8
3
9
4
type DocCodeBlockProps = {
@@ -12,62 +7,20 @@ type DocCodeBlockProps = {
12
7
filePath ?: string
13
8
} & React . HTMLAttributes < HTMLDivElement >
14
9
15
- const themeName : Record < string , string > = {
16
- light : "github-light" ,
17
- dark : "github-dark" ,
18
- system : "github-dark" ,
19
- }
20
-
21
- export function DocCodeBlock ( {
10
+ export async function DocCodeBlock ( {
22
11
language,
23
12
code,
24
- filePath,
25
13
...props
26
14
} : DocCodeBlockProps ) {
27
- const [ highlightedHtml , setHighlightedHtml ] = useState < string | null > ( null )
28
- const [ fileContent , setFileContent ] = useState < string > ( code || "" )
29
- const { theme } = useTheme ( )
30
-
31
- useEffect ( ( ) => {
32
- async function highlight ( ) {
33
- const content = filePath ? await readFileContent ( filePath ) : code || ""
34
-
35
- setFileContent ( content )
36
-
37
- if ( ! content ) {
38
- setHighlightedHtml ( "<pre><code></code></pre>" )
39
- return
40
- }
41
-
42
- const html = await codeToHtml ( {
43
- code : content ,
44
- lang : language ,
45
- theme : themeName [ theme as keyof typeof themeName ] ,
46
- } )
47
- setHighlightedHtml ( html )
48
- }
49
- highlight ( )
50
- } , [ code , language , theme , filePath ] )
15
+ const html = await codeToHtml ( { code, lang : language } )
51
16
52
- // SSR fallback: render plain code if not hydrated yet
53
17
return (
54
- < ClientCodeWrapper code = { fileContent } >
55
- { highlightedHtml ? (
56
- < div
57
- dangerouslySetInnerHTML = { { __html : highlightedHtml } }
58
- className = "not-prose bg-background border-border [&_pre]:!bg-background overflow-auto rounded-md border p-2 text-[13px]"
59
- { ...props }
60
- />
61
- ) : (
62
- < div
63
- className = "not-prose bg-background border-border overflow-auto rounded-md border p-2 text-[13px]"
64
- { ...props }
65
- >
66
- < pre >
67
- < code > { fileContent } </ code >
68
- </ pre >
69
- </ div >
70
- ) }
18
+ < ClientCodeWrapper code = { code } >
19
+ < div
20
+ dangerouslySetInnerHTML = { { __html : html } }
21
+ className = "not-prose bg-background border-border overflow-auto rounded-md border p-2 text-[13px] dark:[&_pre]:!bg-transparent dark:[&_span]:!text-white"
22
+ { ...props }
23
+ />
71
24
</ ClientCodeWrapper >
72
25
)
73
26
}
0 commit comments