Skip to content

Instantly share code, notes, and snippets.

@dguo
Last active March 24, 2019 03:24
Show Gist options
  • Save dguo/1730d4bfeb370d92117e092311262bfa to your computer and use it in GitHub Desktop.
Save dguo/1730d4bfeb370d92117e092311262bfa to your computer and use it in GitHub Desktop.
blog - How to Add Copy to Clipboard Buttons to Code Blocks in Hugo - add-buttons
document.querySelectorAll('pre > code').forEach(function (codeBlock) {
var button = document.createElement('button');
button.className = 'copy-code-button';
button.type = 'button';
button.innerText = 'Copy';
var pre = codeBlock.parentNode;
if (pre.parentNode.classList.contains('highlight')) {
var highlight = pre.parentNode;
highlight.parentNode.insertBefore(button, highlight);
} else {
pre.parentNode.insertBefore(button, pre);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment