Fix code block copy

This commit is contained in:
Weves 2024-06-07 15:10:13 -07:00 committed by Chris Weaver
parent c9160c705a
commit d56e6c495a

View File

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