Repository navigation
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
278 lines (238 loc) · 6.17 KB
/
Copy pathmdx-components.tsx
File metadata and controls
278 lines (238 loc) · 6.17 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
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
import {
Children,
Fragment,
isValidElement,
type ComponentPropsWithoutRef,
type HTMLAttributes,
type ReactElement,
type ReactNode,
} from "react";
import Link from "next/link";
import type { MDXComponents } from "mdx/types";
import { resolveSiteUrl } from "@/lib/site-url";
import { Admonition } from "@/components/content/admonition";
import CodeBlock from "@/components/content/code-block";
import { DocExample } from "@/components/content/doc-example";
import { Tabs as ContentTabs, type Tab } from "@/components/content/tabs";
const DEVHUB_SITE_URL_PLACEHOLDER = "__DEVHUB_SITE_URL__";
type CodeTab = {
code: string;
label: string;
language?: string;
meta?: string | null;
};
type TabItemProps = {
children?: ReactNode;
default?: boolean;
label?: string;
value: string;
};
type TabValue = {
default?: boolean;
label?: string;
value: string;
};
type TabsProps = {
children?: ReactNode;
className?: string;
defaultValue?: string | null;
groupId?: string;
labels?: string[];
queryString?: boolean | string;
values?: readonly TabValue[];
};
function substituteDevhubSiteUrl(value: string): string {
return value.includes(DEVHUB_SITE_URL_PLACEHOLDER)
? value.replaceAll(DEVHUB_SITE_URL_PLACEHOLDER, resolveSiteUrl())
: value;
}
function substituteDevhubSiteUrlChildren(children: ReactNode): ReactNode {
return Children.map(children, (child) =>
typeof child === "string" ? substituteDevhubSiteUrl(child) : child,
);
}
function extractText(node: ReactNode): string {
if (typeof node === "string") {
return substituteDevhubSiteUrl(node);
}
if (typeof node === "number") {
return String(node);
}
if (Array.isArray(node)) {
return node.map(extractText).join("");
}
if (isValidElement<{ children?: ReactNode }>(node)) {
return extractText(node.props.children);
}
return "";
}
function Anchor({ href, children, ...props }: ComponentPropsWithoutRef<"a">) {
if (href?.startsWith("/")) {
return (
<Link
className="text-db-lava-light underline decoration-white/20 underline-offset-4 transition-colors hover:text-white"
href={href}
>
{children}
</Link>
);
}
return (
<a
className="text-db-lava-light underline decoration-white/20 underline-offset-4 transition-colors hover:text-white"
href={href}
rel="noreferrer"
target={href?.startsWith("http") ? "_blank" : undefined}
{...props}
>
{children}
</a>
);
}
function Code({ children, ...props }: HTMLAttributes<HTMLElement>) {
const rawCode = extractText(children);
const rawCodeProps = rawCode.includes(resolveSiteUrl())
? { "data-raw-code": rawCode }
: {};
return (
<code {...props} {...rawCodeProps}>
{substituteDevhubSiteUrlChildren(children)}
</code>
);
}
function Paragraph({
children,
...props
}: HTMLAttributes<HTMLParagraphElement>) {
return <p {...props}>{substituteDevhubSiteUrlChildren(children)}</p>;
}
function Span({ children, ...props }: HTMLAttributes<HTMLSpanElement>) {
return <span {...props}>{substituteDevhubSiteUrlChildren(children)}</span>;
}
function AdmonitionComponent({
children,
title,
type = "note",
}: {
children?: ReactNode;
title?: ReactNode;
type?: string;
}): ReactNode {
return (
<Admonition title={title} type={type}>
{children}
</Admonition>
);
}
function parseCodeTabs(tabs: CodeTab[] | string): CodeTab[] {
if (typeof tabs !== "string") {
return tabs;
}
try {
const parsed = JSON.parse(tabs) as unknown;
if (!Array.isArray(parsed)) {
return [];
}
return parsed.filter((tab): tab is CodeTab => {
if (tab === null || typeof tab !== "object") {
return false;
}
const candidate = tab as Partial<CodeTab>;
return (
typeof candidate.code === "string" &&
typeof candidate.label === "string"
);
});
} catch {
return [];
}
}
function CodeTabs({ tabs }: { tabs: CodeTab[] | string }) {
const markdownTabs: Tab[] = parseCodeTabs(tabs).map((tab, index) => ({
content: (
<CodeBlock
language={tab.language ?? "text"}
title={tab.meta ?? undefined}
>
{tab.code}
</CodeBlock>
),
label: tab.label,
value: `${tab.label}-${index}`,
}));
return markdownTabs.length > 0 ? <ContentTabs tabs={markdownTabs} /> : null;
}
function flattenChildren(children: ReactNode): ReactNode[] {
if (children == null || typeof children === "boolean") {
return [];
}
if (Array.isArray(children)) {
return children.flatMap(flattenChildren);
}
if (isValidElement<{ children?: ReactNode }>(children)) {
if (children.type === Fragment) {
return flattenChildren(children.props.children);
}
}
return [children];
}
function isTabItem(child: ReactNode): child is ReactElement<TabItemProps> {
return (
isValidElement<TabItemProps>(child) &&
typeof child.props.value === "string" &&
child.props.value !== ""
);
}
function TabItem({ children }: TabItemProps) {
return <>{children}</>;
}
function Tabs({
children,
className,
defaultValue,
groupId,
labels,
queryString,
values,
}: TabsProps) {
const tabItems = flattenChildren(children).filter(isTabItem);
if (tabItems.length === 0) {
return <>{children}</>;
}
const tabs: Tab[] = tabItems.map((tab, index) => {
const value = values?.[index]?.value ?? tab.props.value;
return {
content: tab.props.children,
default: values?.[index]?.default ?? tab.props.default,
label:
values?.[index]?.label ?? labels?.[index] ?? tab.props.label ?? value,
value,
};
});
const element = (
<ContentTabs
defaultValue={defaultValue ?? undefined}
groupId={groupId}
queryString={queryString}
tabs={tabs}
/>
);
return className ? <div className={className}>{element}</div> : element;
}
const devhubMdxComponents = {
Admonition: AdmonitionComponent,
CodeTabs,
DocExample,
code: Code,
p: Paragraph,
span: Span,
TabItem,
Tabs,
} satisfies MDXComponents;
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...devhubMdxComponents,
a: Anchor,
...components,
};
}