diff --git a/web/src/app/chat/message/CodeBlock.tsx b/web/src/app/chat/message/CodeBlock.tsx index c6798333e..d7007ce42 100644 --- a/web/src/app/chat/message/CodeBlock.tsx +++ b/web/src/app/chat/message/CodeBlock.tsx @@ -64,9 +64,15 @@ export function CodeBlock({ codeLines.pop(); // Remove the last line with the trailing backticks } - // remove leading whitespace from each line for nicer copy/paste experience - const trimmedCodeLines = codeLines.map((line) => line.trimStart()); - codeText = trimmedCodeLines.join("\n"); + 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"); } }