-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
143 lines (134 loc) ยท 3.77 KB
/
mdx-components.tsx
File metadata and controls
143 lines (134 loc) ยท 3.77 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
import { useMDXComponents as getBlogMDXComponents } from 'nextra-theme-blog';
import { useMDXComponents as getNextraComponents } from 'nextra/mdx-components';
import { Posts } from '@/components/posts';
import { Tags } from '@/components/tags';
import { CustomTOC } from '@/components/custom-toc';
import { CustomTodo } from '@/components/custom-todo';
import { slugify } from '@/lib/slugify';
import React from 'react';
function getHeadingId(children: React.ReactNode, id?: string): string | undefined {
if (id) return id;
// children์์ ํ
์คํธ ์ถ์ถ
if (typeof children === 'string') {
return slugify(children);
}
if (React.isValidElement(children)) {
const props = children.props as { children?: React.ReactNode };
if (typeof props.children === 'string') {
return slugify(props.children);
}
}
// children์ด ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ
if (Array.isArray(children)) {
const text = children
.map(child => {
if (typeof child === 'string') return child;
if (React.isValidElement(child)) {
const childProps = child.props as { children?: React.ReactNode };
if (typeof childProps.children === 'string') {
return childProps.children;
}
}
return '';
})
.join(' ')
.trim();
return text ? slugify(text) : undefined;
}
return undefined;
}
const blogComponents = getBlogMDXComponents({
h1: ({ children, id, ...props }) => {
const headingId = getHeadingId(children, id);
return (
<h1 id={headingId} className="custom-h1" {...props}>
{children}
</h1>
);
},
h2: ({ children, id, ...props }) => {
const headingId = getHeadingId(children, id);
return (
<h2 id={headingId} {...props}>
{children}
</h2>
);
},
h3: ({ children, id, ...props }) => {
const headingId = getHeadingId(children, id);
return (
<h3 id={headingId} {...props}>
{children}
</h3>
);
},
h4: ({ children, id, ...props }) => {
const headingId = getHeadingId(children, id);
return (
<h4 id={headingId} {...props}>
{children}
</h4>
);
},
h5: ({ children, id, ...props }) => {
const headingId = getHeadingId(children, id);
return (
<h5 id={headingId} {...props}>
{children}
</h5>
);
},
h6: ({ children, id, ...props }) => {
const headingId = getHeadingId(children, id);
return (
<h6 id={headingId} {...props}>
{children}
</h6>
);
},
img: ({ src, alt, ...props }: any) => {
const srcValue = typeof src === 'string' ? src : src?.src || src?.default?.src || undefined;
return <img src={srcValue} alt={alt} className="rounded-lg" {...props} />;
},
figure: ({ children, ...props }: { children?: React.ReactNode; [key: string]: any }) => {
return (
<figure {...props}>
{React.Children.map(children, child => {
if (React.isValidElement(child) && child.type === 'img') {
const childProps = child.props as { className?: string; [key: string]: any };
return React.cloneElement(child, {
className: `rounded-lg ${childProps.className || ''}`,
} as any);
}
return child;
})}
</figure>
);
},
DateFormatter: ({ date }) =>
`Last updated at ${date.toLocaleDateString('en', {
day: 'numeric',
month: 'long',
year: 'numeric',
})}`,
});
const defaultComponents = getNextraComponents({
wrapper({ children, toc }) {
return (
<>
{children}
{/*<TOC toc={toc}/>*/}
</>
);
},
});
export function useMDXComponents() {
return {
...blogComponents,
...defaultComponents,
Posts: Posts,
Tags: Tags,
CustomTOC: CustomTOC,
CustomTodo: CustomTodo,
};
}