-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
157 lines (141 loc) · 3.49 KB
/
mdx-components.tsx
File metadata and controls
157 lines (141 loc) · 3.49 KB
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
import { RegistrationForm } from "app/blog/posts/examples/translation-example/registration-form";
import { Code } from "bright";
import { bodyFontClass } from "lib/fonts";
import { getCurrentTheme } from "lib/headers";
import { MDXRemote } from "next-mdx-remote/rsc";
import Image from "next/image";
import Link from "next/link";
import React from "react";
import styles from "./mdx-components.module.css";
function Table({ data }) {
const headers = data.headers.map((header) => <th key={header}>{header}</th>);
const rows = data.rows.map((row) => (
<tr key={row}>
{row.map((cell) => (
<td key={cell}>{cell}</td>
))}
</tr>
));
return (
<table>
<thead>
<tr>{headers}</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
function CustomLink(props) {
const href = props.href;
if (href.startsWith("/")) {
return (
<Link href={href} {...props}>
{props.children}
</Link>
);
}
if (href.startsWith("#")) {
return <a {...props} />;
}
return <a target="_blank" rel="noopener noreferrer" {...props} />;
}
function RoundedImage(props) {
return <Image alt={props.alt} className="rounded-lg" {...props} />;
}
export function CodeComponent(theme) {
// Create a more robust theme mapping
const themeMap: Record<string, typeof Code.theme> = {
dark: "dracula",
light: "light-plus",
default: "dark-plus",
monokai: "monokai",
cobalt2: "solarized-dark",
opus: "solarized-dark",
};
return function Component({ children, ...props }) {
if (!props.className) {
return (
<code {...props} className={styles.inlineCode}>
{children}
</code>
);
}
// Use the theme mapping with a fallback to default
const codeTheme = themeMap[theme] || themeMap.default;
return (
<Code
className={`${styles.pre} ${bodyFontClass}`}
lineNumbers
theme={codeTheme}
lightThemeSelector='[data-theme="light"]'
lang={props.className === "language-python" ? "py" : "ts"}
>
{children}
</Code>
);
};
}
function slugify(str) {
return str
.toString()
.toLowerCase()
.trim() // Remove whitespace from both ends of a string
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word characters except for -
.replace(/\-\-+/g, "-"); // Replace multiple - with single -
}
function createHeading(level) {
const Heading = ({ children }) => {
const slug = slugify(children);
return React.createElement(
`h${level}`,
{
id: slug,
className: styles[`h${level}`],
},
[
React.createElement(
"a",
{
href: `#${slug}`,
key: `link-${slug}`,
},
children,
),
],
);
};
Heading.displayName = `Heading${level}`;
return Heading;
}
export async function CustomMDX(props) {
const theme = await getCurrentTheme();
const components = {
h1: createHeading(1),
h2: createHeading(2),
h3: createHeading(3),
h4: createHeading(4),
h5: createHeading(5),
h6: createHeading(6),
Image: RoundedImage,
a: CustomLink,
code: CodeComponent(theme),
Table,
RegistrationForm,
p: ({ children }) => <p className={styles.p}>{children}</p>,
blockquote: ({ children }) => (
<blockquote className={styles.blockquote}>{children}</blockquote>
),
ul: ({ children }) => <ul className={styles.ul}>{children}</ul>,
};
return (
<article className={styles.article}>
<MDXRemote
source={props.source}
options={props.options}
components={{ ...components, ...(props.components || {}) }}
/>
</article>
);
}