From b71850ea731ffd04f87f32fe25373a9c103ed669 Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 1 Feb 2024 22:01:48 +0100 Subject: [PATCH] Strip trailing newline in markdown code copy (#29019) 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/web_src/js/markup/codecopy.js b/web_src/js/markup/codecopy.js index a12802ef7..078d74125 100644 --- a/web_src/js/markup/codecopy.js +++ b/web_src/js/markup/codecopy.js @@ -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); } }