Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/WingLim/hugo-tania.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWingLim <643089849@qq.com>2022-05-30 15:38:56 +0300
committerGitHub <noreply@github.com>2022-05-30 15:38:56 +0300
commit6c4173f56e24a9192398f94d6e71d3e3da6d52de (patch)
tree791ae5e15b3916473fcf855ba10fcebaf751db5b
parent92e052d4f12633ad0e3a7f55966a6c1d917989c8 (diff)
fix: copy button with or without lineNos (#87)HEADmain
-rw-r--r--assets/ts/copyButton.ts20
1 files changed, 7 insertions, 13 deletions
diff --git a/assets/ts/copyButton.ts b/assets/ts/copyButton.ts
index e05458b..96d7435 100644
--- a/assets/ts/copyButton.ts
+++ b/assets/ts/copyButton.ts
@@ -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;