-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
198 lines (188 loc) · 5.21 KB
/
mdx-components.tsx
File metadata and controls
198 lines (188 loc) · 5.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import GithubSlugger from 'github-slugger'
import Link from 'next/link'
import type { ReactNode } from 'react'
import { ClickableHeader } from '@/components/ui/clickable-header'
import { Note } from '@/components/ui/note'
import { Stars } from '@/components/ui/stars'
import { TableOfContents, type TocVariant } from '@/components/ui/toc'
import { cn } from '@/lib/utils'
import type { MDXComponents } from 'mdx/types'
// Define Spotify component props
interface SpotifyProps {
link: string
wide?: boolean
className?: string
}
interface QuoteProps {
children: ReactNode
mark?: boolean
className?: string
}
// Spotify component that converts Spotify links to embed URLs
const Spotify = ({ link, wide = false, className }: SpotifyProps) => {
// Convert spotify URL to embed URL
const embedUrl = link.replace('spotify.com', 'spotify.com/embed')
return (
<div
className={cn(
'mx-auto my-8 w-full max-w-[350px]',
wide && 'max-w-[650px]',
className,
)}
>
<iframe
src={embedUrl}
width="100%"
height={wide ? '80' : '352'}
allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"
loading="lazy"
className="rounded-xl"
style={{ borderRadius: '12px' }}
/>
</div>
)
}
const Quote = ({ children, mark = false, className }: QuoteProps) => {
return (
<blockquote className={cn(mark && 'quote-mark', className)}>
{children}
</blockquote>
)
}
export function useMDXComponents(components: MDXComponents): MDXComponents {
const slugger = new GithubSlugger()
return {
...components,
// hr: () => <Stars asHr />,
StarDivider: ({
variant,
className,
}: {
variant?: 1 | 2 | 3
className?: string
}) => <Stars asHr variant={variant} className={className} />,
// Add Spotify component to MDX components
Spotify,
// Add an opt-in quote component for decorative quote-mark styling.
Quote,
Note,
strike: ({ children }) => <span className="line-through">{children}</span>,
// Add custom link component to handle external links
a: ({ href, children, ...props }) => {
const isExternal = href?.startsWith('http') || href?.startsWith('mailto:')
if (isExternal) {
return (
<a href={href} target="_blank" rel="noopener noreferrer" {...props}>
{children}
</a>
)
}
return href ? (
<Link href={href} {...props}>
{children}
</Link>
) : null
},
h1: ({ children }) => {
const slug = slugger.slug(children?.toString() || '')
return (
<ClickableHeader level={1} slug={slug}>
{children}
</ClickableHeader>
)
},
h2: ({ children }) => {
const slug = slugger.slug(children?.toString() || '')
// Exclude Footnotes heading from TOC
if (children?.toString().toLowerCase() === 'footnotes') {
return (
<ClickableHeader
level={2}
slug={slug}
dataAttributes={{ 'data-toc-exclude': '' }}
>
{children}
</ClickableHeader>
)
}
return (
<ClickableHeader level={2} slug={slug}>
{children}
</ClickableHeader>
)
},
h3: ({ children }) => {
const slug = slugger.slug(children?.toString() || '')
return (
<ClickableHeader level={3} slug={slug}>
{children}
</ClickableHeader>
)
},
h4: ({ children }) => {
const slug = slugger.slug(children?.toString() || '')
return (
<ClickableHeader level={4} slug={slug}>
{children}
</ClickableHeader>
)
},
Img: ({
src,
alt,
title,
caption,
size,
className,
lightSrc,
}: {
src: string
alt: string
title: string
caption: string
invert?: boolean
size?: number
className?: string
lightSrc?: string
}) => {
return (
<figure className="flex flex-col items-center">
<a
href={src}
target="_blank"
rel="noopener noreferrer"
className="no-underline"
>
<img
title={title}
src={src}
alt={alt}
className={cn(
'cover mx-auto rounded-xl shadow-lg',
lightSrc ? 'hidden dark:block' : 'block',
className,
)}
style={size ? { width: `${size}%`, height: 'auto' } : undefined}
/>
{lightSrc && (
<img
title={title}
src={lightSrc}
alt={alt}
className={cn(
'cover mx-auto block rounded-xl dark:hidden',
className,
)}
style={size ? { width: `${size}%`, height: 'auto' } : undefined}
/>
)}
</a>
<figcaption className="text-center font-sans">{caption}</figcaption>
</figure>
)
},
TOC: ({ title, variant }: { title?: string; variant?: TocVariant }) => {
return <TableOfContents title={title} variant={variant} />
},
}
}