How to Add a Copy Code Button to Hugo Static Website?

Web Development  
2 min read
 
How to Add a Copy Code Button to Hugo Static Website?
Icon

How to add a copy-button to your Hugo static website?

1. Let’s create css file in /static/css/copy-code-button.css:

.highlight-wrapper {
  display: block;
}

.highlight-wrapper .lntd pre {
  padding: 0;
}

.chroma .lntd pre {
  border: 0px solid #ccc;
  padding-right: 0px;
  padding-left: 0px;
  padding-top: 30px;
  padding-bottom: 18px;
}

.highlight {
  position: relative;
  z-index: 0;
  padding: 0;
  margin: 25px 0px 40px 0px;
  border-radius: 4px;
}

.highlight>.chroma {
  font-size: 85%;
  line-height: 25px;
  /* padding-left: 40px; */
  margin-bottom: 30px;
  position: static;
  z-index: 1;
  border-top-left-radius: 0px;
  border-top-right-radius: 0px;
  border-bottom-left-radius: 0px;
  border-bottom-right-radius: 0px;
  overflow: auto;
}

.copy-code-button:hover {
  cursor: pointer;
  background-color: #999999;
}

.copy-code-button:active {
  background-color: green;
}

.highlight pre {
  margin: 0;
}

.copyable-text-area {
  position: absolute;
  height: 0;
  z-index: -1;
  opacity: .01;
}

.chroma [data-lang]:before {
  position: absolute;
  z-index: 0;
  line-height: 17px;
  top:-17px;
  left: 0;
  content: attr(data-lang);
  font-size: 12px;
  color: #999999;
  background-color: #0F0F0F;
  padding: 0px 3px 0px 3px;
  border-radius: 2px 2px 0px 0px;
  letter-spacing: 0.5px;
  opacity: 0.9;
}

.copy-code-button {
  position: absolute;
  z-index: 2;
  right: 0px;
  top: 0px;
  line-height: 10px;
  font-size: 13px;
  letter-spacing: 0.5px;
  width: 68px;
  color: white;
  background-color: #515151;
  border: 1.25px solid #232326;
  border-radius: 0px 0px 0px 0px;
  white-space: nowrap;
  padding: 5px 6px 7px 6px;
  cursor: pointer;
}

.light .copy-code-button {
  border-color: transparent;
}

2. Now let’s create a javascript file called: copy-code-button.js in /static/js/copy-code-button.js

(function() {
  'use strict';

  if (!document.queryCommandSupported('copy')) {
    return;
  }

  function flashCopyMessage(el, msg) {
    el.textContent = msg;
    setTimeout(function() {
      el.textContent = "Copy";
    }, 2000);
  }
  
  function selectText(node) {
    var selection = window.getSelection();
    var range = document.createRange();
    range.selectNodeContents(node);
    selection.removeAllRanges();
    selection.addRange(range);
    return selection;
  }

  function addCopyButton(containerEl) {
    var copyBtn = document.createElement("button");
    copyBtn.className = "copy-code-button";
    copyBtn.textContent = "Copy";

    var codeEl = containerEl.firstElementChild;
    copyBtn.addEventListener('click', function() {
      try {
        var selection = selectText(codeEl);
        document.execCommand('copy');
        selection.removeAllRanges();

        flashCopyMessage(copyBtn, 'Copied!')
      } catch(e) {
        console && console.log(e);
        flashCopyMessage(copyBtn, 'Failed :\'(')
      }
    });

    containerEl.appendChild(copyBtn);
  }

  // Add copy button to code blocks
  var highlightBlocks = document.getElementsByClassName('highlight');
  for (var i = 0; i < highlightBlocks.length; i++) {
    addCopyButton(highlightBlocks[i]);
  }
})();

3. Finally lets add our Go command to our /layouts/_default/baseof.html for it to lazy load.

{{ if (findRE "<pre" .Content 1) }}
    <script src="/js/copy-code-button.js"></script>
{{ end }}

4. usage

Hugo will automatically add copy button when you use code fences.

Example:
```cmd
your code goes here
```

Check out HUGO doc for:
Icon Hugo Syntax highlighting

Tags:  #hugo