-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx-components.tsx
123 lines (119 loc) · 3.45 KB
/
mdx-components.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
119
120
121
122
123
import React, { ComponentPropsWithoutRef } from "react";
import Link from "next/link";
import { highlight } from "sugar-high";
type HeadingProps = ComponentPropsWithoutRef<"h1">;
type ParagraphProps = ComponentPropsWithoutRef<"p">;
type ListProps = ComponentPropsWithoutRef<"ul">;
type ListItemProps = ComponentPropsWithoutRef<"li">;
type AnchorProps = ComponentPropsWithoutRef<"a">;
type BlockquoteProps = ComponentPropsWithoutRef<"blockquote">;
const components = {
h1: (props: HeadingProps) => (
<h1
className="font-bold text-3xl md:text-5xl tracking-tight text-gray-700 dark:text-white mb-6"
{...props}
/>
),
h2: (props: HeadingProps) => (
<h2
className="font-bold text-2xl md:text-4xl tracking-tight text-gray-700 dark:text-white mb-4"
{...props}
/>
),
h3: (props: HeadingProps) => (
<h3
className="font-bold text-xl tracking-tight text-gray-700 dark:text-white mb-4"
{...props}
/>
),
h4: (props: HeadingProps) => <h4 className="font-bold" {...props} />,
p: (props: ParagraphProps) => (
<p
className="text-gray-700 dark:text-gray-400 tracking-normal leading-7 mb-4"
{...props}
/>
),
ol: (props: ListProps) => (
<ol
className="text-gray-700 dark:text-gray-400 tracking-normal leading-7 mb-4 list-decimal pl-5 space-y-2"
{...props}
/>
),
ul: (props: ListProps) => (
<ul
className="text-gray-700 dark:text-gray-400 tracking-normal leading-7 mb-4 list-disc pl-5 space-y-1"
{...props}
/>
),
li: (props: ListItemProps) => <li className="pl-1" {...props} />,
em: (props: ComponentPropsWithoutRef<"em">) => (
<em className="font-bold" {...props} />
),
strong: (props: ComponentPropsWithoutRef<"strong">) => (
<strong className="font-bold" {...props} />
),
a: ({ href, children, ...props }: AnchorProps) => {
const className = "text-blue-500 dark:text-blue-400 font-medium underline";
if (href?.startsWith("/")) {
return (
<Link href={href} className={className} {...props}>
{children}
</Link>
);
}
if (href?.startsWith("#")) {
return (
<a href={href} className={className} {...props}>
{children}
</a>
);
}
return (
<a
href={href}
target="_blank"
rel="noopener noreferrer"
className={className}
{...props}
>
{children}
</a>
);
},
code: ({ children, ...props }: ComponentPropsWithoutRef<"code">) => {
const codeHTML = highlight(children as string);
return <code dangerouslySetInnerHTML={{ __html: codeHTML }} {...props} />;
},
Table: ({ data }: { data: { headers: string[]; rows: string[][] } }) => (
<table>
<thead>
<tr>
{data.headers.map((header, index) => (
<th key={index}>{header}</th>
))}
</tr>
</thead>
<tbody>
{data.rows.map((row, index) => (
<tr key={index}>
{row.map((cell, cellIndex) => (
<td key={cellIndex}>{cell}</td>
))}
</tr>
))}
</tbody>
</table>
),
blockquote: (props: BlockquoteProps) => (
<blockquote
className="ml-[0.075em] border-l-3 border-gray-300 pl-4 text-gray-700 dark:border-zinc-600 dark:text-zinc-300"
{...props}
/>
),
};
declare global {
type MDXProvidedComponents = typeof components;
}
export function useMDXComponents(): MDXProvidedComponents {
return components;
}