-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmdx-components.tsx
71 lines (68 loc) · 2.21 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { MDXComponents } from "mdx/types";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { CopyToClipboard } from "@/components/CopyToClipboard";
import dracula from "react-syntax-highlighter/dist/cjs/styles/prism/dracula";
import Image from "next/image";
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
code: (props: any) => {
const { children, ...restProps } = props;
const [, language] =
props.className !== undefined && props.className.length > 0
? props.className.match(/language-(\w+)/)
: [];
const codeText = String(children).replace(/\n$/, "");
if (language !== undefined) {
return (
<div className="relative">
<SyntaxHighlighter
language={language}
style={dracula}
showLineNumbers={true}
>
{codeText}
</SyntaxHighlighter>
<CopyToClipboard text={codeText} />
</div>
);
}
// return <Code fontSize="md" wordBreak="break-all" {...props} />;
return (
<span className="text-md w-fit rounded-sm bg-[#1c1c1c] px-2 py-1 font-mono">
{codeText}
</span>
);
},
h1: (props: any) => <h1 className="h1">{props.children}</h1>,
h2: (props: any) => <h2 className="h2">{props.children}</h2>,
h3: (props: any) => <h3 className="h3">{props.children}</h3>,
h4: (props: any) => <h4 className="h4">{props.children}</h4>,
p: (props: any) => <p className="mdx-p text-xl">{props.children}</p>,
a: (props: any) => (
<a className="mdx-a" target="_blank" {...props}>
{props.children}
</a>
),
ul: (props: any) => (
<ul className="mdx-ul text-xl" {...props}>
{props.children}
</ul>
),
ol: (props: any) => (
<ol className="mdx-ol text-xl" {...props}>
{props.children}
</ol>
),
img: (props: any) => (
<Image
width={1000}
height={100}
className="mdx-img m-0 h-auto w-full object-cover"
alt=""
{...props}
/>
),
};
}