Skip to content

Commit

Permalink
Add copy button files for main blog
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel authored Dec 8, 2023
1 parent 04c932f commit 036b85b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions static/css/blog.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ code.hljs {
.hljs-meta .hljs-keyword,.hljs-meta-keyword {
font-weight: 700
}

.hljs-copy {
position: absolute;
right: 0px;
color: black;
background-color: #CCC;
}
17 changes: 17 additions & 0 deletions static/js/copybutton-blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var snippets = document.getElementsByTagName('pre');
var code_snippets = [];

for (i = 0; i < snippets.length; i++) {
code_snippets[i] = snippets[i].innerText;
snippets[i].classList.add('hljs'); // append copy button to pre tag
snippets[i].innerHTML = '<button class="hljs-copy" id="' + i + '">Copy</button>' + snippets[i].innerHTML; // append copy button
snippets[i].getElementsByClassName('hljs-copy')[0].addEventListener("click", function () {
this.innerText = 'Copying';
navigator.clipboard.writeText(code_snippets[this.id]);
this.innerText = 'Copied!';
button = this;
setTimeout(function () {
button.innerText = 'Copy';
}, 1000)
});
}

0 comments on commit 036b85b

Please sign in to comment.