-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmdx-components.tsx
214 lines (201 loc) · 5.44 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
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
import type { MDXComponents } from "mdx/types";
import Image from "next/image";
import CodeBlock from "@/ui/CodeBlock";
export function useMDXComponents(
components: MDXComponents = {},
): MDXComponents {
return {
// Headings
h1: (props) => (
<h1
style={{ fontSize: "2.5rem", marginBottom: "1rem", color: "#333" }}
{...props}
/>
),
h2: (props) => (
<h2
style={{ fontSize: "2rem", marginBottom: "0.75rem", color: "#333" }}
{...props}
/>
),
h3: (props) => (
<h3
style={{ fontSize: "1.75rem", marginBottom: "0.75rem", color: "#333" }}
{...props}
/>
),
h4: (props) => (
<h4
style={{ fontSize: "1.5rem", marginBottom: "0.5rem", color: "#333" }}
{...props}
/>
),
h5: (props) => (
<h5
style={{ fontSize: "1.25rem", marginBottom: "0.5rem", color: "#333" }}
{...props}
/>
),
h6: (props) => (
<h6
style={{ fontSize: "1rem", marginBottom: "0.5rem", color: "#333" }}
{...props}
/>
),
// Paragraphs & text
p: (props) => (
<p style={{ marginBottom: "1rem", color: "#555" }} {...props} />
),
a: (props) => (
<a style={{ color: "#0070f3", textDecoration: "underline" }} {...props} />
),
strong: (props) => <strong style={{ fontWeight: "bold" }} {...props} />,
em: (props) => <em style={{ fontStyle: "italic" }} {...props} />,
// Inline code: if no language is specified
code: (props) => {
if (!props.className) {
return (
<code
style={{
backgroundColor: "#000",
padding: "0.2em 0.4em",
borderRadius: "3px",
fontFamily: "monospace",
color: "#fff",
}}
{...props}
/>
);
}
// If a language is specified, delegate to CodeBlock for highlighting.
return <CodeBlock {...props} />;
},
// For preformatted code blocks, also use CodeBlock when possible.
pre: (props) => {
const child = props.children;
if (child && child.props && child.props.className) {
return <CodeBlock {...child.props} />;
}
return (
<pre
style={{
backgroundColor: "#000",
padding: "1em",
overflowX: "auto",
borderRadius: "5px",
marginBottom: "1rem",
color: "#000", // ensure default text color is black
}}
{...props}
/>
);
},
// Images with default width/height if not provided
img: (props: any) => {
const { src, alt } = props;
return (
<div
style={{
display: "flex",
justifyContent: "center",
marginBottom: "1rem",
}}
>
<Image
src={src}
alt={alt || "MDX Image"}
width={200}
height={200}
style={{ objectFit: "contain" }}
/>
</div>
);
},
// Blockquote
blockquote: (props) => (
<blockquote
style={{
borderLeft: "4px solid #888",
paddingLeft: "1rem",
marginBottom: "1rem",
color: "#666",
fontStyle: "italic",
margin: 0,
}}
{...props}
/>
),
// Tables (wrapped in a responsive container)
table: (props) => (
<div style={{ overflowX: "auto", width: "100%" }}>
<table
style={{
width: "100%",
borderCollapse: "collapse",
marginBottom: "1rem",
padding: "0.5rem",
}}
{...props}
/>
</div>
),
thead: (props) => <thead style={{ background: "none" }} {...props} />,
tbody: (props) => <tbody {...props} />,
tr: (props) => <tr style={{ borderBottom: "1px solid #ddd" }} {...props} />,
th: (props) => (
<th
style={{
padding: "0.75rem",
textAlign: "left",
fontWeight: "bold",
}}
{...props}
/>
),
td: (props) => (
<td style={{ padding: "0.75rem", textAlign: "left" }} {...props} />
),
// Figures
figure: (props) => (
<figure style={{ margin: "1rem 0", textAlign: "center" }} {...props} />
),
figcaption: (props) => (
<figcaption
style={{ fontSize: "0.9rem", color: "#777", marginTop: "0.5rem" }}
{...props}
/>
),
// Details and Summary
details: (props) => (
<details
style={{
marginBottom: "1rem",
padding: "1rem",
backgroundColor: "#f9f9f9",
border: "1px solid #ddd",
borderRadius: "4px",
}}
{...props}
/>
),
summary: (props) => (
<summary style={{ cursor: "pointer", fontWeight: "bold" }} {...props} />
),
// Definition List
dl: (props) => <dl style={{ marginBottom: "1rem" }} {...props} />,
dt: (props) => (
<dt style={{ fontWeight: "bold", marginTop: "1rem" }} {...props} />
),
dd: (props) => (
<dd style={{ marginLeft: "1rem", marginBottom: "1rem" }} {...props} />
),
// Subscript and Superscript
sub: (props) => <sub style={{ fontSize: "0.8em" }} {...props} />,
sup: (props) => <sup style={{ fontSize: "0.8em" }} {...props} />,
// Highlighted text
mark: (props) => (
<mark style={{ backgroundColor: "#ff0", color: "#000" }} {...props} />
),
...components,
};
}