From 143da5bc0d6a49c4e58a904818e31cb38604a00d Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Wed, 23 Oct 2024 10:26:54 -0700 Subject: [PATCH] add copying for unrecognized languages (#2883) * add copying for unrecognized languages * k --- web/src/app/chat/lib.tsx | 1 - web/src/app/chat/message/CodeBlock.tsx | 1 + web/src/app/chat/message/codeUtils.ts | 24 +++++++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/web/src/app/chat/lib.tsx b/web/src/app/chat/lib.tsx index 3fbb9397ab..38fdac037a 100644 --- a/web/src/app/chat/lib.tsx +++ b/web/src/app/chat/lib.tsx @@ -348,7 +348,6 @@ export function getCitedDocumentsFromMessage(message: Message) { } export function groupSessionsByDateRange(chatSessions: ChatSession[]) { - console.log(chatSessions); const today = new Date(); today.setHours(0, 0, 0, 0); // Set to start of today for accurate comparison diff --git a/web/src/app/chat/message/CodeBlock.tsx b/web/src/app/chat/message/CodeBlock.tsx index c1f2f99397..5ab6b73b56 100644 --- a/web/src/app/chat/message/CodeBlock.tsx +++ b/web/src/app/chat/message/CodeBlock.tsx @@ -116,6 +116,7 @@ export const CodeBlock = memo(function CodeBlock({ {codeText && } )} + ); diff --git a/web/src/app/chat/message/codeUtils.ts b/web/src/app/chat/message/codeUtils.ts index d2aad29904..a9cd13944f 100644 --- a/web/src/app/chat/message/codeUtils.ts +++ b/web/src/app/chat/message/codeUtils.ts @@ -1,3 +1,5 @@ +import React from "react"; + export function extractCodeText( node: any, content: string, @@ -32,7 +34,27 @@ export function extractCodeText( codeText = formattedCodeLines.join("\n").trim(); } else { // Fallback if position offsets are not available - codeText = children?.toString() || null; + const extractTextFromReactNode = (node: React.ReactNode): string => { + if (typeof node === "string") return node; + if (typeof node === "number") return String(node); + if (!node) return ""; + + if (React.isValidElement(node)) { + const children = node.props.children; + if (Array.isArray(children)) { + return children.map(extractTextFromReactNode).join(""); + } + return extractTextFromReactNode(children); + } + + if (Array.isArray(node)) { + return node.map(extractTextFromReactNode).join(""); + } + + return ""; + }; + + codeText = extractTextFromReactNode(children); } return codeText || "";