Skip to content

Commit 2ef32a6

Browse files
committed
feat: copy-to-clipboard removes diff-highlight tokens, plus some css changes
1 parent b267c3d commit 2ef32a6

8 files changed

Lines changed: 695 additions & 11 deletions

File tree

assets/css/prism/prism-toolbar.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ div.code-toolbar > .toolbar > .toolbar-item > a,
2727
div.code-toolbar > .toolbar > .toolbar-item > button,
2828
div.code-toolbar > .toolbar > .toolbar-item > span {
2929
color: var(--main-text-color);
30-
font-size: .7em;
30+
font-size: .8em;
3131
padding: 0 .5em;
3232
background-color: var(--toolbar-background-color);
3333
/* box-shadow: 0 2px 0 0 rgba(0,0,0,0.2); */

assets/js.bundle/common/prism-copy-to-clipboard.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ document.querySelectorAll('.copy-to-clipboard-button').forEach(el => {
9898
for (let i = 0; i < children.length; i++) {
9999
if (children[i].tagName.toLowerCase() === 'pre') {
100100
return filterTextToCopy(children[i], children[i].textContent);
101+
} else {
102+
console.error("Error obtaining text, could not find PRE element.");
101103
}
102104
}
103105
return undefined;
@@ -122,11 +124,10 @@ document.querySelectorAll('.copy-to-clipboard-button').forEach(el => {
122124
const NEW_LINE_EXP = /\n(?!$)/g;
123125

124126
function filterTextToCopy(preElement, code) {
125-
// Filter out output lines.
126-
// This may arise from the command-line or line-number plugins.
127+
// Filter out output lines from the command-line or line-number plugins.
127128
const lineNumbersToSkip = preElement.getAttribute('data-output');
129+
const lines = code.split(NEW_LINE_EXP);
128130
if (lineNumbersToSkip !== null) {
129-
const lines = code.split(NEW_LINE_EXP);
130131
const linesNum = lines.length;
131132

132133
lineNumbersToSkip.split(',').forEach(function (section) {
@@ -150,13 +151,30 @@ function filterTextToCopy(preElement, code) {
150151
}
151152
});
152153

153-
code = lines.filter(s => s !== undefined).join('\n');
154+
code = lines.filter(s => s !== undefined);
154155
} else if (preElement.classList.contains('command-line')) {
155156
const nonOutputLines = preElement.querySelectorAll('.token.command');
156157
code = Array.prototype.slice.call(nonOutputLines)
157-
.map(e => e.textContent)
158-
.join('\n');
158+
.map(e => e.textContent);
159+
} else {
160+
code = lines;
161+
}
162+
163+
// Filter out diff lines from the diff-highlight plugin.
164+
let isDiffBlock = false;
165+
for (let i = 0; i < preElement.classList.length; i++)
166+
if (preElement.classList[i].startsWith('language-diff')) {
167+
isDiffBlock = true;
168+
break;
169+
}
170+
171+
if (isDiffBlock) {
172+
code = code
173+
.map(line => line.startsWith('-') ? undefined : line.slice(1))
174+
.filter(l => l !== undefined);
159175
}
160176

177+
code = code.join('\n');
178+
161179
return code;
162180
}

assets/scss/_common.scss

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ a {
4343
text-decoration: none !important;
4444

4545
background-image: linear-gradient(to right, currentColor, currentColor);
46-
background-size: 0 0.1em;
46+
background-size: .5rem 0.06em;
4747
background-position: 0 100%;
4848
background-repeat: no-repeat;
4949
transition: background-size $transition-duration linear;
50+
cursor: pointer;
5051
}
5152

5253
a:hover,
5354
a:focus,
5455
a:active {
55-
background-size: 100% 0.1em;
56+
background-size: 100% 0.06em;
5657
}
5758

5859
a:has(> i:only-child) {

assets/scss/_posts.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
text-align: center;
9292
line-height: $line-height-compact;
9393
margin-top: $space-half;
94+
width: 90%;
95+
margin-left: auto;
96+
margin-right: auto;
9497
}
9598

9699
b, strong {
@@ -215,6 +218,11 @@
215218
{
216219
content: none;
217220
}
221+
222+
a[id^="fnref"]
223+
{
224+
background-image: none;
225+
}
218226
}
219227

220228
div.center,

assets/scss/_variables.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ html[data-theme="dark"] {
8080
--highlight-color-rgb: 75, 75, 75;
8181
--border-color-rgb: 58, 57, 60;
8282

83-
--main-text-color-rgb: 140, 140, 140;
83+
--main-text-color-rgb: 155, 155, 155;
8484
--light-text-color-rgb: 118, 120, 120;
8585
}
8686

0 commit comments

Comments
 (0)