Jekyll Code snippet copy-to-clipboard button
Asked Answered
S

2

13

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?

Stud answered 3/1, 2018 at 13:17 Comment(1)
I think you should perhaps create the buttons and id's using javascript. I made this very contained example. The div elements are elements that I want to copy text from. The jquery code goes through every single div element adding a unique id and the a button, which points to that unique id and finally it calls the clipboard.js.Guildroy
A
6

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:

```javascript
/* Global scope: this code is executed once */
const redis = require('redis');

const host = <HOSTNAME>;
const port = <PORT>;
const password = <PASSWORD>;

...
```
{: #code-example-1}

That will generate:

<div id="code-example-1" class="language-javascript highlighter-rouge">
....
</div>

using jquery

Code blocks use thecode 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.

{% if page.content contains "code" %}
<script>
<!-- clipboard.js code -->
</script>
{% endif %}

// get all <code> elements
var allCodeBlocksElements = $( "code" );

allCodeBlocksElements.each(function(i) {
 	// add different id for each code block

	// target	
  var currentId = "codeblock" + (i + 1);
  $(this).attr('id', currentId);
     
  //trigger
  var clipButton = '<button class="btn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
     $(this).after(clipButton);
  });
 
  new Clipboard('.btn');
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js"></script>


<code>print("Club Nacional de Football")</code>
<br>
<code>print("is a sports institution")</code>
<br>
<code>print("from Uruguay")</code>
Adelbert answered 3/1, 2018 at 13:54 Comment(3)
a. Is there a way to automate this? b. I still have to manually add the copy button and create the event somehowStud
@AdamMatan you can automate it with a plugin, for example, using the :post_render hook, generate the code for each language block, or create a tag that generates it.Adelbert
@AdamMatan I've updated the answer including the code to automatically generate code blocks id's and their buttons to copy their content.Adelbert
Z
1

Let's use includes.

tuto.md

---
front matter things here
---
{%- capture code -%}
/* Some js code */
const redis = require('redis');
const host = <HOSTNAME>;
{%- endcapture -%}

{% include code_snippet.md code=code language='javascript' %}

{%- capture code -%}
# Some ruby code
t = Time.now
t.succ  
{%- endcapture -%}

{% include code_snippet.md code=code language='ruby' %}

_includes/code_snippet.md

{% assign code = include.code %}
{% assign language = include.language %}

``` {{ language }}
{{ code }}
```
{% assign nanosecond = "now" | date: "%N" %}
<textarea id="code{{ nanosecond }}" style="display:none;">{{ code | xml_escape }}</textarea>
<button id="copybutton{{ nanosecond }}" data-clipboard-target="#code{{ nanosecond }}">
  Copy to clipboard
</button>

<script>
var copybutton = document.getElementById('copybutton{{ nanosecond }}');
var clipboard{{ nanosecond }} = new Clipboard(copybutton);

clipboard{{ nanosecond }}.on('success', function(e) {
    console.log(e);
});
clipboard{{ nanosecond }}.on('error', function(e) {
    console.log(e);
});
</script>
Zolly answered 10/1, 2018 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.