Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/pavel-pi/kiss-em.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmir Ribic <ribice@mac.local>2019-03-24 08:38:46 +0300
committerEmir Ribic <ribice@mac.local>2019-03-24 08:38:46 +0300
commit279ad18aa4970246239a308d5b49d80f67a15e00 (patch)
tree5db49b17fd9fd4e46edf4cdd2ff8e96441fd6e34
parentf02220a4de376f38ab8be747de0322e666a2dab3 (diff)
Various fixes
-rw-r--r--README.md1
-rw-r--r--layouts/_default/single.html6
-rw-r--r--static/js/copycode.js45
3 files changed, 25 insertions, 27 deletions
diff --git a/README.md b/README.md
index d3e0919..6e4d55e 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,7 @@ enableSocial = true # Adds OpenGraph and Twitter cards
homeTitle = "" # Title for home page
poweredby = true # Adds powered by hugo and kiss below Copyright
related = true # Includes related articles at the bottom of the article
+codeCopy = true # Add copy button above code blocks
[params.features]
disqusOnDemand = true # Load Disqus comments on click
diff --git a/layouts/_default/single.html b/layouts/_default/single.html
index d8d95fc..d6fb577 100644
--- a/layouts/_default/single.html
+++ b/layouts/_default/single.html
@@ -1,8 +1,5 @@
{{ partial "header" . }}
{{ partial "nav" . }}
-{{ if (findRE "class=\"highlight\"" .Content 1) }}
-<script src="/js/copycode.js"></script>
-{{ end }}
<section class="section">
<div class="container">
<div class="subtitle tags is-6 is-pulled-right">
@@ -26,5 +23,8 @@
{{ end}}
</div>
</section>
+{{ if (and (.Site.Params.Info.codeCopy) (findRE "<pre" .Content 1)) }}
+ <script src="/js/copycode.js"></script>
+{{ end }}
{{ partial "disqus" . }}
{{ partial "footer" . }}
diff --git a/static/js/copycode.js b/static/js/copycode.js
index c197e68..65758e7 100644
--- a/static/js/copycode.js
+++ b/static/js/copycode.js
@@ -1,34 +1,33 @@
/* global clipboard */
/* eslint-disable no-console */
-
function addCopyButtons(clipboard) {
- document.querySelectorAll('.highlight').forEach(function(codeBlock) {
+ document.querySelectorAll('pre > code').forEach(function (codeBlock) {
var button = document.createElement('button');
button.className = 'copy-code-button';
button.type = 'button';
button.innerText = 'Copy';
-
- button.addEventListener('click', function() {
- clipboard.writeText(codeBlock.innerText).then(
- function() {
- button.blur();
-
- button.innerText = 'Copied!';
- setTimeout(function() {
- button.innerText = 'Copy';
- }, 2000);
- },
- function(error) {
- button.innerText = 'Error';
- console.error(error);
- }
- );
+ button.addEventListener('click', function () {
+ clipboard.writeText(codeBlock.innerText).then(function () {
+ /* Chrome doesn't seem to blur automatically,
+ leaving the button in a focused state. */
+ button.blur();
+ button.innerText = 'Copied!';
+ setTimeout(function () {
+ button.innerText = 'Copy';
+ }, 2000);
+ }, function (error) {
+ button.innerText = 'Error';
+ });
});
-
- codeBlock.parentNode.insertBefore(button, codeBlock);
+ 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);
+ }
});
}
-
if (navigator && navigator.clipboard) {
addCopyButtons(navigator.clipboard);
} else {
@@ -37,10 +36,8 @@ if (navigator && navigator.clipboard) {
'https://cdnjs.cloudflare.com/ajax/libs/clipboard-polyfill/2.7.0/clipboard-polyfill.promise.js';
script.integrity = 'sha256-waClS2re9NUbXRsryKoof+F9qc1gjjIhc2eT7ZbIv94=';
script.crossOrigin = 'anonymous';
-
- script.onload = function() {
+ script.onload = function () {
addCopyButtons(clipboard);
};
-
document.body.appendChild(script);
} \ No newline at end of file