Add Copy Button For Hexo Code Block
I am using Clipboard.js to achieve this.
Step 1:
Create a clipboard-use.js
file in your JavaScript folder. In my case, the path is:/var/www/myblog/themes/landscape/source/js
1 | !function (window, document) { |
Step 2:
Add the script tag to the after-footer
template. Make sure to place it below the jQuery script tag.
In my case, the file is located at:/var/www/myblog/themes/landscape/layout/_partial/after-footer.ejs
1 | <!-- Load Clipboard.js from CDN --> |
Step 3:
Add the necessary CSS.
For this demo, I have added the styles in: /var/www/myblog/themes/landscape/source/css/style.styl
1 | //copy button in code block |
Step 4:
Run the following commands in your terminal:
1 | cd /var/www/myblog/ |
Done 🎉
Your Hexo blog should now display a Copy button in the code blocks, allowing users to quickly copy the code. 🚀