diff --git a/README.md b/README.md index 1a4e872..9f84891 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,15 @@ ## Appearance and Layout - [ ] Pagination supported -- [ ] Easy-to-use and self-expanding table of contents - [ ] social links supported ## Extended Features - [ ] Search support - [ ] Twemoji supported - [ ] Automatically highlighting code -- [ ] Copy code to clipboard with one click +- [x] Copy code to clipboard with one click - [ ] Mathematical formula supported by KaTeX - [ ] Diagrams shortcode supported by mermaid - [ ] Interactive data visualization shortcode supported by ECharts - [ ] Dynamic scroll supported by native CSS - [ ] Cookie consent banner supported by cookieconsent -- [ ] Option to cache remote images \ No newline at end of file diff --git a/sass/_typography.scss b/sass/_typography.scss index 9e5f152..10fb207 100644 --- a/sass/_typography.scss +++ b/sass/_typography.scss @@ -26,6 +26,7 @@ code, pre { } pre { + position: relative; padding: 1.25rem; border-radius: 8px; overflow-x: auto; @@ -40,6 +41,42 @@ pre { } } +.copy-code-btn { + position: absolute; + top: 0.5rem; + right: 0.5rem; + display: flex; + align-items: center; + justify-content: center; + width: 32px; + height: 32px; + background: rgba(255, 255, 255, 0.1); + color: #cdd6f4; + border: 1px solid rgba(255, 255, 255, 0.2); + border-radius: 4px; + cursor: pointer; + opacity: 1; + transition: background 0.2s ease, color 0.2s ease; + + svg { + width: 16px; + height: 16px; + } + + &:hover { + background: rgba(255, 255, 255, 0.2); + color: #ffffff; + } + + &.copied { + background: var(--accent-color); + border-color: var(--accent-color); + color: var(--bg-color); + } +} + +// Button is always visible, no hover-to-reveal needed + code { background: var(--surface-color); padding: 0.2em 0.4em; diff --git a/static/js/copy-code.js b/static/js/copy-code.js new file mode 100644 index 0000000..b976a95 --- /dev/null +++ b/static/js/copy-code.js @@ -0,0 +1,50 @@ +document.addEventListener('DOMContentLoaded', () => { + // Select all
 elements that contain code blocks
+    const preBlocks = document.querySelectorAll('pre');
+
+    const copyIcon = '';
+    const checkIcon = '';
+
+    preBlocks.forEach((pre) => {
+        // Create the copy button
+        const button = document.createElement('button');
+        button.innerHTML = copyIcon;
+        button.className = 'copy-code-btn';
+        button.setAttribute('aria-label', 'Copy code');
+        button.setAttribute('title', 'Copy code');
+
+        // Add the button to the 
 block
+        // Assuming pre is position relative in CSS
+        pre.appendChild(button);
+
+        button.addEventListener('click', async () => {
+            // Find the  element within the 
+            const code = pre.querySelector('code');
+            if (!code) return;
+
+            // Get the text content, removing any leading/trailing whitespace
+            const text = code.innerText;
+
+            try {
+                // Wait for the clipboard API
+                await navigator.clipboard.writeText(text);
+
+                // Add visual feedback
+                button.innerHTML = checkIcon;
+                button.classList.add('copied');
+                button.setAttribute('aria-label', 'Copied');
+                button.setAttribute('title', 'Copied!');
+
+                // Revert back after 2 seconds
+                setTimeout(() => {
+                    button.innerHTML = copyIcon;
+                    button.classList.remove('copied');
+                    button.setAttribute('aria-label', 'Copy code');
+                    button.setAttribute('title', 'Copy code');
+                }, 2000);
+            } catch (err) {
+                console.error('Failed to copy code: ', err);
+            }
+        });
+    });
+});
diff --git a/templates/base.html b/templates/base.html
index c1f5d2c..e233bca 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -14,8 +14,8 @@
         {% include "partials/footer.html" %}
     
     
-    
     
+    
     {% block scripts %}{% endblock scripts %}