-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd-components.tsx
263 lines (251 loc) · 7.46 KB
/
md-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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"use client";
import React, { useEffect, useState } from "react";
import ReactMarkdown from "react-markdown";
import { cn } from "@/lib/utils";
import { NpmCommands } from "@/types/unist";
import { Style } from "@/components/md/styles";
import { Event } from "@/lib/events";
import { CopyButton } from "@/components/md/copy-button";
import { CodeBlockWrapper } from "@/components/md/code-block-wrapper";
import Image from "next/image";
import { StyleWrapper } from "@/components/md/style-wrapper";
import remarkGfm from "remark-gfm";
import remarkGemoji from "remark-gemoji";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { coldarkDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { remark } from "remark";
import rehypeKatex from "rehype-katex";
const components = {
h1: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h1
className={cn(
"group font-heading mt-2 scroll-m-20 text-4xl font-bold",
className,
)}
{...props}
/>
),
h2: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h2
className={cn(
"group font-heading mt-12 scroll-m-20 border-b pb-2 text-2xl font-semibold tracking-tight first:mt-0",
className,
)}
{...props}
/>
),
h3: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h3
className={cn(
"group font-heading mt-8 scroll-m-20 text-xl font-semibold tracking-tight",
className,
)}
{...props}
/>
),
h4: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h4
className={cn(
"group font-heading mt-8 scroll-m-20 text-lg font-semibold tracking-tight",
className,
)}
{...props}
/>
),
h5: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h5
className={cn(
"group mt-8 scroll-m-20 text-lg font-semibold tracking-tight",
className,
)}
{...props}
/>
),
h6: ({ className, ...props }: React.HTMLAttributes<HTMLHeadingElement>) => (
<h6
className={cn(
"group mt-8 scroll-m-20 text-base font-semibold tracking-tight",
className,
)}
{...props}
/>
),
a: ({ className, ...props }: React.HTMLAttributes<HTMLAnchorElement>) => (
<a
className={cn("font-medium underline underline-offset-4", className)}
{...props}
/>
),
p: ({ className, ...props }: React.HTMLAttributes<HTMLParagraphElement>) => (
<p
className={cn("leading-7 [&:not(:first-child)]:mt-6", className)}
{...props}
/>
),
ul: ({ className, ...props }: React.HTMLAttributes<HTMLUListElement>) => (
<ul className={cn("my-6 ml-6 list-disc", className)} {...props} />
),
ol: ({ className, ...props }: React.HTMLAttributes<HTMLOListElement>) => (
<ol className={cn("my-6 ml-6 list-decimal", className)} {...props} />
),
li: ({ className, ...props }: React.HTMLAttributes<HTMLElement>) => (
<li className={cn("mt-2", className)} {...props} />
),
blockquote: ({ className, ...props }: React.HTMLAttributes<HTMLElement>) => (
<blockquote
className={cn("mt-6 border-l-2 pl-6 italic", className)}
{...props}
/>
),
img: ({
className,
alt,
...props
}: React.ImgHTMLAttributes<HTMLImageElement>) => (
// eslint-disable-next-line @next/next/no-img-element
<img className={cn("rounded-md", className)} alt={alt} {...props} />
),
hr: ({ ...props }: React.HTMLAttributes<HTMLHRElement>) => (
<hr className="my-4 md:my-8" {...props} />
),
table: ({ className, ...props }: React.HTMLAttributes<HTMLTableElement>) => (
<div className="my-6 w-full overflow-y-auto">
<table className={cn("w-full", className)} {...props} />
</div>
),
tr: ({ className, ...props }: React.HTMLAttributes<HTMLTableRowElement>) => (
<tr
className={cn("m-0 border-t p-0 even:bg-muted", className)}
{...props}
/>
),
th: ({ className, ...props }: React.HTMLAttributes<HTMLTableCellElement>) => (
<th
className={cn(
"border px-4 py-2 text-left font-bold [&[align=center]]:text-center [&[align=right]]:text-right",
className,
)}
{...props}
/>
),
td: ({ className, ...props }: React.HTMLAttributes<HTMLTableCellElement>) => (
<td
className={cn(
"border px-4 py-2 text-left [&[align=center]]:text-center [&[align=right]]:text-right",
className,
)}
{...props}
/>
),
pre: ({
className,
__rawString__,
__npmCommand__,
__yarnCommand__,
__pnpmCommand__,
__bunCommand__,
__withMeta__,
__src__,
__event__,
__style__,
...props
}: React.HTMLAttributes<HTMLPreElement> & {
__style__?: Style["name"];
__rawString__?: string;
__withMeta__?: boolean;
__src__?: string;
__event__?: Event["name"];
} & NpmCommands) => {
return (
<StyleWrapper styleName={__style__}>
<pre
className={cn(
"mb-4 mt-6 max-h-[650px] shadow overflow-x-auto rounded-lg border bg-primary-foreground/70 font-semibold",
className,
)}
{...props}
/>
{__rawString__ && !__npmCommand__ && (
<CopyButton
value={__rawString__}
src={__src__}
event={__event__}
className={cn("absolute right-4 top-4", __withMeta__ && "top-16")}
/>
)}
</StyleWrapper>
);
},
code(props: any) {
const { children, className, node, ...rest } = props;
const match = /language-(\w+)/.exec(className || "");
const language = match ? match[1] : null;
const value = String(children).replace(/\n$/, "");
const customStyle = {
backgroundColor: "var(--primary-foreground) / 0.05",
};
return match ? (
<div className="relative">
<code className={cn("relative rounded text-sm", className)}>
<SyntaxHighlighter
{...rest}
PreTag="div"
customStyle={customStyle}
language={language}
style={coldarkDark}
codeTagProps={
language === "txt" && {
className: "dark:text-white text-black font-bold",
}
}
>
{value}
</SyntaxHighlighter>
</code>
<CopyButton className="absolute right-0 top-0 m-2" value={value} />
</div>
) : (
<code
className={cn(
"relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-semibold text-sm",
className,
)}
{...props}
/>
);
},
Image,
CodeBlockWrapper: ({ ...props }) => (
<CodeBlockWrapper className="rounded-md border" {...props} />
),
};
const MarkdownReader = ({ markdown }: { markdown: any }) => {
const [mdSourse, setMdSourse] = useState("");
const Mdgenerating = async () => {
const file = await remark()
.use(remarkGfm)
.use(remarkGemoji)
.process(markdown);
return file.toString();
};
useEffect(() => {
Mdgenerating().then((res) => {
setMdSourse(res);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [markdown]);
return (
<div className="markdown-container">
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkGemoji]}
rehypePlugins={[rehypeKatex]}
components={{
...components,
}}
>
{mdSourse}
</ReactMarkdown>
</div>
);
};
export default MarkdownReader;