From d3a0442653d223221a0df7eda2b5d428440c73bb Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Feb 2026 20:06:01 +0000 Subject: [PATCH] fix: strip trailing 'Copy code' from clipboard when copying code blocks --- docs/source/_static/js/fix-copy.js | 18 ++++++++++++++++++ docs/source/conf.py | 1 + 2 files changed, 19 insertions(+) create mode 100644 docs/source/_static/js/fix-copy.js diff --git a/docs/source/_static/js/fix-copy.js b/docs/source/_static/js/fix-copy.js new file mode 100644 index 00000000..0999cc3e --- /dev/null +++ b/docs/source/_static/js/fix-copy.js @@ -0,0 +1,18 @@ +/* Fix: Prevent "Copy code" button label from appearing in clipboard content. + * + * sphinxawesome_theme inserts a copy button inside
 elements. When
+ * clipboard.js selects all children of the 
 to copy, the button's
+ * sr-only text ("Copy code") is included in the selection. This listener
+ * intercepts the copy event and strips that trailing label from the data
+ * written to the clipboard.
+ */
+document.addEventListener('copy', function (e) {
+    if (!e.clipboardData) { return; }
+    var selection = window.getSelection();
+    if (!selection) { return; }
+    var text = selection.toString();
+    var clean = text.replace(/\nCopy code\s*$/, '');
+    if (clean === text) { return; }
+    e.clipboardData.setData('text/plain', clean);
+    e.preventDefault();
+}, true);
diff --git a/docs/source/conf.py b/docs/source/conf.py
index d0787fea..2ec59aa8 100644
--- a/docs/source/conf.py
+++ b/docs/source/conf.py
@@ -116,6 +116,7 @@ html_theme_options = asdict(theme_options)
 # so a file named "default.css" will overwrite the builtin "default.css".
 html_static_path = ["_static"]
 html_css_files = ["css/custom.css"]
+html_js_files = ["js/fix-copy.js"]
 
 pygments_style = "lovelace"
 pygments_style_dark = "github-dark"