Skip to content

Commit 93d5d77

Browse files
committed
Codeblock: Add code type and copy button
Use render codeblock hook to add head for code type and copy button Add code type pulled from render codeblock hook Add code-copy-v2 which has click handler for new copy button
1 parent db1d92e commit 93d5d77

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

assets/css/v2/style.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,16 +988,61 @@ blockquote.side-callout {
988988

989989
/* Codeblocks */
990990
.highlight {
991+
grid-column: 1 / -1 !important;
991992
position: relative;
992993
z-index: -1;
993994

995+
/* mad attempt at a border */
996+
/* border: 1px solid #cccccc;
997+
padding: 1rem; */
998+
/* mad attempt at a border */
999+
9941000
code .cl {
9951001
position: relative;
9961002
width: 100%;
9971003
white-space: pre-wrap;
9981004
}
9991005
}
10001006

1007+
.code-header {
1008+
grid-column: 1 / -1 !important;
1009+
}
1010+
1011+
.code-type {
1012+
margin-left: 1rem;
1013+
text-transform: uppercase;
1014+
border: 1px solid #cccccc;
1015+
border-bottom: 1px solid white;
1016+
padding: .25rem 1rem; /* Padding for button content */
1017+
font-size: 12px; /* Font size */
1018+
z-index: 9999;
1019+
margin-bottom: -1px;
1020+
}
1021+
1022+
.code-copy-button {
1023+
background-color: #f2f2f2;
1024+
border: none;
1025+
padding: 5px 10px;
1026+
cursor: pointer;
1027+
font-family: "JetBrains Mono", monospace;
1028+
font-size: 12px;
1029+
color: #000;
1030+
}
1031+
1032+
.code-copy-button:hover {
1033+
background-color: #e0e0e0;
1034+
}
1035+
1036+
.code-copy-button:focus {
1037+
outline: none;
1038+
box-shadow: 0 0 2px 2px #a5a5a5;
1039+
}
1040+
1041+
.code-header {
1042+
display: flex;
1043+
justify-content: space-between;
1044+
}
1045+
10011046
.highlight-mf {
10021047
grid-column: 1 / -1 !important;
10031048
position: relative;
@@ -1174,6 +1219,7 @@ figure {
11741219
/* FILTHY HACKS END */
11751220

11761221
/* Hidden temporarily */
1222+
11771223
.code-copy {
11781224
display: none;
11791225
}

assets/js/code-copy-v2.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Tightly coupled to `render-codeblock.html` for element targeting
2+
const copyToClipBoard = ((clipboard) => async (button) => {
3+
const codeBlock = button.parentNode.nextElementSibling;
4+
5+
if (!codeBlock) {
6+
console.error('No code block found to copy from.');
7+
button.innerHTML = 'Error';
8+
return;
9+
}
10+
11+
const cleanCode = codeBlock.textContent
12+
.replace(/^\s*\d+\s/gm, '') // Remove line numbers
13+
.trim();
14+
15+
try {
16+
await clipboard.writeText(cleanCode);
17+
18+
updateButtonState(button, 'Copied!', 2000, true);
19+
} catch (error) {
20+
updateButtonState(button, 'Error');
21+
console.error(error);
22+
}
23+
})(navigator.clipboard);
24+
25+
const updateButtonState = (button, message, revertDelay, disable = false) => {
26+
button.innerHTML = message;
27+
28+
if (disable) {
29+
button.disabled = true;
30+
}
31+
32+
if (revertDelay) {
33+
setTimeout(() => {
34+
button.innerHTML = 'Copy';
35+
button.disabled = false;
36+
}, revertDelay);
37+
}
38+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="code-header">
2+
<span class="code-type">{{ .Type }}</span>
3+
<button onclick="copyToClipBoard(this)" class="code-copy-button" type="button">Copy</button>
4+
</div>
5+
6+
{{ $result := transform.HighlightCodeBlock . }}
7+
{{ $result.Wrapped }}

layouts/partials/scripts.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
{{ $codecopy := resources.Get "/js/code-copy.js" | fingerprint "sha512" }}
1414
<script src="{{ $codecopy.RelPermalink }}" type="text/javascript"></script>
1515

16+
<!-- load code copy v2 js -->
17+
{{ $codecopyv2 := resources.Get "/js/code-copy-v2.js" | fingerprint "sha512" }}
18+
<script src="{{ $codecopyv2.RelPermalink }}" type="text/javascript"></script>
19+
1620
<!-- load mermaid.js -->
1721
{{ if .Page.Store.Get "hasMermaid" }}
1822
{{ $mermaid := resources.Get "js/mermaid.min.js" | fingerprint "sha512" }}

0 commit comments

Comments
 (0)