-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx-components.tsx
65 lines (63 loc) · 2.04 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { MDXComponents } from 'mdx/types';
import React from 'react';
import { Paragraph } from './lib/ui/paragraph';
import NextLink, { LinkProps } from 'next/link';
import { Link } from './lib/ui/link';
import { Quote } from './lib/ui/quote';
import { TitleLink } from './components/title-link';
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
h1: (props) => <TitleLink level={1} {...props} />,
h2: (props) => <TitleLink level={2} {...props} />,
h3: (props) => <TitleLink level={3} {...props} />,
p: (props) => <Paragraph {...props} />,
a: ({ href, ...props }) => {
if (
typeof props.children === 'string' &&
props.children?.startsWith('#unstyled')
) {
return (
<NextLink href={href ?? ''} {...(props as Omit<LinkProps, 'href'>)}>
{props.children.replace('#unstyled', '').trim()}
</NextLink>
);
}
return <Link href={href ?? ''} underline {...props} />;
},
ul: ({ children, ...props }) => {
return (
<ul
className='list-inside list-disc space-y-1 border-y-[1px] border-background-50 py-4 dark:border-background-500'
{...props}
>
{children}
</ul>
);
},
ol: ({ children, ...props }) => {
return (
<ol
className='list-inside list-decimal space-y-1 border-y-[1px] border-background-50 py-4 dark:border-background-500'
{...props}
>
{children}
</ol>
);
},
li: ({ children }) => {
const textChildren: { children: string } | undefined = (
(children as []) || []
).find((el: any) => el.props);
const text = textChildren
? (textChildren as { props: { children: string } }).props.children
: '';
return (
<li className=' text-primary-800 marker:text-accent-600 dark:text-primary-200 dark:marker:text-accent-400'>
{text}
</li>
);
},
blockquote: (props) => <Quote {...props} />,
};
}