-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMDX.tsx
118 lines (111 loc) · 3.06 KB
/
MDX.tsx
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
"use client";
import { Alert, Badge, Card, Text } from "@/components/retroui";
import { useMDXComponent } from "next-contentlayer/hooks";
import React, { AnchorHTMLAttributes, HTMLAttributes } from "react";
import { ComponentShowcase } from "./ComponentShowcase";
import { cn } from "@/lib/utils";
import { ComponentSource } from "./ComponentSource";
import { CodeBlock } from "./CodeBlock";
import Link from "next/link";
import { ComponentInstall } from "./ComponentInstall";
import Image from "next/image";
import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@headlessui/react";
const components = (type: "doc" | "blog") => ({
h1: (props: HTMLAttributes<HTMLHeadingElement>) => (
<Text as="h1" {...props} />
),
h2: (props: HTMLAttributes<HTMLHeadingElement>) =>
type === "blog" ? (
<Text as="h2" className="mb-4" {...props} />
) : (
<Text as="h2" className="border-b pb-1 mb-6" {...props} />
),
h3: (props: HTMLAttributes<HTMLHeadingElement>) => (
<Text as="h3" className="mb-4" {...props} />
),
h4: (props: HTMLAttributes<HTMLHeadingElement>) => (
<Text as="h4" className="mb-2" {...props} />
),
h5: (props: HTMLAttributes<HTMLHeadElement>) => <Text as="h5" {...props} />,
h6: (props: HTMLAttributes<HTMLHeadElement>) => <Text as="h6" {...props} />,
p: (props: HTMLAttributes<HTMLHeadElement>) =>
type === "blog" ? (
<Text {...props} className="text-lg text-zinc-600" />
) : (
<Text {...props} />
),
li: (props: HTMLAttributes<HTMLHeadElement>) =>
type === "blog" ? (
<Text
as="li"
{...props}
className="text-lg text-zinc-600 ml-4 lg:ml-8 mb-2"
/>
) : (
<Text as="li" {...props} />
),
img: (props: HTMLAttributes<HTMLImageElement>) => (
<img className="mx-auto w-full max-w-[600px] my-8" {...props} />
),
a: (props: AnchorHTMLAttributes<HTMLAnchorElement>) => {
const { href, target, rel, ...rest } = props;
if (!href) {
return <a {...rest} />;
}
const isExternal = href.startsWith("http");
return isExternal ? (
<a
href={href}
target={target || "_blank"}
rel={rel || "noopener noreferrer"}
className="underline underline-offset-4 hover:decoration-primary"
{...rest}
/>
) : (
<Link
href={href}
className="underline underline-offset-4 hover:decoration-primary"
{...rest}
/>
);
},
pre: CodeBlock,
code: ({
className,
children,
...props
}: React.HTMLAttributes<HTMLElement>) => (
<code
className={cn(
"relative rounded-sm bg-[#282A36] p-1 text-primary text-sm",
className,
)}
{...props}
>
{children}
</code>
),
TabGroup,
TabList,
Tab,
TabPanels,
TabPanel,
Link,
Badge,
Image,
Card,
Alert,
ComponentShowcase,
ComponentSource,
ComponentInstall,
});
export default function MDX({
code,
type = "doc",
}: {
code: string;
type?: "doc" | "blog";
}) {
const Content = useMDXComponent(code);
return <Content components={components(type)} />;
}