-
Notifications
You must be signed in to change notification settings - Fork 91
/
CodeBlock.tsx
44 lines (42 loc) · 1.07 KB
/
CodeBlock.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
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { vs } from "react-syntax-highlighter/dist/esm/styles/prism";
export const CodeBlock = ({
className,
children,
}: {
className: string;
children: string;
}) => {
let lang = "text";
if (className && className.startsWith("lang-")) {
lang = className.replace("lang-", "");
return (
<SyntaxHighlighter language={lang} style={vs} wrapLongLines>
{children}
</SyntaxHighlighter>
);
} else {
// inline code block
return (
<span
style={{
fontFamily: "courier",
color: "purple",
backgroundColor: "#fff8e1",
}}
>
{children}
</span>
);
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const PreBlock = ({ children, ...rest }: { children: any }) => {
if ("type" in children && children.type === "code") {
return CodeBlock({
className: children.props.className,
children: children.props.children,
});
}
return <pre {...rest}>{children}</pre>;
};