-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
347 lines (340 loc) · 9.73 KB
/
Copy pathmdx-components.tsx
File metadata and controls
347 lines (340 loc) · 9.73 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { icons } from "@tabler/icons-react";
import Image from "next/image";
import Link from "next/link";
import {
type ComponentProps,
type HTMLAttributes,
isValidElement,
type ReactNode,
} from "react";
import { Kbd } from "@/components/ui/8bit/kbd";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { AspectRatio } from "@/components/ui/aspect-ratio";
import { Button } from "@/components/ui/button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { cn } from "@/lib/utils";
import CodeSnippet from "./components/code-snippet";
export const mdxComponents = {
h1: ({ className, ...props }: ComponentProps<"h1">) => (
<h1
className={cn(
"mt-2 scroll-m-28 font-bold font-heading text-3xl tracking-tight",
className
)}
{...props}
/>
),
h2: ({ className, ...props }: ComponentProps<"h2">) => (
<h2
className={cn(
"[&+.steps]:!mt-0 [&+.steps>h3]:!mt-4 [&+h3]:!mt-6 [&+p]:!mt-4 mt-10 scroll-m-28 font-heading font-medium text-xl tracking-tight first:mt-0 lg:mt-16 [&+]*:[code]:text-xl",
className
)}
id={props.children
?.toString()
.replace(/ /g, "-")
.replace(/'/g, "")
.replace(/\?/g, "")
.toLowerCase()}
{...props}
/>
),
h3: ({ className, ...props }: ComponentProps<"h3">) => (
<h3
className={cn(
"[&+p]:!mt-4 mt-12 scroll-m-28 font-heading font-medium text-lg tracking-tight *:[code]:text-xl",
className
)}
{...props}
/>
),
h4: ({ className, ...props }: ComponentProps<"h4">) => (
<h4
className={cn(
"mt-8 scroll-m-28 font-heading font-medium text-base tracking-tight",
className
)}
{...props}
/>
),
h5: ({ className, ...props }: ComponentProps<"h5">) => (
<h5
className={cn(
"mt-8 scroll-m-28 font-medium text-base tracking-tight",
className
)}
{...props}
/>
),
h6: ({ className, ...props }: ComponentProps<"h6">) => (
<h6
className={cn(
"mt-8 scroll-m-28 font-medium text-base tracking-tight",
className
)}
{...props}
/>
),
a: ({ className, ...props }: ComponentProps<"a">) => (
<a
className={cn("font-medium underline underline-offset-4", className)}
{...props}
/>
),
p: ({ className, ...props }: ComponentProps<"p">) => (
<p
className={cn("leading-relaxed [&:not(:first-child)]:mt-6", className)}
{...props}
/>
),
strong: ({ className, ...props }: HTMLAttributes<HTMLElement>) => (
<strong className={cn("font-medium", className)} {...props} />
),
ul: ({ className, ...props }: ComponentProps<"ul">) => (
<ul className={cn("my-6 ml-6 list-disc", className)} {...props} />
),
ol: ({ className, ...props }: ComponentProps<"ol">) => (
<ol className={cn("my-6 ml-6 list-decimal", className)} {...props} />
),
li: ({ className, ...props }: ComponentProps<"li">) => (
<li className={cn("mt-2", className)} {...props} />
),
blockquote: ({ className, ...props }: ComponentProps<"blockquote">) => (
<blockquote
className={cn("mt-6 border-l-2 pl-6 italic", className)}
{...props}
/>
),
img: ({ className, alt, ...props }: ComponentProps<"img">) => (
/* biome-ignore lint/performance/noImgElement: MDX content requires standard img elements */
/* biome-ignore lint/correctness/useImageSize: MDX images have dynamic sizes from markdown */
<img alt={alt} className={cn("rounded-md", className)} {...props} />
),
hr: ({ ...props }: ComponentProps<"hr">) => (
<hr className="my-4 md:my-8" {...props} />
),
table: ({ className, ...props }: ComponentProps<"table">) => (
<div className="no-scrollbar my-6 w-full overflow-y-auto rounded-lg border">
<table
className={cn(
"relative w-full overflow-hidden border-none text-sm [&_tbody_tr:last-child]:border-b-0",
className
)}
{...props}
/>
</div>
),
tr: ({ className, ...props }: ComponentProps<"tr">) => (
<tr className={cn("m-0 border-b", className)} {...props} />
),
th: ({ className, ...props }: ComponentProps<"th">) => (
<th
className={cn(
"px-4 py-2 text-left font-bold [&[align=center]]:text-center [&[align=right]]:text-right",
className
)}
{...props}
/>
),
td: ({ className, ...props }: ComponentProps<"td">) => (
<td
className={cn(
"whitespace-nowrap px-4 py-2 text-left [&[align=center]]:text-center [&[align=right]]:text-right",
className
)}
{...props}
/>
),
pre: ({ className, children, ...props }: ComponentProps<"pre">) => (
<pre
className={cn(
"no-scrollbar min-w-0 overflow-x-auto px-4 py-3.5 outline-none has-[[data-slot=tabs]]:p-0 has-[[data-highlighted-line]]:px-0 has-[[data-line-numbers]]:px-0",
className
)}
{...props}
>
{children}
</pre>
),
figure: ({ className, ...props }: ComponentProps<"figure">) => (
<figure className={cn(className)} {...props} />
),
figcaption: ({
className,
children,
...props
}: ComponentProps<"figcaption">) => {
const IconComponent =
"data-language" in props && typeof props["data-language"] === "string"
? icons[props["data-language"] as keyof typeof icons]
: null;
return (
<figcaption
className={cn(
"flex items-center gap-2 text-code-foreground [&_svg]:size-4 [&_svg]:text-code-foreground [&_svg]:opacity-70",
className
)}
{...props}
>
{IconComponent && <IconComponent />}
{children}
</figcaption>
);
},
code: ({
className,
__raw__,
__src__,
__npm__,
__yarn__,
__pnpm__,
__bun__,
children,
...props
}: ComponentProps<"code"> & {
__raw__?: string;
__src__?: string;
__npm__?: string;
__yarn__?: string;
__pnpm__?: string;
__bun__?: string;
}) => {
// Inline Code.
if (typeof children === "string") {
return (
<code
className={cn(
"relative break-words rounded-md bg-muted px-[0.3rem] py-[0.2rem] font-mono text-[0.8rem] outline-none",
className
)}
{...props}
>
{children}
</code>
);
}
// Extract text content from React children if __raw__ is not available
const getTextContent = (node: ReactNode): string => {
if (typeof node === "string") {
return node;
}
if (typeof node === "number") {
return String(node);
}
if (Array.isArray(node)) {
return node.map(getTextContent).join("");
}
if (isValidElement(node)) {
const nodeProps = node.props as { children?: ReactNode };
if (nodeProps.children) {
return getTextContent(nodeProps.children);
}
}
return "";
};
const codeContent = __raw__ || getTextContent(children);
return <CodeSnippet>{codeContent}</CodeSnippet>;
},
Step: ({ className, ...props }: ComponentProps<"h3">) => (
<h3
className={cn(
"mt-8 scroll-m-32 font-heading font-medium text-xl tracking-tight",
className
)}
{...props}
/>
),
Steps: ({ ...props }) => (
<div
className="[&>h3]:step steps *:[h3]:first:!mt-0 mb-12 [counter-reset:step]"
{...props}
/>
),
Image: ({
src,
className,
width,
height,
alt,
...props
}: ComponentProps<"img">) => (
<Image
alt={alt || ""}
className={cn("mt-6 rounded-md border", className)}
height={Number(height)}
src={(src as string) || ""}
width={Number(width)}
{...props}
/>
),
Tabs: ({ className, ...props }: ComponentProps<typeof Tabs>) => (
<Tabs className={cn("relative mt-6 w-full", className)} {...props} />
),
TabsList: ({ className, ...props }: ComponentProps<typeof TabsList>) => (
<TabsList
className={cn(
"justify-start gap-4 rounded-none bg-transparent px-0",
className
)}
{...props}
/>
),
TabsTrigger: ({
className,
...props
}: ComponentProps<typeof TabsTrigger>) => (
<TabsTrigger
className={cn(
"rounded-none border-0 border-transparent border-b-2 bg-transparent px-0 pb-3 text-base text-muted-foreground hover:text-primary data-[state=active]:border-primary data-[state=active]:bg-transparent data-[state=active]:text-foreground data-[state=active]:shadow-none dark:data-[state=active]:border-primary dark:data-[state=active]:bg-transparent",
className
)}
{...props}
/>
),
TabsContent: ({
className,
...props
}: ComponentProps<typeof TabsContent>) => (
<TabsContent
className={cn(
"relative [&>.steps]:mt-6 [&_h3.font-heading]:font-medium [&_h3.font-heading]:text-base *:[figure]:first:mt-0",
className
)}
{...props}
/>
),
Tab: ({ className, ...props }: ComponentProps<"div">) => (
<div className={cn(className)} {...props} />
),
Button,
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
Alert,
AlertTitle,
AlertDescription,
AspectRatio,
Link: ({ className, ...props }: ComponentProps<typeof Link>) => (
<Link
className={cn("font-medium underline underline-offset-4", className)}
{...props}
/>
),
LinkedCard: ({ className, ...props }: ComponentProps<typeof Link>) => (
<Link
className={cn(
"flex w-full flex-col items-center rounded-xl bg-surface p-6 text-surface-foreground transition-colors hover:bg-surface/80 sm:p-10",
className
)}
{...props}
/>
),
Kbd,
};