Skip to content

Commit

Permalink
fix: copy button with or without lineNos (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
WingLim authored May 30, 2022
1 parent 92e052d commit 6c4173f
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions assets/ts/copyButton.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
// This file is copy from https://github.com/CaiJimmy/hugo-theme-stack/blob/24915a912f23e8c0a21aa156714ea7f071469fdb/assets/ts/main.ts#L61-L87
// This file is copy from https://github.com/CaiJimmy/hugo-theme-stack/blob/c1fcec95a64c6787cd08ed9f5194306642058b7a/assets/ts/main.ts#L65-L92
// All right reserved by Jimmy Cai

const codeBlocks = document.querySelectorAll('.article-post .highlight');
const highlights = document.querySelectorAll('.article-post div.highlight');
const copyText = `Copy`,
copiedText = `Copied!`;

export let renderCopyButton = function() {
codeBlocks.forEach(codeBlock => {
highlights.forEach(highlight => {
const copyButton = document.createElement('button')
copyButton.innerHTML = copyText
copyButton.classList.add('copyCodeButton');
codeBlock.appendChild(copyButton);
highlight.appendChild(copyButton);

const pre = codeBlock.getElementsByTagName('pre');
// This theme's code block has line number, so the second is where the
// real code locate
let codeIndex = 0
if (pre.length == 2) {
codeIndex = 1
}
const code = pre[codeIndex].textContent;
const codeBlock = highlight.querySelector('code[data-lang]');
if (!codeBlock) return;

copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(code)
navigator.clipboard.writeText(codeBlock.textContent)
.then(() => {
copyButton.textContent = copiedText;

Expand Down

0 comments on commit 6c4173f

Please sign in to comment.