-
Notifications
You must be signed in to change notification settings - Fork 97
/
mdx-components.tsx
47 lines (45 loc) · 1.29 KB
/
mdx-components.tsx
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
import type { MDXComponents } from 'mdx/types';
import { cn } from '@/lib/utils';
const generateId = (text: string) => {
return text
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '');
};
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
h1: ({ children, ...props }: React.HTMLProps<HTMLHeadingElement>) => {
const id = generateId(children?.toString() || '');
return (
<h1 id={id} data-heading='1' {...props}>
{children}
</h1>
);
},
h2: ({ children, ...props }: React.HTMLProps<HTMLHeadingElement>) => {
const id = generateId(children?.toString() || '');
return (
<h2 id={id} data-heading='2' {...props}>
{children}
</h2>
);
},
h3: ({ children, ...props }: React.HTMLProps<HTMLHeadingElement>) => {
const id = generateId(children?.toString() || '');
return (
<h3 id={id} data-heading='3' {...props}>
{children}
</h3>
);
},
h4: ({ children, ...props }: React.HTMLProps<HTMLHeadingElement>) => {
const id = generateId(children?.toString() || '');
return (
<h4 id={id} data-heading='4' {...props}>
{children}
</h4>
);
},
};
}