Fix code pasting formatting (#2033)

* fix pasting formatting

* add back small comments
This commit is contained in:
pablodanswer 2024-08-04 09:56:48 -07:00 committed by GitHub
parent 0261d689dc
commit 876feecd6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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");
}
}