]> source.dussan.org Git - gitea.git/commitdiff
Strip trailing newline in markdown code copy (#29019)
authorsilverwind <me@silverwind.io>
Thu, 1 Feb 2024 21:01:48 +0000 (22:01 +0100)
committerGitHub <noreply@github.com>
Thu, 1 Feb 2024 21:01:48 +0000 (15:01 -0600)
Behaviour now matches GH. Safeguard added in the for loop because
`textContent` may be null in which case it does not make sense to render
the copy button.

web_src/js/markup/codecopy.js

index a12802ef734799fec9f4ae0d507f6bd0050138f1..078d741253860fb7dcf548b2b6a976870902c423 100644 (file)
@@ -12,8 +12,10 @@ export function renderCodeCopy() {
   if (!els.length) return;
 
   for (const el of els) {
+    if (!el.textContent) continue;
     const btn = makeCodeCopyButton();
-    btn.setAttribute('data-clipboard-text', el.textContent);
+    // remove final trailing newline introduced during HTML rendering
+    btn.setAttribute('data-clipboard-text', el.textContent.replace(/\r?\n$/, ''));
     el.after(btn);
   }
 }