-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
RenderedMarkdown.tsx
168 lines (158 loc) · 4.41 KB
/
RenderedMarkdown.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
import {
Box,
Button,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
SystemStyleObject,
useDisclosure,
} from "@chakra-ui/react";
import { Prose } from "@nikolovlazar/chakra-ui-prose";
import NextLink from "next/link";
import { useTranslation } from "next-i18next";
import { memo, MouseEvent, useMemo, useState } from "react";
import ReactMarkdown from "react-markdown";
import type { ReactMarkdownOptions } from "react-markdown/lib/react-markdown";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import remarkGfm from "remark-gfm";
interface RenderedMarkdownProps {
markdown: string;
}
const sx: SystemStyleObject = {
overflowX: "auto",
pre: {
bg: "transparent",
},
code: {
before: {
content: `""`, // charka prose come with "`" by default
},
bg: "gray.300",
p: 0.5,
borderRadius: "2px",
_dark: {
bg: "gray.700",
},
},
"p:only-child": {
my: 0, // ovoid margin when markdown only render 1 p tag
},
p: {
whiteSpace: "pre-wrap",
mb: 4,
fontSize: "md",
fontWeight: "normal",
lineHeight: 6,
},
wordBreak: "break-word",
"> blockquote": {
borderInlineStartColor: "gray.300",
_dark: {
borderInlineStartColor: "gray.500",
},
},
"table tbody tr": {
borderBottomColor: "gray.400",
_dark: {
borderBottomColor: "gray.700",
},
},
"table thead tr": {
borderBottomColor: "gray.400",
borderBottomWidth: "1px",
_dark: {
borderBottomColor: "gray.700",
},
},
};
const plugins = [remarkGfm];
// eslint-disable-next-line react/display-name
const RenderedMarkdown = ({ markdown }: RenderedMarkdownProps) => {
const { isOpen, onOpen, onClose } = useDisclosure();
const [link, setLink] = useState<string | undefined>();
const components: ReactMarkdownOptions["components"] = useMemo(() => {
return {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
code({ node, inline, className, children, style, ...props }) {
const match = /language-(\w+)/.exec(className || "");
const lang = match ? match[1] : "";
return !inline ? (
<SyntaxHighlighter style={oneDark} language={lang} {...props}>
{String(children).replace(/\n$/, "")}
</SyntaxHighlighter>
) : (
<code className={className} {...props}>
{children}
</code>
);
},
a({ href, ...props }) {
if (!href) {
return props.children;
}
return (
<NextLink // use NextLink to handle locale if link is our internal, but it's really edge case.
{...props}
href={href}
target="_blank"
rel="noopener noreferrer"
onClick={(e: MouseEvent) => {
e.preventDefault();
setLink(href);
onOpen();
}}
></NextLink>
);
},
} as ReactMarkdownOptions["components"];
}, [onOpen]);
const linkProps = useMemo(() => {
return {
as: NextLink,
href: link,
target: "_blank",
rel: "noopener noreferrer",
};
}, [link]);
const { t } = useTranslation(["common", "message"]);
return (
<>
<MemorizedMarkdown components={components}>{markdown}</MemorizedMarkdown>
<Modal isOpen={isOpen} onClose={onClose} isCentered>
<ModalOverlay />
<ModalContent>
<ModalHeader>{t("message:confirm_open_link_header")}</ModalHeader>
<ModalCloseButton />
<ModalBody>
<div>{t("message:confirm_open_link_body")}</div>
<Box textDecoration="underline" {...linkProps}>
{link}
</Box>
</ModalBody>
<ModalFooter>
<Button variant="ghost" mr={3} onClick={onClose}>
{t("cancel")}
</Button>
<Button colorScheme="blue" as={NextLink} {...linkProps} onClick={onClose}>
{t("confirm")}
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
};
// eslint-disable-next-line react/display-name
const MemorizedMarkdown = memo((props: ReactMarkdownOptions) => {
return (
<Prose as="div" sx={sx}>
<ReactMarkdown remarkPlugins={plugins} {...props}></ReactMarkdown>
</Prose>
);
});
export default RenderedMarkdown;