-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
107 lines (101 loc) · 3.76 KB
/
mdx-components.tsx
File metadata and controls
107 lines (101 loc) · 3.76 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
import type { MDXComponents } from "mdx/types";
import { Accordion, Accordions } from "fumadocs-ui/components/accordion";
import { Step, Steps } from "fumadocs-ui/components/steps";
import { Callout } from "fumadocs-ui/components/callout";
import { Tab, Tabs } from "fumadocs-ui/components/tabs";
import { Cards, Card } from "fumadocs-ui/components/card";
import { TypeTable } from "fumadocs-ui/components/type-table";
import { Heading } from "fumadocs-ui/components/heading";
import defaultComponents from "fumadocs-ui/mdx";
import {
CodeBlock,
type CodeBlockProps,
Pre,
} from "fumadocs-ui/components/codeblock";
import type { ReactNode } from "react";
import "fumadocs-twoslash/twoslash.css";
import { Popup, PopupContent, PopupTrigger } from "fumadocs-twoslash/ui";
import YouTube from "@/components/content-design/youtube";
import Gallery from "@/components/content-design/gallery";
import { cn } from "@/utils/cn";
import { BadgeCheck } from "lucide-react";
import dynamic from "next/dynamic";
import { DataAPIPage, MetricsAPIPage } from "@/components/api/api-pages";
import Quiz from "@/components/quizzes/quiz";
const Mermaid = dynamic(() => import("@/components/content-design/mermaid"), {
ssr: false,
});
const StateGrowthChart = dynamic(() => import("@/components/content-design/state-growth-chart"), {
ssr: false,
});
export function useMDXComponents(components: MDXComponents): MDXComponents {
// Exclude heading and img components from defaultComponents to avoid conflicts
const { h1, h2, h3, h4, h5, h6, img, ...restDefaultComponents } = defaultComponents;
// Custom components registered as MDX shortcodes. The MDXComponents index
// signature in @mdx-js/mdx v3 is stricter than before, so we type these
// separately and merge with a cast.
const customComponents = {
BadgeCheck,
Popup,
PopupContent,
PopupTrigger,
Tabs,
Tab,
Cards,
Card,
Callout,
TypeTable,
Step,
Steps,
APIPage: (props: any) => {
const document = props.document || '';
const isMetricsApi = document.includes('popsicle.json');
return isMetricsApi ? <MetricsAPIPage {...props} /> : <DataAPIPage {...props} />;
},
Accordion,
Accordions,
YouTube,
Gallery,
Mermaid,
StateGrowthChart,
Quiz,
InstallTabs: ({
items,
children,
}: {
items: string[];
children: ReactNode;
}) => (
<Tabs items={items} id="package-manager">
{children}
</Tabs>
),
};
// The MDXComponents index signature in @mdx-js/mdx v3 (via next-mdx-remote 6)
// is stricter and doesn't accept arbitrary React components for custom keys.
// We build the full map untyped and cast at the boundary.
const allComponents: Record<string, any> = {
...restDefaultComponents,
h1: (props: any) => <Heading as="h1" {...props} />,
h2: (props: any) => <Heading as="h2" {...props} />,
h3: (props: any) => <Heading as="h3" {...props} />,
h4: (props: any) => <Heading as="h4" {...props} />,
h5: (props: any) => <Heading as="h5" {...props} />,
h6: (props: any) => <Heading as="h6" {...props} />,
// Fix srcset -> srcSet for React 19 compatibility
img: (props: any) => {
const { srcset, ...imgProps } = props;
// eslint-disable-next-line jsx-a11y/alt-text, @next/next/no-img-element
return <img {...imgProps} {...(srcset && { srcSet: srcset })} />;
},
pre: ({ title, className, icon, allowCopy, ...props }: CodeBlockProps) => (
<CodeBlock title={title} icon={icon} allowCopy={allowCopy}>
<Pre className={cn("max-h-[1200px]", className)} {...(props as any)} />
</CodeBlock>
),
blockquote: (props: any) => <Callout>{props.children}</Callout>,
...customComponents,
...components,
};
return allComponents as MDXComponents;
}