Skip to content

Commit d75dbe2

Browse files
authored
fix: copy code button does not work when line number is enabled (#487)
* fix: copy code button does not work when line number is enabled * fix pre style * Add gist shortcode to exampleSite
1 parent 88beecd commit d75dbe2

File tree

4 files changed

+66
-46
lines changed

4 files changed

+66
-46
lines changed

assets/scss/partials/layout/article.scss

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
}
124124

125125
.article-page.has-toc {
126-
127126
.left-sidebar {
128127
display: none;
129128
}
@@ -395,6 +394,41 @@
395394
}
396395
}
397396

397+
.highlight {
398+
background-color: var(--pre-background-color);
399+
padding: var(--card-padding);
400+
position: relative;
401+
402+
&:hover {
403+
.copyCodeButton {
404+
opacity: 1;
405+
}
406+
}
407+
408+
pre {
409+
margin: initial;
410+
padding: 0;
411+
margin: 0;
412+
width: auto;
413+
}
414+
}
415+
416+
.copyCodeButton {
417+
position: absolute;
418+
top: calc(var(--card-padding));
419+
right: calc(var(--card-padding));
420+
background: var(--card-background);
421+
border: none;
422+
box-shadow: var(--shadow-l2);
423+
border-radius: var(--tag-border-radius);
424+
padding: 8px 16px;
425+
color: var(--card-text-color-main);
426+
cursor: pointer;
427+
font-size: 14px;
428+
opacity: 0;
429+
transition: opacity 0.3s ease;
430+
}
431+
398432
.table-wrapper {
399433
padding: 0 var(--card-padding);
400434
overflow-x: auto;
@@ -449,6 +483,7 @@
449483
/// Negative margins
450484
blockquote,
451485
figure,
486+
.highlight,
452487
pre,
453488
.gallery,
454489
.video-wrapper,
@@ -458,30 +493,4 @@
458493
margin-right: calc((var(--card-padding)) * -1);
459494
width: calc(100% + var(--card-padding) * 2);
460495
}
461-
462-
.highlight {
463-
position: relative;
464-
465-
&:hover {
466-
.copyCodeButton {
467-
opacity: 1;
468-
}
469-
}
470-
}
471-
472-
.copyCodeButton {
473-
position: absolute;
474-
top: calc(var(--card-padding));
475-
right: 0;
476-
background: var(--card-background);
477-
border: none;
478-
box-shadow: var(--shadow-l2);
479-
border-radius: var(--tag-border-radius);
480-
padding: 8px 16px;
481-
color: var(--card-text-color-main);
482-
cursor: pointer;
483-
font-size: 14px;
484-
opacity: 0;
485-
transition: opacity 0.3s ease;
486-
}
487496
}

assets/ts/main.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,21 @@ let Stack = {
6262
/**
6363
* Add copy button to code block
6464
*/
65-
const codeBlocks = document.querySelectorAll('.article-content > div.highlight');
65+
const highlights = document.querySelectorAll('.article-content div.highlight');
6666
const copyText = `Copy`,
6767
copiedText = `Copied!`;
68-
codeBlocks.forEach(codeBlock => {
68+
69+
highlights.forEach(highlight => {
6970
const copyButton = document.createElement('button');
7071
copyButton.innerHTML = copyText;
7172
copyButton.classList.add('copyCodeButton');
72-
codeBlock.appendChild(copyButton);
73+
highlight.appendChild(copyButton);
7374

74-
const pre = codeBlock.getElementsByTagName('pre');
75-
const code = pre[0].textContent;
75+
const codeBlock = highlight.querySelector('code[data-lang]');
76+
if (!codeBlock) return;
7677

7778
copyButton.addEventListener('click', () => {
78-
navigator.clipboard.writeText(code)
79+
navigator.clipboard.writeText(codeBlock.textContent)
7980
.then(() => {
8081
copyButton.textContent = copiedText;
8182

exampleSite/config.yaml

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ params:
109109
defaultHomeserverUrl: "https://matrix.cactus.chat:8448"
110110
serverName: "cactus.chat"
111111
siteName: "" # You must insert a unique identifier here matching the one you registered (See https://cactus.chat/docs/getting-started/quick-start/#register-your-site)
112-
112+
113113
giscus:
114114
repo:
115115
repoID:
@@ -122,15 +122,15 @@ params:
122122
emitMetadata: 0
123123

124124
gitalk:
125-
owner:
126-
admin:
127-
repo:
128-
clientID:
129-
clientSecret:
130-
125+
owner:
126+
admin:
127+
repo:
128+
clientID:
129+
clientSecret:
130+
131131
cusdis:
132-
host:
133-
id:
132+
host:
133+
id:
134134
widgets:
135135
enabled:
136136
- search
@@ -183,19 +183,19 @@ menu:
183183
### For demonstration purpose, the home link will be open in a new tab
184184
newTab: true
185185
icon: home
186-
186+
187187
social:
188188
- identifier: github
189189
name: GitHub
190190
url: https://github.com/CaiJimmy/hugo-theme-stack
191191
params:
192-
icon: brand-github
193-
192+
icon: brand-github
193+
194194
- identifier: twitter
195195
name: Twitter
196196
url: https://twitter.com
197197
params:
198-
icon: brand-twitter
198+
icon: brand-twitter
199199

200200
related:
201201
includeNewer: true
@@ -219,3 +219,9 @@ markup:
219219
startLevel: 2
220220
highlight:
221221
noClasses: false
222+
codeFences: true
223+
guessSyntax: true
224+
lineNoStart: 1
225+
lineNos: true
226+
lineNumbersInTable: true
227+
tabWidth: 4

exampleSite/content/post/rich-content/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ Hugo ships with several [Built-in Shortcodes](https://gohugo.io/content-manageme
3636
## bilibilibi Shortcode
3737

3838
{{< bilibili av498363026 >}}
39+
40+
## Gist Shortcode
41+
42+
{{< gist spf13 7896402 >}}

0 commit comments

Comments
 (0)