From 9a3613eb44e51ec7f477e4c213c92a28a2050cbb Mon Sep 17 00:00:00 2001 From: Weves Date: Mon, 27 May 2024 14:09:32 -0700 Subject: [PATCH] Fix unknown languages causing the chat to crash --- web/src/app/chat/message/CodeBlock.tsx | 21 +++++++++++++++++++++ web/src/app/chat/message/Messages.tsx | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/web/src/app/chat/message/CodeBlock.tsx b/web/src/app/chat/message/CodeBlock.tsx index 8c6d984a4c..297ccf4933 100644 --- a/web/src/app/chat/message/CodeBlock.tsx +++ b/web/src/app/chat/message/CodeBlock.tsx @@ -51,6 +51,27 @@ export function CodeBlock({ } } + // handle unknown languages. They won't have a `node.position.start.offset` + if (!codeText) { + const findTextNode = (node: any): string | null => { + if (node.type === "text") { + return node.value; + } + let finalResult = ""; + if (node.children) { + for (const child of node.children) { + const result = findTextNode(child); + if (result) { + finalResult += result; + } + } + } + return finalResult; + }; + + codeText = findTextNode(props.node); + } + const handleCopy = () => { if (!codeText) { return; diff --git a/web/src/app/chat/message/Messages.tsx b/web/src/app/chat/message/Messages.tsx index 47623f7e5c..8a2d461c4a 100644 --- a/web/src/app/chat/message/Messages.tsx +++ b/web/src/app/chat/message/Messages.tsx @@ -236,7 +236,7 @@ export const AIMessage = ({ ), }} remarkPlugins={[remarkGfm]} - rehypePlugins={[rehypePrism]} + rehypePlugins={[[rehypePrism, { ignoreMissing: true }]]} > {content}