Skip to content

Commit

Permalink
Add SyntaxHighlighter to code blocks returned by chatbot (#59)
Browse files Browse the repository at this point in the history
* Move `prismaClient` into serverless inner scope

* Add `SyntaxHighlighter` to `code` blocks
  • Loading branch information
homanp authored Apr 9, 2023
1 parent 2db8731 commit 946790a
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 16 deletions.
34 changes: 33 additions & 1 deletion components/chat/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import {
Avatar,
Box,
HStack,
Icon,
IconButton,
Stack,
useColorModeValue,
} from "@chakra-ui/react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import PropTypes from "prop-types";
import { BeatLoader } from "react-spinners";
import SyntaxHighlighter from "react-syntax-highlighter";
import { dracula } from "react-syntax-highlighter/dist/esm/styles/prism";
import { TbCopy } from "react-icons/tb";

export default function ChatOuput({ messages, isLoading, ...properties }) {
const loaderColor = useColorModeValue("gray.100", "white");
Expand Down Expand Up @@ -42,7 +47,34 @@ export default function ChatOuput({ messages, isLoading, ...properties }) {
>
<Avatar src={agent ? "/chatbot.png" : "/user.png"} size="xs" />
<Stack spacing={4} fontSize="sm">
<ReactMarkdown remarkPlugins={[remarkGfm]}>
<ReactMarkdown
components={{
pre: (pre) => {
const codeChunk = pre.node.children[0].children[0].value;

const handleCopyCode = () => {
navigator.clipboard.writeText(codeChunk);
};

return (
<Box position="relative">
<IconButton
position="absolute"
top={4}
right={2}
size="sm"
icon={<Icon as={TbCopy} fontSize="lg" />}
onClick={() => handleCopyCode()}
/>
<SyntaxHighlighter style={dracula}>
{pre.children[0].props.children[0]}
</SyntaxHighlighter>
</Box>
);
},
}}
remarkPlugins={[remarkGfm]}
>
{response}
</ReactMarkdown>
</Stack>
Expand Down
226 changes: 226 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"react-icons": "^4.8.0",
"react-markdown": "^8.0.6",
"react-spinners": "^0.13.8",
"react-syntax-highlighter": "^15.5.0",
"react-use": "^17.4.0",
"remark-gfm": "^3.0.1",
"typeorm": "^0.3.12"
Expand Down
Loading

1 comment on commit 946790a

@vercel
Copy link

@vercel vercel bot commented on 946790a Apr 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchain-ui – ./

langchain-ui-git-main-homanp.vercel.app
langchain-ui-homanp.vercel.app
langchain-ui.vercel.app

Please sign in to comment.