diff --git a/web/src/app/chat/message/CodeBlock.tsx b/web/src/app/chat/message/CodeBlock.tsx index 297ccf493..bfb7bdf12 100644 --- a/web/src/app/chat/message/CodeBlock.tsx +++ b/web/src/app/chat/message/CodeBlock.tsx @@ -39,15 +39,25 @@ export function CodeBlock({ props.node.position.start.offset, props.node.position.end.offset ); + codeText = codeText.trim(); // Remove the language declaration and trailing backticks const codeLines = codeText.split("\n"); - if (codeLines.length > 1 && codeLines[0].startsWith("```")) { + if ( + codeLines.length > 1 && + (codeLines[0].startsWith("```") || codeLines[0].trim().startsWith("```")) + ) { codeLines.shift(); // Remove the first line with the language declaration - if (codeLines[codeLines.length - 1] === "```") { + if ( + codeLines[codeLines.length - 1] === "```" || + codeLines[codeLines.length - 1]?.trim() === "```" + ) { codeLines.pop(); // Remove the last line with the trailing backticks } - codeText = codeLines.join("\n"); + + // remove leading whitespace from each line for nicer copy/paste experience + const trimmedCodeLines = codeLines.map((line) => line.trimStart()); + codeText = trimmedCodeLines.join("\n"); } }