-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
mdx.tsx
307 lines (270 loc) · 6.97 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
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
import cx from "classnames";
import GithubSlugger from "github-slugger";
import { createElement, ReactNode, useContext, useEffect } from "react";
import { createPortal } from "react-dom";
import Image from "next/image";
import type { ImageProps } from "next/image";
import { useHover } from "~/helpers/use-hover";
import { AdditionalResources } from "./additional-resources";
import { NotesContext } from "./code-notes";
import { CopyToClipboard } from "./copy-to-clipboard";
import { SplitCodeView } from "./split-code-view";
const DocsLink = ({
children,
href,
...props
}: {
href?: string;
className?: string;
children: React.ReactNode;
}) => {
href = href ? href.replace(/.md(#.*)?$/, "$1") : "";
return (
<a
href={href}
{...props}
className={cx(props.className, {
underline: !href.startsWith("#") && href,
})}
>
{children}
</a>
);
};
const DocsImage = ({ src, ...props }: { src: string } & ImageProps) => (
// eslint-disable-next-line jsx-a11y/alt-text
<Image className="border-2 border-red-500 max-w-full" src={src} {...props} />
);
const heading =
(level: 1 | 2 | 3 | 4 | 5 | 6) =>
// eslint-disable-next-line react/display-name
({ children }: { children: ReactNode }) => {
if (level >= 4) {
// Any levels that are h4 and above have different styles
return createElement(
`h${level}`,
{
className: cx("mt-8 mb-4 text-md font-bold"),
},
children
);
}
return createElement(
`h${level}`,
{
className: cx("my-8", {
"text-3xl": level === 1,
"text-2xl": level === 2,
"text-xl": level === 3,
underline: level > 1,
}),
},
children
);
};
const Paragraph = ({ children }: { children: ReactNode }) => (
<p className="mb-4">{children}</p>
);
const UnorderedList = ({ children, ...props }: { children: ReactNode }) => (
<ul className="mb-4 list-disc list-inside" {...props}>
{children}
</ul>
);
const TableHeader = ({ children, ...props }: { children: string }) => {
const slugger = new GithubSlugger();
const slug = slugger.slug(children);
return (
<th
{...props}
className={cx("text-left", "p-2", "border-b", "border-current", {
"w-full": slug === "description",
"whitespace-nowrap": slug === "parameter-name" || slug === "type",
"pr-8": slug === "parameter-name" || slug === "type",
})}
>
{children}
</th>
);
};
const TableCell = ({ children, ...props }: { children: string }) => {
return (
<td
{...props}
className={cx("text-left", "p-2", "border-b", "border-current", "w-20")}
>
{children}
</td>
);
};
const TableRow = ({ children }: { children: ReactNode }) => (
<tr className="even:bg-indigo-100 dark:even:bg-indigo-900">{children}</tr>
);
const Table = ({ children }: { children: ReactNode }) => (
<div className="max-w-full mb-4 overflow-x-scroll">
<table className="w-full border-collapse">{children}</table>
</div>
);
const Separator = () => (
<div className="my-16 relative text-center">
<hr className="border-t border-red-500" />
<span className="absolute -top-6 text-lg p-2 bg-white dark:bg-gray-800">
🍓
</span>
</div>
);
const Pre = ({
children,
...props
}: {
children: ReactNode;
className?: string;
}) => {
const ctx = useContext(NotesContext);
const { ref } = useHover({
selector: ".code-note",
onMouseOver: (el) => {
const noteId = el.dataset.noteId;
if (noteId) {
ctx.setCurrentNote({ id: noteId, element: el });
}
},
onMouseOut: () => {
ctx.setCurrentNote(null);
},
});
const getCode = () => {
return ref.current.querySelector("code").innerText;
};
return (
<pre
ref={ref}
className={cx(
"mb-8 font-mono overflow-x-auto border-2 border-red-500 p-6",
"bg-white dark:text-white dark:bg-gray-800",
"relative",
props.className
)}
>
<div className="absolute top-3 right-3 z-10">
<CopyToClipboard getText={getCode} />
</div>
{children}
</pre>
);
};
const Code = ({ children }: { children: ReactNode }) => {
return <code className="p-1">{children}</code>;
};
const CodeNotes = ({
children,
...props
}: {
children: ReactNode;
id: string;
}) => {
const { currentNote, setCurrentNote } = useContext(NotesContext);
useEffect(() => {
const removeNote = () => {
setCurrentNote(null);
};
window.addEventListener("scroll", removeNote);
return () => {
window.removeEventListener("scroll", removeNote);
};
}, [setCurrentNote]);
if (currentNote === null) {
return null;
}
const boundingBox = currentNote.element.getBoundingClientRect();
return createPortal(
<div
className={cx("fixed p-4 border-2 border-red-500 bg-gray-100", {
hidden: currentNote.id !== props.id,
})}
style={{
top: boundingBox.top + boundingBox.height + 4 + "px",
left: boundingBox.left + "px",
}}
{...props}
>
<p className="text-gray-600">{children}</p>
</div>,
currentNote.element
);
};
const BaseBlock = ({
heading,
color,
children,
}: {
heading: string;
color: "green" | "blue" | "yellow";
children: ReactNode;
}) => (
<div
className={cx("border-l-4 shadow-sm mb-4 overflow-auto", {
"border-green-500": color === "green",
"border-blue-500": color === "blue",
"border-yellow-500": color === "yellow",
})}
>
<div
className={cx("py-2 px-3 mb-2 font-bold", {
"bg-green-100 dark:bg-green-500": color === "green",
"bg-blue-100 dark:bg-blue-500": color === "blue",
"bg-yellow-100 dark:bg-yellow-500": color === "yellow",
})}
>
{heading}
</div>
<div className="px-3">{children}</div>
</div>
);
const NoteBlock = ({ children }: { children: ReactNode }) => (
<BaseBlock color="blue" heading="📝 Note">
{children}
</BaseBlock>
);
const TipBlock = ({ children }: { children: ReactNode }) => (
<BaseBlock color="green" heading="💡 Tip">
{children}
</BaseBlock>
);
const WarningBlock = ({ children }: { children: ReactNode }) => (
<BaseBlock color="yellow" heading="⚠️ Warning">
{children}
</BaseBlock>
);
const BlockQuote = ({ children }: { children: ReactNode }) => (
<blockquote className="border-l-4 border-gray-300 pt-2 px-3 overflow-auto italic opacity-80 mb-4">
{children}
</blockquote>
);
const theme = {
h1: heading(1),
h2: heading(2),
h3: heading(3),
h4: heading(4),
h5: heading(5),
h6: heading(6),
p: Paragraph,
ul: UnorderedList,
table: Table,
th: TableHeader,
td: TableCell,
tr: TableRow,
hr: Separator,
// eslint-disable-next-line react/display-name
pre: Pre,
inlineCode: Code,
a: DocsLink,
blockquote: BlockQuote,
AdditionalResources,
img: DocsImage,
CodeNotes,
SplitCodeView,
Note: NoteBlock,
Tip: TipBlock,
Warning: WarningBlock,
};
export default theme;