Skip to content

Commit

Permalink
Improved code copy behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
ShUl0w committed Aug 8, 2024
1 parent 6ae6684 commit 911837f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions private/pages/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,30 @@ function CopyContentToClipboard(target) {
table.parentNode.replaceChild(textNode, table);
});

// Remove code language from copy content
if(clone.tagName === "CODE") {
const firstChildNode = clone.firstChild;
const lastChildNode = clone.lastChild;

if(firstChildNode && firstChildNode.nodeName === "#text") {
// Remove the first node, as it contains the code language
// It is possible that the code language is followed by actual code, so check for that
if(firstChildNode.textContent.includes("\n")) {
firstChildNode.textContent = firstChildNode.textContent.split("\n")[1];
} else {
clone.removeChild(firstChildNode);
}
}

if (lastChildNode && lastChildNode.nodeName === "#text") {
// In some cases, there might be markdown content at the end of the code block due to formatting errors
const re = "\n?```"
if(lastChildNode.textContent.match(re)) {
lastChildNode.textContent = lastChildNode.textContent.replace("\n```", "");
}
}
}


// Get the text content of the modified clone
const msgTxt = clone.textContent.trim();
Expand Down

0 comments on commit 911837f

Please sign in to comment.