-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmdx-components.tsx
150 lines (147 loc) · 6.27 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { MDXComponents } from "mdx/types";
import Link from "next/link";
import Icon from "@/components/common/Icon";
import CodeBlock from "@/components/common/CodeBlock";
import Keycap from "@/components/common/Keycap";
import InlineCode from "@/components/common/InlineCode";
import Image from "next/image";
import { Table, Thead, Tbody, Tr, Th, Td } from "@/components/common/Table";
import AppCompatibility from "@/components/common/AppCompatibility";
import AppCompatibilityItem from "@/components/common/AppCompatibilityItem";
import Noti from "@/components/common/Noti";
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
h2({ children, ...rest }) {
return (
<h2 {...rest}>
{ children }
</h2>
);
},
h3({ children, ...rest }) {
return (
<h3 className="sub" {...rest}>
{ children }
</h3>
);
},
h4({ children, ...rest }) {
return (
<h4 className="sub" {...rest}>
{ children }
</h4>
);
},
p({ children }) {
return (<p className={"[&>svg]:relative [&>svg]:top-[-.05em] [&>svg]:z-0 [&>svg]:inline-block"}>{ children }</p>)
},
Mark({ children }) {
return (<mark className={"bg-yellow text-gray-800 font-bold"}>{ children }</mark>);
},
Section({ children }) {
return (<section className={"flex flex-col gap-[24px] w-full leading-normal section"}>{ children }</section>);
},
ol({ children, ...rest }) {
return (<ol className={"flex flex-col gap-[2px]"} { ...rest }>{ children }</ol>);
},
ul({ children, ...rest }) {
return (<ul { ...rest }>{ children }</ul>);
},
li({ children, ...rest }) {
return (<li className={"leading-normal [&_ul]:ml-[1rem] [&_ol]:ml-[1rem]"} { ...rest }>{ children }</li>);
},
img({ ...p }) {
return (
(p.alt === "icon") ?
<Icon className={"w-[.75rem] h-[.75rem] stroke-inherit-text"} icon={ p.src as Icons } />
:
<div className={`justify-center items-center w-full h-fit ${(p.alt?.endsWith("#lightonly")) ? "flex dark:hidden" : ((p.alt?.endsWith("#darkonly")) ? "hidden dark:flex" : "")}`}>
<Image src={p.src as string} alt={p.alt || ""} className="rounded-[6px]" />
</div>
)
},
Image(p) {
return (
<div className={`justify-center items-center w-full h-fit ${(p.alt?.endsWith("#lightonly")) ? "flex dark:hidden" : ((p.alt?.endsWith("#darkonly")) ? "hidden dark:flex" : "")}`}>
<img src={p.src as string} alt={p.alt || ""} className="rounded-[6px]" />
</div>
)
},
code({ children, className }) {
const sthlMatch = /language-(\w+)/.exec(className || "");
const keyMatch = /key-(\w+|\W+)/.exec(children as string || "");
const content = String(children).replace(/\n$/, "");
return (
(sthlMatch) ?
<CodeBlock language={ sthlMatch[1] } content={ content } />
:
(keyMatch) ?
<Keycap keytext={ keyMatch[1] } />
:
<InlineCode>{ children }</InlineCode>
);
},
del(p) {
return (<del className="text-noimportance" { ...p }></del>);
},
Table({ children, className, ...rest }) {
return (<Table { ...rest }>{ children }</Table>);
},
Thead({ children, ...rest }) {
return (<Thead { ...rest }>{ children }</Thead>);
},
Tbody({ children, ...rest }) {
return (<Tbody { ...rest }>{ children }</Tbody>);
},
Tr({ children, ...rest }) {
return (<Tr { ...rest }>{ children }</Tr>);
},
Th({ children, ...rest }) {
return (<Th { ...rest }>{ children }</Th>);
},
Td({ children, ...rest }) {
return (<Td { ...rest }>{ children }</Td>);
},
Blockquote({ children, className, title }) {
let header;
let blockStyle = "bg-default-hover border-default-hover";
return (
<blockquote className={`flex flex-col gap-[4px] p-[8px_12px] rounded-[6px] border leading-normal md:p-[12px_16px] ${blockStyle}`}>
<div className={"flex items-center gap-[6px]"}>
{ header }
</div>
<div>
{ children }
</div>
</blockquote>
)
},
Noti({ children, ...p }) {
return (<Noti {...p}>{ children }</Noti>);
},
a({children, href, ...rest }) {
if (href?.startsWith("/")) {
return (<Link href={ href }>{ children }</Link>);
}
else {
return <a href={ href } { ...rest }>{ children }</a>
}
},
Dl({ children, className, ...rest }) {
return (<dl className={`[&_dl]:mt-[8px] [&_dl]:mb-[4px] [&_dl]:first:mt-0 [&_dl]:pl-[1rem] [&_dl]:border-l-default [&_dl]:border-l-[2px] ${className || ""}`} { ...rest }>{ children }</dl>);
},
Dt({ children, className, ...rest }) {
return (<dt className={`mt-[8px] mb-[4px] first:mt-0 ${className || ""}`} { ...rest }>{ children }</dt>);
},
Dd({ children, className, ...rest }) {
return (<dd className={`flex flex-col gap-[4px] ml-[16px] mb-[16px] last:mb-0 ${className || ""}`} { ...rest }>{ children }</dd>);
},
AppCompatibility({ children, listTitle, ...rest }) {
return (<AppCompatibility listTitle={ listTitle } { ...rest }>{ children }</AppCompatibility>);
},
AppCompatibilityItem({ children, msgBot, arBot, sl, ...rest }) {
return (<AppCompatibilityItem msgBot={msgBot} arBot={arBot} sl={sl} { ...rest }>{ children }</AppCompatibilityItem>);
},
...components,
}
}