From 911837f678588f652e335f1ddc3d382f34ca611c Mon Sep 17 00:00:00 2001 From: ShUl0w <37832993+ShUl0w@users.noreply.github.com> Date: Thu, 8 Aug 2024 07:33:29 +0200 Subject: [PATCH] Improved code copy behaviour --- private/pages/interface.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/private/pages/interface.php b/private/pages/interface.php index d586d98..d5e8ce6 100644 --- a/private/pages/interface.php +++ b/private/pages/interface.php @@ -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();