-
Notifications
You must be signed in to change notification settings - Fork 4
/
mdx-components.tsx
86 lines (85 loc) · 2.28 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
import React from "react";
import { Box, Code, Heading, Image, Link, List, Text } from "@chakra-ui/react";
import { ExternalLinkIcon } from "@chakra-ui/icons";
import NextLink from "next/link";
import type { MDXComponents } from "mdx/types";
import { getAbsoluteURL, isRelativePath } from "./utils/path";
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
ul(props) {
return (
<List styleType="disc" mb={2} paddingInlineStart="2em" {...props} />
);
},
li(props) {
return <Box as="li" mb={1} {...props} />;
},
pre(props: any) {
return (
<Code
w="100%"
py={3}
px={1}
border="1px solid"
borderColor="gray.300"
borderRadius={2}
overflowX="auto"
{...props}
/>
);
},
code(props) {
return <Code as="code" fontSize="inherit" {...props} />;
},
p(props) {
return <Text as="p" my={2} lineHeight="tall" {...props} />;
},
h1(props) {
return <Heading as="h1" size="xl" my={5} {...props} />;
},
h2(props) {
return <Heading as="h2" size="lg" my={4} {...props} />;
},
h3(props) {
return <Heading as="h3" size="md" my={3} {...props} />;
},
h4(props) {
return <Heading as="h4" size="sm" my={2} {...props} />;
},
h5(props) {
return <Heading as="h5" size="sm" my={1} {...props} />;
},
img(props) {
const { src, alt } = props;
return <Image {...props} alt={alt || ""} src={getAbsoluteURL(src)} />;
},
a({ href, ...rest }) {
if (!href) {
return <Link {...rest} href="#" />;
}
const isRelative = isRelativePath(href);
if (isRelative) {
/**
* If the link is relative, use Next.js's `Link` component.
*/
return <Link {...rest} as={NextLink} href={href} />;
}
/**
* If the link is external, mark it as such.
*/
return (
<Link {...rest} href={href} position="relative" pr={4} isExternal>
{rest.children}
<ExternalLinkIcon
as="sup"
position="absolute"
top={0}
right={0}
fontSize="xs"
/>
</Link>
);
},
};
}