Skip to content

Commit 6b53888

Browse files
committed
ShowCodeCopyButtons: true
1 parent ec8dfa7 commit 6b53888

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

hugo.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ baseURL: https://winterarch.github.io/
22
languageCode: zh-CN
33
params:
44
comments: true
5+
ShowCodeCopyButtons: true
56
title: WinterArch's WebLog
67
theme: ["PaperMod"]

public/posts/helloworld/index.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,53 @@ <h1 class="post-title entry-hint-parent">
271271
})
272272

273273
</script>
274+
<script>
275+
document.querySelectorAll('pre > code').forEach((codeblock) => {
276+
const container = codeblock.parentNode.parentNode;
277+
278+
const copybutton = document.createElement('button');
279+
copybutton.classList.add('copy-code');
280+
copybutton.innerHTML = 'copy';
281+
282+
function copyingDone() {
283+
copybutton.innerHTML = 'copied!';
284+
setTimeout(() => {
285+
copybutton.innerHTML = 'copy';
286+
}, 2000);
287+
}
288+
289+
copybutton.addEventListener('click', (cb) => {
290+
if ('clipboard' in navigator) {
291+
navigator.clipboard.writeText(codeblock.textContent);
292+
copyingDone();
293+
return;
294+
}
295+
296+
const range = document.createRange();
297+
range.selectNodeContents(codeblock);
298+
const selection = window.getSelection();
299+
selection.removeAllRanges();
300+
selection.addRange(range);
301+
try {
302+
document.execCommand('copy');
303+
copyingDone();
304+
} catch (e) { };
305+
selection.removeRange(range);
306+
});
307+
308+
if (container.classList.contains("highlight")) {
309+
container.appendChild(copybutton);
310+
} else if (container.parentNode.firstChild == container) {
311+
312+
} else if (codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "TABLE") {
313+
314+
codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(copybutton);
315+
} else {
316+
317+
codeblock.parentNode.appendChild(copybutton);
318+
}
319+
});
320+
</script>
274321
</body>
275322

276323
</html>

public/posts/基于mvvm模式手把手写出可测试性强的swiftui代码/index.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,53 @@ <h1 id="总结">总结<a hidden class="anchor" aria-hidden="true" href="#总结"
586586
})
587587

588588
</script>
589+
<script>
590+
document.querySelectorAll('pre > code').forEach((codeblock) => {
591+
const container = codeblock.parentNode.parentNode;
592+
593+
const copybutton = document.createElement('button');
594+
copybutton.classList.add('copy-code');
595+
copybutton.innerHTML = 'copy';
596+
597+
function copyingDone() {
598+
copybutton.innerHTML = 'copied!';
599+
setTimeout(() => {
600+
copybutton.innerHTML = 'copy';
601+
}, 2000);
602+
}
603+
604+
copybutton.addEventListener('click', (cb) => {
605+
if ('clipboard' in navigator) {
606+
navigator.clipboard.writeText(codeblock.textContent);
607+
copyingDone();
608+
return;
609+
}
610+
611+
const range = document.createRange();
612+
range.selectNodeContents(codeblock);
613+
const selection = window.getSelection();
614+
selection.removeAllRanges();
615+
selection.addRange(range);
616+
try {
617+
document.execCommand('copy');
618+
copyingDone();
619+
} catch (e) { };
620+
selection.removeRange(range);
621+
});
622+
623+
if (container.classList.contains("highlight")) {
624+
container.appendChild(copybutton);
625+
} else if (container.parentNode.firstChild == container) {
626+
627+
} else if (codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "TABLE") {
628+
629+
codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(copybutton);
630+
} else {
631+
632+
codeblock.parentNode.appendChild(copybutton);
633+
}
634+
});
635+
</script>
589636
</body>
590637

591638
</html>

0 commit comments

Comments
 (0)