From d850ea528d004207153a6c57e099b2eb46362adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Tue, 22 Feb 2022 20:36:59 +0100 Subject: [PATCH] clipboard: remove trailing line break from copied text #175 --- static/js/theme.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/static/js/theme.js b/static/js/theme.js index 92f7b2c266..f60a24cd3b 100644 --- a/static/js/theme.js +++ b/static/js/theme.js @@ -341,18 +341,23 @@ jQuery(function() { var clip = new ClipboardJS('.copy-to-clipboard-button', { text: function(trigger) { var text = $(trigger).prev('code').text(); + // remove a trailing line break, this may most likely + // come from the browser / Hugo transformation + text = text.replace(/\n$/, ''); + // removes leading $ signs from text in an assumption + // that this has to be the unix prompt marker - weird return text.replace(/^\$\s/gm, ''); } }); clip.on('success', function(e) { e.clearSelection(); - var inPre = $(e.trigger).parent().parent().prop('tagName') == 'PRE'; + var inPre = $(e.trigger).parent().prop('tagName') == 'PRE'; $(e.trigger).attr('aria-label', window.T_Copied_to_clipboard).addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's')); }); clip.on('error', function(e) { - var inPre = $(e.trigger).parent().parent().prop('tagName') == 'PRE'; + var inPre = $(e.trigger).parent().prop('tagName') == 'PRE'; $(e.trigger).attr('aria-label', fallbackMessage(e.action)).addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's')); $(document).one('copy', function(){ $(e.trigger).attr('aria-label', window.T_Copied_to_clipboard).addClass('tooltipped tooltipped-' + (inPre ? 'w' : 's'));