-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdx-components.tsx
136 lines (135 loc) · 3.38 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
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
import type { MDXComponents } from 'mdx/types';
import { Link } from '@nextui-org/react';
import Image from 'next/image';
import { ImageProps } from 'next/image';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { materialDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
a(props: any) {
return (
<Link
href={props.href}
isExternal
size="md"
style={{ wordBreak: 'break-all' }}
>
{props.children}
</Link>
);
},
img(props: any) {
return (
<div style={{ position: 'relative', width: '100%', height: '300px' }}>
<Image
fill
sizes="100vw"
style={{
objectFit: 'contain',
}}
{...(props as ImageProps)}
/>
</div>
);
},
p({ children }) {
return (
<p style={{ marginTop: '0.5rem', marginBottom: '0.5rem' }}>
{children}
</p>
);
},
h1({ children }) {
return (
<h1
className="text-3xl font-bold sm:text-3xl sm:tracking-tight"
style={{ marginTop: '1.5rem', marginBottom: '1rem' }}
>
{children}
</h1>
);
},
h2({ children }) {
return (
<h2
className="mb-5 text-2xl font-bold sm:text-3xl sm:tracking-tight"
style={{ marginTop: '1.5rem', marginBottom: '1rem' }}
>
{children}
</h2>
);
},
h3({ children }) {
return (
<h3
className="mb-5 text-xl font-bold sm:text-3xl sm:tracking-tight"
style={{ marginTop: '1.5rem', marginBottom: '1rem' }}
>
{children}
</h3>
);
},
ol({ children }) {
return (
<ol
style={{
listStylePosition: 'inside',
listStyleType: 'decimal',
marginBottom: '1rem',
}}
>
{children}
</ol>
);
},
ul({ children }) {
return (
<ul
style={{
listStylePosition: 'inside',
listStyleType: 'disc',
marginBottom: '1rem',
}}
>
{children}
</ul>
);
},
li({ children }) {
return <li className="mb-2 list-item list-inside">{children}</li>;
},
blockquote({ children }) {
return (
<blockquote className="relative border-s-4 border-gray-800 bg-slate-200 pl-2 ps-4 sm:ps-6">
{children}
</blockquote>
);
},
table({ children }) {
return <table className="table-auto">{children}</table>;
},
code(props) {
const { children, className, ...rest } = props;
const match = /language-(\w+)/.exec(className || '');
return match ? (
<SyntaxHighlighter
PreTag="div"
lineProps={{
style: { wordBreak: 'break-all', whiteSpace: 'pre-wrap' },
}}
// eslint-disable-next-line react/no-children-prop
children={String(children).replace(/\n$/, '')}
wrapLines
wrapLongLines
language={match[1]}
style={materialDark}
/>
) : (
<code {...rest} className={className}>
{children}
</code>
);
},
...components,
};
}