The Problem
I am building a Jekyll site with the minima theme to publish some tutorial online. The tutorial pages contain many code snippets, for example:
```javascript
/* Global scope: this code is executed once */
const redis = require('redis');
const host = <HOSTNAME>;
const port = <PORT>;
const password = <PASSWORD>;
...
```
I would like to add a "copy to clipboard" button to each code snippet (example), but not sure what's the right way to do it in Jekyll.
What have I tried
- Using clipboardjs.com. It requires a unique ID for each snippet, and I'm not sure how to implement this in Jekyll/Markdown.
- STFW
My question
How can I add a "Copy to Clipboard" button for code snippets in Jekyll?
Manually generate id's for each block of code with kramdown's Block Inline Attribute Lists, adding
{: #code-example-1}
after it.In your example:
That will generate:
using jquery
Code blocks use the
code
html element, if we detect it, then we load the js, traverse all code elements adding a custom id, and a button to copy their content. Finally initialize the Clipboard buttons.