-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdx-components.tsx
132 lines (127 loc) · 4.28 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
124
125
126
127
128
129
130
131
132
import React, { ComponentPropsWithoutRef } from 'react';
import Link from 'next/link';
import Image from 'next/image';
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-lg mt-6 mb-2" {...props} />
),
h2: (props: HeadingProps) => (
<h2 className="font-bold text-xl mt-8 mb-4" {...props} />
),
h3: (props: HeadingProps) => (
<h3 className="font-semibold text-lg mt-6 mb-3" {...props} />
),
h4: (props: HeadingProps) => (
<h4 className="font-semibold text-base mt-4 mb-2" {...props} />
),
p: (props: ParagraphProps) => (
<p className="text-gray-700 leading-relaxed mb-4 max-w-[92ch]" style={{
maxWidth: '720px',
}} {...props}/>
),
ol: (props: ListProps) => (
<ol className="list-decimal pl-8 mb-4 space-y-2" {...props} />
),
ul: (props: ListProps) => (
<ul className="list-disc pl-8 mb-4 space-y-2" {...props} />
),
li: (props: ListItemProps) => (
<li className="text-gray-700" {...props} />
),
em: (props: ComponentPropsWithoutRef<'em'>) => (
<em className="italic" {...props} />
),
strong: (props: ComponentPropsWithoutRef<'strong'>) => (
<strong className="font-bold" {...props} />
),
a: ({ href, children, ...props }: AnchorProps) => {
const className = 'text-brand_orange hover:text-brand_orange-light 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
className="bg-gray-100 px-1.5 py-0.5 rounded text-sm"
dangerouslySetInnerHTML={{ __html: codeHTML }}
{...props}
/>
);
},
Table: ({ data }: { data: { headers: string[]; rows: string[][] } }) => (
<table className="w-full border-collapse mb-4">
<thead>
<tr className="bg-gray-100">
{data.headers.map((header, index) => (
<th key={index} className="border p-2 text-left">{header}</th>
))}
</tr>
</thead>
<tbody>
{data.rows.map((row, index) => (
<tr key={index} className="border-b">
{row.map((cell, cellIndex) => (
<td key={cellIndex} className="border p-2">{cell}</td>
))}
</tr>
))}
</tbody>
</table>
),
blockquote: (props: BlockquoteProps) => (
<blockquote
className="border-l-4 border-brand_orange pl-4 py-2 my-4 bg-gray-50 italic"
{...props}
/>
),
img: ({ src, alt, ...props }: ComponentPropsWithoutRef<'img'>) => {
return (
<div className="my-8">
<Image
src={src as string}
alt={alt || ''}
className="min-w-40"
width={800}
height={400}
/>
</div>
);
},
};
declare global {
type MDXProvidedComponents = typeof components;
}
export function useMDXComponents(): MDXProvidedComponents {
return components;
}