-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
142 lines (140 loc) · 4.13 KB
/
mdx-components.tsx
File metadata and controls
142 lines (140 loc) · 4.13 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { ClientCodeWrapper } from "@/components/app/client-code-wrapper"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { codeToHtml } from "@/lib/shiki"
import { cn } from "@/lib/utils"
import type { MDXComponents } from "mdx/types"
import Link from "next/link"
import { extractCodeFromFilePath } from "./lib/code"
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
code: ({ children, ...props }: React.HTMLAttributes<HTMLElement>) => (
<code {...props} className="not-prose bg-secondary p-1 font-mono">
{children}
</code>
),
// @ts-ignore
a: (props: React.ComponentProps<typeof Link>) => (
<Link {...props} href={props.href || ""}>
{props.children}
</Link>
),
Link: ({ children, ...props }: React.ComponentProps<"a">) => (
<Link {...props} href={props.href as string}>
{children}
</Link>
),
// @ts-ignore
CodeBlock: async ({
language,
code,
filePath,
...props
}: {
language: string
code: string
filePath?: string
} & React.HTMLAttributes<HTMLDivElement>) => {
const fileContent = filePath
? extractCodeFromFilePath(filePath)
: code || ""
const html = await codeToHtml({ code: fileContent, lang: language })
return (
<ClientCodeWrapper code={fileContent}>
<div
dangerouslySetInnerHTML={{ __html: html }}
className="not-prose bg-zinc-50 dark:bg-zinc-900/50 overflow-auto rounded-md border border-zinc-200 dark:border-zinc-700 p-4 text-[13px]"
/>
</ClientCodeWrapper>
)
},
Step: ({ className, children, ...props }: React.ComponentProps<"h3">) => (
<h3 className={cn("step", className)} data-heading="3" {...props}>
{children}
</h3>
),
Steps: ({ ...props }) => (
<div
className="steps mb-12 ml-4 border-l pl-8 [counter-reset:step]"
{...props}
/>
),
Tabs: ({ className, ...props }: React.ComponentProps<typeof Tabs>) => (
<Tabs className={cn("relative mt-6 w-full", className)} {...props} />
),
TabsList: ({
className,
...props
}: React.ComponentProps<typeof TabsList>) => (
<TabsList className={cn(className)} {...props} />
),
TabsTrigger: ({
className,
...props
}: React.ComponentProps<typeof TabsTrigger>) => (
<TabsTrigger className={cn(className)} {...props} />
),
TabsContent: ({
className,
...props
}: React.ComponentProps<typeof TabsContent>) => (
<TabsContent className={cn(className)} {...props} />
),
table: ({
className,
...props
}: React.HTMLAttributes<HTMLTableElement>) => (
<div className="not-prose relative w-full table-auto overflow-auto rounded-lg border border-zinc-200 dark:border-zinc-700 text-sm">
<table className={cn("w-full", className)} {...props} />
</div>
),
thead: ({
className,
...props
}: React.HTMLAttributes<HTMLTableSectionElement>) => (
<thead
className={cn(
"bg-zinc-100 text-zinc-900 dark:bg-zinc-800 dark:text-zinc-100",
className
)}
{...props}
/>
),
tbody: ({
className,
...props
}: React.HTMLAttributes<HTMLTableSectionElement>) => (
<tbody
className={cn(
"divide-y divide-zinc-200 dark:divide-y dark:divide-zinc-700",
className
)}
{...props}
/>
),
tr: ({
className,
...props
}: React.HTMLAttributes<HTMLTableRowElement>) => (
<tr className={cn("h-10", className)} {...props} />
),
th: ({
className,
...props
}: React.HTMLAttributes<HTMLTableHeaderCellElement>) => (
<th
className={cn("px-4 pb-0 text-left align-middle font-[450]", className)}
{...props}
/>
),
td: ({
className,
...props
}: React.HTMLAttributes<HTMLTableDataCellElement>) => (
<td
className={cn("px-4 py-2 text-left align-middle", className)}
{...props}
/>
),
}
}