Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Commit 52fcc93

Browse files
add code for copy button (#97)
Signed-off-by: Kapunahele Wong <kapunahelewong@gmail.com>
1 parent 6b156bb commit 52fcc93

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

assets/js/app.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,59 @@ function toggleNav() {
2727
leftnavToggle.classList.remove("open");
2828
}
2929
}
30+
31+
// Add copy button on code blocks
32+
// This code is kindly shared under the MIT License by Tom Spencer in their most excellent [tutorial](https://www.tomspencer.dev/blog/2018/09/14/adding-click-to-copy-buttons-to-a-hugo-powered-blog/).
33+
34+
(function() {
35+
'use strict';
36+
37+
if(!document.queryCommandSupported('copy')) {
38+
return;
39+
}
40+
41+
function confirmationMsg(el, msg) {
42+
el.textContent = msg;
43+
setTimeout(function() {
44+
el.textContent = "Copy";
45+
}, 1200);
46+
}
47+
48+
function selectText(node) {
49+
var selection = window.getSelection();
50+
var range = document.createRange();
51+
range.selectNodeContents(node);
52+
selection.removeAllRanges();
53+
selection.addRange(range);
54+
return selection;
55+
}
56+
57+
// Create a copy button
58+
function addCopyButton(containerEl) {
59+
var copyBtn = document.createElement("button");
60+
copyBtn.className = "highlight-copy-btn";
61+
copyBtn.textContent = "Copy";
62+
63+
var codeEl = containerEl.firstElementChild;
64+
copyBtn.addEventListener('click', function() {
65+
try {
66+
var selection = selectText(codeEl);
67+
document.execCommand('copy');
68+
selection.removeAllRanges();
69+
70+
confirmationMsg(copyBtn, 'Copied!')
71+
} catch(e) {
72+
console && console.log(e);
73+
confirmationMsg(copyBtn, 'Failed.')
74+
}
75+
});
76+
77+
containerEl.appendChild(copyBtn);
78+
}
79+
80+
// Add copy button to code blocks
81+
var highlightBlocks = document.getElementsByClassName('highlight');
82+
Array.prototype.forEach.call(highlightBlocks, addCopyButton);
83+
})();
84+
85+

assets/sass/custom.sass

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,26 @@ footer
293293
a
294294
color: lighten($primary-color, 30%)
295295
text-decoration: none
296+
297+
298+
299+
// styles for copy button on code snippets
300+
.highlight
301+
position: relative
302+
303+
.highlight pre
304+
padding-right: 75px
305+
306+
.highlight-copy-btn
307+
position: absolute
308+
top: 7px
309+
right: 7px
310+
border: 0
311+
border-radius: 4px
312+
font-size: 0.8rem
313+
color: #fff
314+
background-color: #777
315+
min-width: 55px
316+
317+
.highlight-copy-btn:hover
318+
background-color: #666

content/docs/demo.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,33 @@ Common languages used in Kubernetes documentation code blocks include:
206206
- `xml`
207207
- `none` (disables syntax highlighting for the block)
208208

209+
The following are some examples of inline code blocks:
210+
211+
```html
212+
<html>
213+
<div>
214+
<p>Sample HTML</p>
215+
</div>
216+
</html>
217+
```
218+
219+
```js
220+
function myFunction() {
221+
console.log("A JavaScript function");
222+
}
223+
```
224+
225+
```css
226+
body
227+
font-family: $font-family-body
228+
font-size: .9rem
229+
font-color: $font-color
230+
231+
.logo
232+
max-width: 200px
233+
```
234+
235+
209236
### Code blocks containing Hugo shortcodes
210237

211238
To show raw Hugo shortcodes as in the above example and prevent Hugo

0 commit comments

Comments
 (0)