Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
add copy code button and config param #122
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitrichius committed Jan 18, 2022
1 parent 4be0a7b commit 05206ec
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 3 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ params:
style: light-without-switcher
readMore: false
disableSummary: false
copyCodeButton: true # true by default
# utteranc.es support
utterancesRepo: "" # mandatory
utterancesTheme: "" # optional
Expand Down Expand Up @@ -131,9 +132,9 @@ Now enter [`localhost:1313`](http://localhost:1313/) in the address bar of your
### Dark Mode
Customize via `style` param in `params` section of config.
Options:
- `light-without-switcher` - light theme, without switcher, JS-free (by default)
- `dark-without-switcher` - dark theme, without switcher, JS-free
- `auto-without-switcher` - theme based on user system settings, without switcher, JS-free
- `light-without-switcher` - light theme, without switcher (by default)
- `dark-without-switcher` - dark theme, without switcher
- `auto-without-switcher` - theme based on user system settings, without switcher
- `light` - light theme by default, can be switched by user to dark theme and back. Theme settings are saved for user
- `dark` - dark theme by default, can be switched by user to light theme and back. Theme settings are saved for user
- `auto` - theme based on user system settings by default, can be switched by user to dark/light theme. Theme settings are saved for user (by default in example sites)
Expand Down
41 changes: 41 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,47 @@ ul.language-select > li, ul.footer-menu > li {
display: none;
}

/* Copy code */
.highlight {
position: relative;
}
.highlight pre {
padding-right: 75px;
}

.highlight:hover .highlight-copy-btn {
display: inline-block;
border: 1px solid var(--bg-color);
}

.highlight-copy-btn {
display: none;
position: absolute;
top: 7px;
right: 7px;
border: 0;
border-radius: 4px;
padding: 1px;
font-size: 0.7em;
line-height: 1.8;
color: #fff;
background-color: #777;
min-width: 25px;
text-align: center;
border-radius: 5px;
}
.highlight-copy-btn:hover {
transition-duration: .1s;
background-color: #666;
border: 1px solid var(--bq-color) !important;
}

.highlight-copy-btn,
.highlight-copy-btn svg {
vertical-align: middle;
margin: 8px;
}

/* Media Queries */

@media (max-width: 840px) {
Expand Down
64 changes: 64 additions & 0 deletions assets/js/copy-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
This file has been taken from following blogpost with some modifications:
https://koki-nakamura22.github.io/blog/2019/10/03/hugo-adding-copy-button/
Many thanks to Koki Nakamura!
*/

document.addEventListener("DOMContentLoaded", function(event) {
'use strict';

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

let svgCopyCode = '<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 25 25"><path d="M18 6v-6h-18v18h6v6h18v-18h-6zm-12 10h-4v-14h14v4h-10v10zm16 6h-14v-14h14v14z"/></svg>';
let svgSuccessCode = '<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 25 25"><path d="M20.285 2l-11.285 11.567-5.286-5.011-3.714 3.716 9 8.728 15-15.285z"/></svg>';
let svgFailCode = '<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 25 25"><path d="M23.954 21.03l-9.184-9.095 9.092-9.174-2.832-2.807-9.09 9.179-9.176-9.088-2.81 2.81 9.186 9.105-9.095 9.184 2.81 2.81 9.112-9.192 9.18 9.1z"/></svg>';

function changeIcon(el, innerHtml) {
el.innerHTML = innerHtml;
setTimeout(() => {
el.innerHTML = svgCopyCode;
}, 1000);
}

function selectText(node) {
let selection = window.getSelection();
let range = document.createRange();
if (node.childElementCount === 2) {
// Skip the title.
range.selectNodeContents(node.children[1]);
} else {
range.selectNodeContents(node);
}
selection.removeAllRanges();
selection.addRange(range);
return selection;
}

function addCopyButton(containerEl) {
let copyBtn = document.createElement("button");
copyBtn.className = "highlight-copy-btn";
copyBtn.innerHTML = svgCopyCode;

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

changeIcon(copyBtn, svgSuccessCode)
} catch(e) {
console && console.log(e);
changeIcon(copyBtn, svgFailCode)
}
});

containerEl.appendChild(copyBtn);
}

// Add copy button to code blocks
let highlightBlocks = document.getElementsByClassName('highlight');
Array.prototype.forEach.call(highlightBlocks, addCopyButton);
}, false);
1 change: 1 addition & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dateFormat = "2006-01-02"
paginationSinglePost = true
style = "auto"
readMore = false
copyCodeButton = true

# utteranc.es support
utterancesRepo = "" # mandatory
Expand Down
1 change: 1 addition & 0 deletions exampleSiteMultilingual/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dateFormat = "2006-01-02"
paginationSinglePost = true
style = "auto"
readMore = false
copyCodeButton = true

# utteranc.es support
utterancesRepo = "" # mandatory
Expand Down
4 changes: 4 additions & 0 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
{{ partial "favicons.html" . }}
{{ partial "resource.html" (dict "context" . "type" "css" "filename" "css/main.css") }}

{{ if .Site.Params.copyCodeButton | default true }}
{{ partial "resource.html" (dict "context" . "type" "js" "filename" "js/copy-code.js") }}
{{ end }}

{{ range .Site.Params.customJS -}}
{{ partial "resource.html" (dict "context" $ "type" "js" "filename" . ) }}
{{- end }}
Expand Down

0 comments on commit 05206ec

Please sign in to comment.