-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
243 lines (236 loc) · 5.67 KB
/
mdx-components.tsx
File metadata and controls
243 lines (236 loc) · 5.67 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import type { MDXComponents } from 'mdx/types';
import Image, { ImageProps } from 'next/image';
import BlockMath from './components/posts/BlockMath';
import Toggle from './components/posts/Toggle';
import Callout from './components/posts/Callout';
import { JSX, ReactNode } from 'react';
// TOC를 위한 ID 생성을 위한 함수
const createHeadingId = (children: React.ReactNode): string => {
if (typeof children === 'string') {
return children
.toLowerCase()
.replace(/\s+/g, '-') // 공백을 하이픈으로 변환
.replace(/[?!.,]/g, ''); // 특수문자 제거
}
// children이 복잡한 React 노드인 경우 (예: 강조, 링크 등이 포함된 경우)
// toString을 시도하고 실패하면 빈 문자열 반환
try {
return (children as string)
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[?!.,]/g, '');
} catch (e) {
console.warn('Could not generate ID for heading:', children);
return '';
}
};
/**
공통 heading 렌더링 함수 (h1~h5 대응)
MDX 문서 상의 h1이지만, 실제 HTML에서는 페이지 내 <h1>은 하나만 사용해야 하므로
시멘틱 구조를 위해 <h2>로 렌더링
예: MDX 상의 h1 → HTML의 h2, h2 → h3 ...
*/
const renderHeading = (
level: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6',
tag: keyof JSX.IntrinsicElements,
fontSize: string,
fontWeight: string,
marginTop = '10px',
marginBottom = '10px',
) => {
return ({ children }: { children: ReactNode }) => {
const id = createHeadingId(children);
const HeadingTag = tag;
return (
<HeadingTag
id={id}
style={{
fontFamily: 'PRETENDARD',
fontSize,
fontWeight,
marginTop,
marginBottom,
}}
>
{children}
</HeadingTag>
);
};
};
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
// Heading
h1: renderHeading('h1', 'h2', '40px', '700', '20px', '20px'),
h2: renderHeading('h2', 'h3', '28px', '600', '15px', '15px'),
h3: renderHeading('h3', 'h4', '24px', '500'),
h4: renderHeading('h4', 'h5', '20px', '400'),
h5: renderHeading('h5', 'h6', '18px', '400'),
p: ({ children }) => (
<p
style={{
fontFamily: 'PRETENDARD',
fontSize: '16px',
lineHeight: '1.75',
}}
>
{children}
</p>
),
ul: ({ children }) => (
<ul
style={{
marginLeft: '1.5rem',
marginTop: '0.5rem',
marginBottom: '1rem',
listStyleType: 'circle',
}}
>
{children}
</ul>
),
ol: ({ children }) => (
<ol
style={{
marginLeft: '1.5rem',
marginTop: '0.5rem',
marginBottom: '1rem',
listStyleType: 'decimal',
}}
>
{children}
</ol>
),
li: ({ children }) => (
<li
style={{
fontFamily: 'PRETENDARD',
fontSize: '16px',
marginBottom: '0.5rem',
}}
>
{children}
</li>
),
code: ({ children }) => (
<pre
style={{
fontFamily: 'd2coding, monospace',
padding: '30px 0 30px 40px',
border: '1px solid white',
borderRadius: '10px',
display: 'block',
whiteSpace: 'pre-wrap',
lineHeight: '1.75',
maxHeight: '700px',
overflow: 'scroll',
background:
'radial-gradient(ellipse at bottom, #1B2735 0%, #090A0F 100%)',
scrollbarColor: 'white #333',
scrollbarWidth: 'thin',
}}
>
{children}
</pre>
),
blockquote: ({ children }) => (
<blockquote
style={{
display: 'flex',
alignItems: 'center',
borderLeft: '7px solid #ccc',
margin: '1rem',
paddingLeft: '1rem',
paddingTop: '0.5rem',
paddingBottom: '0.5rem',
}}
>
{children}
</blockquote>
),
img: (props) => (
<Image
sizes="100vw"
style={{
width: '100%',
height: 'auto',
marginBottom: '1rem',
}}
{...(props as ImageProps)}
/>
),
a: ({ children, href }) => (
<a
href={href}
style={{
color: '#0070f3',
textDecoration: 'none',
}}
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
),
hr: () => (
<hr
style={{
border: '0',
borderTop: '1px solid #ccc',
margin: '2rem 0',
}}
/>
),
input: (props) => (
<input
type="checkbox"
style={{
marginRight: '0.5rem',
}}
{...props}
/>
),
// 테이블 스타일 추가
table: ({ children }) => (
<table
style={{
width: '100%',
padding: '2rem',
}}
>
{children}
</table>
),
th: ({ children }) => (
<th
style={{
borderBottom: '1px solid rgba(128, 128, 128, 0.5)',
padding: '1rem',
textAlign: 'center',
fontWeight: '600',
textTransform: 'uppercase',
letterSpacing: '0.5px',
fontSize: '18px',
}}
>
{children}
</th>
),
td: ({ children }) => (
<td
style={{
borderBottom: '1px solid rgba(128, 128, 128, 0.5)',
padding: '1rem',
textAlign: 'center',
}}
>
{children}
</td>
),
// Add custom Components
BlockMath,
Callout,
Toggle,
// Spread custom components if provided
...components,
};
}