Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved code copy behaviour #82

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions private/pages/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,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