Comments (2)
Hi!
I looked into adding copy button before, but as I try to keep everything simple I decided against it, it was too much of support for icons, code and light-dark theme,
But I can reconsider is simple (code wise) solution with arise.
from hugo-book.
Hello @alex-shpak and @djsamperi,
I had the same problem. I made the following solution myself. The implementation is quite simple and so is the layout. Maybe you can add it to the original repo.
Greetings
@eskopp
Solution
Dark
Light
Source Code
JavaScript Code
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre code').forEach((block) => {
const button = document.createElement('button');
button.className = 'copy-button';
button.textContent = 'Copy';
button.addEventListener('click', () => {
const range = document.createRange();
range.selectNodeContents(block);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
button.textContent = 'Copied!';
setTimeout(() => { button.textContent = 'Copy'; }, 2000);
});
block.parentNode.insertBefore(button, block);
});
});
</script>
CSS Code
.copy-button {
position: absolute;
right: 10px;
top: 10px;
padding: 5px 10px;
border: none;
border-radius: 4px;
cursor: pointer;
background-color: #6c757d;
color: white;
font-family: Arial, sans-serif;
font-size: 0.875rem;
box-shadow: 0 2px 4px rgba(0,0,0,0.15);
transition: background-color 0.3s, box-shadow 0.3s;
}
.copy-button:hover {
background-color: #5a6268;
box-shadow: 0 2px 5px rgba(0,0,0,0.25);
}
pre {
position: relative;
background-color: #f8f9fa;
border: 1px solid #e1e1e1;
padding: 16px;
border-radius: 4px;
overflow: auto;
}
from hugo-book.
Related Issues (20)
- links in shortcodes 'columns' do not work HOT 4
- change formatting of markdown heading HOT 5
- Would consider doing an 11ty version?
- markup highlight not work HOT 1
- Curious to know how to control the part of each blog that will display in the homepage of posts HOT 3
- Title and date of posts are not shown HOT 1
- Any experience about using Giscus comment in the hugo-book theme? HOT 2
- Pager for post collections and archives
- numbered plugin ignores chapters
- Highlighting Color Scheme and Adding Line Numbers to Expand Shortcodes
- Expand shortcode is deprecated. Use 'details' instead.
- no such template "_internal/google_analytics_async.html" HOT 1
- IsMultiLingual error on build HOT 5
- Odd menu issue w/ Book upgrade
- youtube in tab broken
- Strange issues on build...
- Edit Page (BookEditPath) function not working for multilanguage projects
- where to hide categories and tages and these three dots?
- blog
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hugo-book.