Repository navigation
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
62 lines (55 loc) · 2.13 KB
/
Copy pathmdx-components.tsx
File metadata and controls
62 lines (55 loc) · 2.13 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
import defaultMdxComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';
import { Children, isValidElement, type ComponentProps, type ReactNode } from 'react';
import { Info } from '@/components/info';
import { Tip } from '@/components/tip';
import { YouTube } from '@/components/youtube';
import { ReactTemplates, ReactTemplate } from '@/components/react-template';
import { ScreenshotsGallery } from '@/components/screenshots-gallery';
import { CopyBlock } from '@/app/components/copy-block';
import { Pipeline, PipelineStep } from '@/components/pipeline';
const DefaultImg = defaultMdxComponents.img;
// remark-image can emit fractional SVG dimensions, which next/image rejects
const round = (n?: number | string) => (n == null ? n : Math.round(Number(n)));
const hasLink = (children: ReactNode): boolean =>
Children.toArray(children).some(
(c) => isValidElement<{ href?: string; children?: ReactNode }>(c) && (c.props.href != null || hasLink(c.props.children)),
);
// fumadocs' Heading wraps its children in an anchor link, so headings that are already links
// would render a nested <a> (invalid HTML that breaks hydration); render those as plain headings
function heading(as: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6') {
const Default = defaultMdxComponents[as];
return function LinkSafeHeading(props: ComponentProps<typeof as>) {
if (!hasLink(props.children)) return <Default {...props} />;
const As = as;
return <As {...props} className={['scroll-m-28', props.className].filter(Boolean).join(' ')} />;
};
}
const headings = {
h1: heading('h1'),
h2: heading('h2'),
h3: heading('h3'),
h4: heading('h4'),
h5: heading('h5'),
h6: heading('h6'),
};