From d8386366d9b5b6fed748adba0715b55c74e91327 Mon Sep 17 00:00:00 2001 From: "sp.wack" <83104063+amanape@users.noreply.github.com> Date: Sat, 27 Apr 2024 15:21:29 +0300 Subject: [PATCH] feature(frontend): improve chat overflow handling and syntax highlighting (#1402) * improve overflow handling and syntax highlighting * add reference to gh page --- frontend/src/components/ChatInterface.tsx | 7 +++--- frontend/src/components/markdown/code.tsx | 28 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 frontend/src/components/markdown/code.tsx diff --git a/frontend/src/components/ChatInterface.tsx b/frontend/src/components/ChatInterface.tsx index e242270a29..e113244f5c 100644 --- a/frontend/src/components/ChatInterface.tsx +++ b/frontend/src/components/ChatInterface.tsx @@ -12,6 +12,7 @@ import { import { Message } from "#/state/chatSlice"; import { RootState } from "#/store"; import ChatInput from "./ChatInput"; +import { code } from "./markdown/code"; interface IChatBubbleProps { msg: Message; @@ -57,10 +58,10 @@ function ChatBubble({ msg }: IChatBubbleProps): JSX.Element { className={`flex mb-0 min-w-0 ${msg?.sender === "user" && "flex-row-reverse ml-auto"}`} >
- {msg?.content} + {msg?.content}
@@ -102,7 +103,7 @@ function MessageList(): JSX.Element { return (
-
+
{newChatSequence.map((msg, index) => ( ))} diff --git a/frontend/src/components/markdown/code.tsx b/frontend/src/components/markdown/code.tsx new file mode 100644 index 0000000000..fff62d160f --- /dev/null +++ b/frontend/src/components/markdown/code.tsx @@ -0,0 +1,28 @@ +import React from "react"; +import { ExtraProps } from "react-markdown"; +import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; +import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism"; + +// See https://github.com/remarkjs/react-markdown?tab=readme-ov-file#use-custom-components-syntax-highlight + +/** + * Component to render code blocks in markdown. + */ +export function code({ + children, + className, +}: React.ClassAttributes & + React.HTMLAttributes & + ExtraProps) { + const match = /language-(\w+)/.exec(className || ""); // get the language + + if (!match) { + return {children}; + } + + return ( + + {String(children).replace(/\n$/, "")} + + ); +}