Skip to content

Commit 63cfb9c

Browse files
authored
Fix/doc code (#78)
* fix: DocCodeBlock * fix: code-block * fix: dark mode color install code block
1 parent 4ad9aeb commit 63cfb9c

File tree

2 files changed

+17
-68
lines changed

2 files changed

+17
-68
lines changed

components/app/doc-code-block.tsx

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
"use client"
2-
3-
import { readFileContent } from "@/lib/actions"
41
import { codeToHtml } from "@/lib/shiki"
5-
import { useTheme } from "next-themes"
6-
import { useEffect, useState } from "react"
72
import { ClientCodeWrapper } from "./client-code-wrapper"
83

94
type DocCodeBlockProps = {
@@ -12,62 +7,20 @@ type DocCodeBlockProps = {
127
filePath?: string
138
} & React.HTMLAttributes<HTMLDivElement>
149

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({
2211
language,
2312
code,
24-
filePath,
2513
...props
2614
}: 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 })
5116

52-
// SSR fallback: render plain code if not hydrated yet
5317
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+
/>
7124
</ClientCodeWrapper>
7225
)
7326
}

mdx-components.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
33
import { cn } from "@/lib/utils"
44
import type { MDXComponents } from "mdx/types"
55
import Link from "next/link"
6+
import { extractCodeFromFilePath } from "./lib/code"
67

78
export function useMDXComponents(components: MDXComponents): MDXComponents {
89
return {
@@ -12,9 +13,8 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
1213
{children}
1314
</code>
1415
),
15-
// @ts-ignore
16-
a: (props: React.ComponentProps<typeof Link>) => (
17-
<Link {...props} href={props.href || ""}>
16+
a: (props: React.AnchorHTMLAttributes<HTMLAnchorElement>) => (
17+
<Link {...props} href={props.href || "#"}>
1818
{props.children}
1919
</Link>
2020
),
@@ -23,8 +23,7 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
2323
{children}
2424
</Link>
2525
),
26-
// @ts-ignore
27-
CodeBlock: async ({
26+
CodeBlock: ({
2827
language,
2928
code,
3029
filePath,
@@ -34,14 +33,11 @@ export function useMDXComponents(components: MDXComponents): MDXComponents {
3433
code: string
3534
filePath?: string
3635
} & React.HTMLAttributes<HTMLDivElement>) => {
37-
return (
38-
<DocCodeBlock
39-
language={language}
40-
code={code}
41-
filePath={filePath}
42-
{...props}
43-
/>
44-
)
36+
const fileContent = filePath
37+
? extractCodeFromFilePath(filePath)
38+
: code || ""
39+
40+
return <DocCodeBlock language={language} code={fileContent} {...props} />
4541
},
4642
Step: ({ className, children, ...props }: React.ComponentProps<"h3">) => (
4743
<h3 className={cn("step", className)} data-heading="3" {...props}>

0 commit comments

Comments
 (0)