Skip to content

Commit 946790a

Browse files
authored
Add SyntaxHighlighter to code blocks returned by chatbot (#59)
* Move `prismaClient` into serverless inner scope * Add `SyntaxHighlighter` to `code` blocks
1 parent 2db8731 commit 946790a

File tree

5 files changed

+262
-16
lines changed

5 files changed

+262
-16
lines changed

components/chat/output.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import {
33
Avatar,
44
Box,
55
HStack,
6+
Icon,
7+
IconButton,
68
Stack,
79
useColorModeValue,
810
} from "@chakra-ui/react";
911
import ReactMarkdown from "react-markdown";
1012
import remarkGfm from "remark-gfm";
1113
import PropTypes from "prop-types";
1214
import { BeatLoader } from "react-spinners";
15+
import SyntaxHighlighter from "react-syntax-highlighter";
16+
import { dracula } from "react-syntax-highlighter/dist/esm/styles/prism";
17+
import { TbCopy } from "react-icons/tb";
1318

1419
export default function ChatOuput({ messages, isLoading, ...properties }) {
1520
const loaderColor = useColorModeValue("gray.100", "white");
@@ -42,7 +47,34 @@ export default function ChatOuput({ messages, isLoading, ...properties }) {
4247
>
4348
<Avatar src={agent ? "/chatbot.png" : "/user.png"} size="xs" />
4449
<Stack spacing={4} fontSize="sm">
45-
<ReactMarkdown remarkPlugins={[remarkGfm]}>
50+
<ReactMarkdown
51+
components={{
52+
pre: (pre) => {
53+
const codeChunk = pre.node.children[0].children[0].value;
54+
55+
const handleCopyCode = () => {
56+
navigator.clipboard.writeText(codeChunk);
57+
};
58+
59+
return (
60+
<Box position="relative">
61+
<IconButton
62+
position="absolute"
63+
top={4}
64+
right={2}
65+
size="sm"
66+
icon={<Icon as={TbCopy} fontSize="lg" />}
67+
onClick={() => handleCopyCode()}
68+
/>
69+
<SyntaxHighlighter style={dracula}>
70+
{pre.children[0].props.children[0]}
71+
</SyntaxHighlighter>
72+
</Box>
73+
);
74+
},
75+
}}
76+
remarkPlugins={[remarkGfm]}
77+
>
4678
{response}
4779
</ReactMarkdown>
4880
</Stack>

package-lock.json

+226
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"react-icons": "^4.8.0",
4141
"react-markdown": "^8.0.6",
4242
"react-spinners": "^0.13.8",
43+
"react-syntax-highlighter": "^15.5.0",
4344
"react-use": "^17.4.0",
4445
"remark-gfm": "^3.0.1",
4546
"typeorm": "^0.3.12"

0 commit comments

Comments
 (0)