-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
80 lines (75 loc) · 2.42 KB
/
Copy pathmdx-components.tsx
File metadata and controls
80 lines (75 loc) · 2.42 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
import type { MDXComponents } from 'mdx/types'
import { Accordion, AccordionItem } from '@/components/Accordian'
import { Basic } from '@/components/Basic'
import { Button } from '@/components/Button'
import { Callout } from '@/components/Callout'
import { Fence } from '@/components/Fence'
import { Figure } from '@/components/Figure'
import { Group } from '@/components/Group'
import { Icon } from '@/components/Icon'
import { QuickLink, QuickLinks } from '@/components/QuickLinks'
import { SnapshotsVsReplay } from '@/components/SnapshotsVsReplay'
import { Steps } from '@/components/Steps'
import { Tab, Tabs } from '@/components/Tabs'
import { TwoColumns } from '@/components/TwoColumns'
import { Video } from '@/components/Video'
import { YouTube } from '@/components/YouTube'
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
Callout,
Figure,
Video,
YouTube,
Steps,
Tabs,
Tab,
Accordion,
AccordionItem,
QuickLinks,
QuickLink,
TwoColumns,
Group,
Icon,
Basic,
Button,
SnapshotsVsReplay,
pre: ({ children, ...props }: React.ComponentPropsWithoutRef<'pre'>) => {
const codeEl = children as React.ReactElement<Record<string, unknown>>
if (codeEl?.props) {
const codeProps = codeEl.props
const className = codeProps.className as string | undefined
const code = codeProps.children as string | undefined
const fileName = codeProps.fileName as string | undefined
const lineNumbers = codeProps.lineNumbers as boolean | undefined
const rawHighlight = codeProps.highlight as
| string
| string[]
| undefined
const highlight =
typeof rawHighlight === 'string'
? (JSON.parse(rawHighlight) as string[])
: rawHighlight
const language = className?.replace('language-', '') || ''
if (typeof code === 'string') {
return (
<Fence
language={language}
fileName={fileName ?? ''}
lineNumbers={lineNumbers ?? false}
highlight={highlight ?? []}
>
{code}
</Fence>
)
}
}
return <pre {...props}>{children}</pre>
},
th: ({ children, ...props }: React.ComponentPropsWithoutRef<'th'>) => (
<th scope="col" {...props}>
{children}
</th>
),
...components,
}
}