-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add copy button and scroll for code blocks
Note that you need to add the related new params to `config.toml` by yourself. Otherwise, these feats can't be appended. closes #76
- Loading branch information
Showing
17 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copy Button for Code Blocks | ||
|
||
// References | ||
// 1. https://tomspencer.dev/blog/2018/09/14/adding-click-to-copy-buttons-to-a-hugo-powered-blog/ | ||
// 2. https://www.dannyguo.com/blog/how-to-add-copy-to-clipboard-buttons-to-code-blocks-in-hugo/ | ||
|
||
const copyText = '{{ i18n "copy" }}'; | ||
const copiedText = '{{ i18n "copied" }}'; | ||
|
||
document.querySelectorAll('.post-body > pre').forEach((e) => { | ||
e.outerHTML = `<div style="position: relative">${e.outerHTML}</div>`; | ||
}); | ||
|
||
function addCopyButtons(clipboard) { | ||
const divs = document.querySelectorAll('table.lntable, .highlight > pre, .post-body > div > pre'); | ||
|
||
divs.forEach((containerEl) => { | ||
containerEl.parentNode.style.position = 'relative'; | ||
|
||
const button = document.createElement('button'); | ||
button.className = 'copy-button'; | ||
button.type = 'button'; | ||
button.innerText = copyText; | ||
|
||
if (containerEl.classList.contains('lntable')) { | ||
var codeBlock = containerEl.querySelectorAll('.lntd')[1]; | ||
} else { | ||
var codeBlock = containerEl.querySelector('code'); | ||
} | ||
|
||
button.addEventListener('click', () => { | ||
clipboard.writeText(codeBlock.innerText).then(() => { | ||
/* Chrome doesn't seem to blur automatically, | ||
leaving the button in a focused state. */ | ||
button.blur(); | ||
|
||
button.innerText = copiedText; | ||
|
||
setTimeout(() => { | ||
button.innerText = copyText; | ||
}, 1000); | ||
}).catch((error) => { | ||
button.innerText = 'Error'; | ||
|
||
console.error(error); | ||
}); | ||
}); | ||
|
||
containerEl.appendChild(button); | ||
}); | ||
} | ||
|
||
if (navigator && navigator.clipboard) { | ||
addCopyButtons(navigator.clipboard); | ||
} else { | ||
const script = document.createElement('script'); | ||
script.src = '{{ partial "third-party/clipboard-polyfill.html" | safeJS }}'; | ||
script.defer = true; | ||
script.onload = function() { | ||
addCopyButtons(clipboard); | ||
}; | ||
|
||
document.body.appendChild(script); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...leSite/resources/_gen/assets/scss/scss/main.scss_0437a1669ac8a9868f1dd1fe846b91a6.content
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
exampleSite/resources/_gen/assets/scss/scss/main.scss_0437a1669ac8a9868f1dd1fe846b91a6.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Target":"css/meme.min.8ebc75cc2e0d5e4beb6ce2cd77cc927ddff5609c3822cb96860098df7115a001.css","MediaType":"text/css","Data":{"Integrity":"sha256-jrx1zC4NXkvrbOLNd8ySfd/1YJw4IsuWhgCY33EVoAE="}} |
1 change: 0 additions & 1 deletion
1
...leSite/resources/_gen/assets/scss/scss/main.scss_b04c21e44835e7dcdc8cb03d103b5ef1.content
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
exampleSite/resources/_gen/assets/scss/scss/main.scss_b04c21e44835e7dcdc8cb03d103b5ef1.json
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://cdn.jsdelivr.net/npm/clipboard-polyfill@2.8.6/dist/clipboard-polyfill.min.js |