forked from langfuse/langfuse-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
82 lines (75 loc) · 2.7 KB
/
mdx-components.tsx
File metadata and controls
82 lines (75 loc) · 2.7 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
import defaultMdxComponents from "fumadocs-ui/mdx";
import type { MDXComponents } from "mdx/types";
import React from "react";
import dynamic from "next/dynamic";
import { Mermaid } from "@/components/Mermaid";
import { Image } from "@/components/ui/image";
import { Frame } from "@/components/Frame";
import { LangTabs } from "@/components/LangTabs";
import { FetchReadme } from "@/components/FetchReadme";
import { Callout, Tabs, Tab, Cards, Card, Steps, FileTree, FileTreeFile, FileTreeFolder, Playground } from "@/components/docs";
import { MdxDetails, MdxSummary } from "@/components/MdxDetails";
import { AvailabilityBanner } from "@/components/availability";
import { Link as MdxLink, type LinkProps } from "@/components/ui/link";
// Lazy-load Video so @vidstack/react (~800 KB) is NOT bundled on every MDX page.
// It only downloads on pages that actually render a <Video> tag.
const Video = dynamic(() => import("@/components/Video").then((m) => ({ default: m.Video })));
const BLOCK_TAGS = new Set([
"div", "details", "summary", "figure", "pre", "table",
"ul", "ol", "blockquote", "section", "article",
]);
function MdxParagraph({ children, ...props }: React.HTMLAttributes<HTMLElement>) {
const childrenArray = React.Children.toArray(children);
// If children contain MdxSummary, render without any wrapping element.
// <summary> MUST be a direct child of <details> for the browser to recognize it
// as the disclosure widget. React does not auto-correct invalid DOM nesting
// the way the HTML parser does, so a <p> or <div> wrapping <summary> keeps it trapped.
const hasSummary = childrenArray.some(
(child) => React.isValidElement(child) && child.type === MdxSummary
);
if (hasSummary) {
return <>{children}</>;
}
const hasBlock = childrenArray.some(
(child) =>
React.isValidElement(child) &&
typeof child.type === "string" &&
BLOCK_TAGS.has(child.type)
);
if (hasBlock) {
return <div {...props}>{children}</div>;
}
return <p {...props}>{children}</p>;
}
export function getMDXComponents(components?: MDXComponents): MDXComponents {
return {
...defaultMdxComponents,
a: (props: LinkProps) => <MdxLink variant="underline" {...props} />,
img: Image,
p: MdxParagraph,
Frame,
Video,
LangTabs,
Callout,
Tabs,
Tab,
"Tabs.Tab": Tab,
Cards,
Card,
"Cards.Card": Card,
Steps,
FileTree,
"FileTree.File": FileTreeFile,
"FileTree.Folder": FileTreeFolder,
FetchReadme,
details: MdxDetails,
summary: MdxSummary,
AvailabilityBanner,
Mermaid,
Playground,
...components,
};
}
export function useMDXComponents(components?: MDXComponents): MDXComponents {
return getMDXComponents(components);
}