From 02cc211e9113ba95ce267555adb49098429cedfe Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Tue, 15 Oct 2024 16:22:40 -0700 Subject: [PATCH] improved code block copying (#2802) * improved code block copying * k --- web/src/app/chat/message/Messages.tsx | 2 +- web/src/app/chat/message/codeUtils.ts | 42 +++++++++++---------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/web/src/app/chat/message/Messages.tsx b/web/src/app/chat/message/Messages.tsx index 56b917164..181e00845 100644 --- a/web/src/app/chat/message/Messages.tsx +++ b/web/src/app/chat/message/Messages.tsx @@ -256,7 +256,7 @@ export const AIMessage = ({ () => ({ a: MemoizedLink, p: MemoizedParagraph, - code: ({ node, inline, className, children, ...props }: any) => { + code: ({ node, className, children, ...props }: any) => { const codeText = extractCodeText( node, finalContent as string, diff --git a/web/src/app/chat/message/codeUtils.ts b/web/src/app/chat/message/codeUtils.ts index 2aaae71bc..d2aad2990 100644 --- a/web/src/app/chat/message/codeUtils.ts +++ b/web/src/app/chat/message/codeUtils.ts @@ -4,40 +4,32 @@ export function extractCodeText( children: React.ReactNode ): string { let codeText: string | null = null; + if ( node?.position?.start?.offset != null && node?.position?.end?.offset != null ) { - codeText = content.slice( - node.position.start.offset, - node.position.end.offset - ); - codeText = codeText.trim(); + codeText = content + .slice(node.position.start.offset, node.position.end.offset) + .trim(); - // Find the last occurrence of closing backticks - const lastBackticksIndex = codeText.lastIndexOf("```"); - if (lastBackticksIndex !== -1) { - codeText = codeText.slice(0, lastBackticksIndex + 3); + // Match code block with optional language declaration + const codeBlockMatch = codeText.match(/^```[^\n]*\n([\s\S]*?)\n?```$/); + if (codeBlockMatch) { + codeText = codeBlockMatch[1]; } - // Remove the language declaration and trailing backticks + // Normalize indentation const codeLines = codeText.split("\n"); - if (codeLines.length > 1 && codeLines[0].trim().startsWith("```")) { - codeLines.shift(); // Remove the first line with the language declaration - if (codeLines[codeLines.length - 1]?.trim() === "```") { - codeLines.pop(); // Remove the last line with the trailing backticks - } + const minIndent = codeLines + .filter((line) => line.trim().length > 0) + .reduce((min, line) => { + const match = line.match(/^\s*/); + return Math.min(min, match ? match[0].length : min); + }, Infinity); - const minIndent = codeLines - .filter((line) => line.trim().length > 0) - .reduce((min, line) => { - const match = line.match(/^\s*/); - return Math.min(min, match ? match[0].length : 0); - }, Infinity); - - const formattedCodeLines = codeLines.map((line) => line.slice(minIndent)); - codeText = formattedCodeLines.join("\n"); - } + const formattedCodeLines = codeLines.map((line) => line.slice(minIndent)); + codeText = formattedCodeLines.join("\n").trim(); } else { // Fallback if position offsets are not available codeText = children?.toString() || null;