Skip to content

Commit 4210108

Browse files
committed
add code block styles
1 parent 74580a2 commit 4210108

3 files changed

Lines changed: 79 additions & 18 deletions

File tree

assets/_markdown.scss

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,52 @@
8484
background: var(--gray-100);
8585
border-radius: $border-radius;
8686
overflow-x: auto;
87+
position: relative;
8788

8889
code {
8990
padding: 0;
9091
background: none;
9192
}
93+
94+
.code-controls {
95+
position: absolute;
96+
top: $padding-8;
97+
right: $padding-8;
98+
display: flex;
99+
align-items: center;
100+
gap: $padding-4;
101+
opacity: 0;
102+
transition: opacity 0.15s ease;
103+
}
104+
105+
&:hover .code-controls {
106+
opacity: 1;
107+
}
108+
109+
.code-lang {
110+
font-family: monospace;
111+
font-size: 0.7em;
112+
color: var(--gray-500);
113+
padding: 1px 6px;
114+
border-radius: $border-radius;
115+
background: var(--gray-200);
116+
line-height: 1.6;
117+
}
118+
119+
.code-copy-btn {
120+
background: none;
121+
border: none;
122+
cursor: pointer;
123+
color: var(--gray-500);
124+
padding: 2px;
125+
line-height: 0;
126+
border-radius: $border-radius;
127+
128+
&:hover {
129+
color: var(--body-font-color);
130+
background: var(--gray-200);
131+
}
132+
}
92133
}
93134

94135
p {

assets/clipboard.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
11
(function () {
2-
function select(element) {
3-
const selection = window.getSelection();
2+
var copyIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
3+
var checkIcon = '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
44

5-
const range = document.createRange();
6-
range.selectNodeContents(element);
5+
document.querySelectorAll("pre code").forEach(function (code) {
6+
var pre = code.parentElement;
77

8-
selection.removeAllRanges();
9-
selection.addRange(range);
10-
}
8+
var langClass = Array.from(code.classList).find(function (c) {
9+
return c.startsWith("language-");
10+
});
11+
var lang = langClass ? langClass.replace("language-", "") : null;
12+
13+
var controls = document.createElement("div");
14+
controls.className = "code-controls";
1115

12-
document.querySelectorAll("pre code").forEach(code => {
13-
code.addEventListener("click", function (event) {
14-
if (window.getSelection().toString()) {
15-
return;
16-
}
17-
select(code.parentElement);
16+
if (lang && lang !== "fallback") {
17+
var label = document.createElement("span");
18+
label.className = "code-lang";
19+
label.textContent = lang;
20+
controls.appendChild(label);
21+
}
1822

19-
if (navigator.clipboard) {
20-
navigator.clipboard.writeText(code.parentElement.textContent);
21-
}
23+
var btn = document.createElement("button");
24+
btn.className = "code-copy-btn";
25+
btn.setAttribute("aria-label", "Copy code");
26+
btn.innerHTML = copyIcon;
27+
28+
btn.addEventListener("click", function () {
29+
var text = code.textContent;
30+
navigator.clipboard.writeText(text).then(function () {
31+
btn.innerHTML = checkIcon;
32+
setTimeout(function () {
33+
btn.innerHTML = copyIcon;
34+
}, 2000);
35+
});
2236
});
37+
38+
controls.appendChild(btn);
39+
pre.appendChild(controls);
2340
});
2441
})();

config/_default/hugo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ publishDir = "public" # make compatible with github pages
2626
[markup.goldmark.extensions.passthrough]
2727
enable = true
2828
[markup.goldmark.extensions.passthrough.delimiters]
29-
block = [['\\[', '\\]'], ['$$', '$$'], ['\\begin{', '\\end{']]
29+
block = [['\[', '\]'], ['$$', '$$'], ['\\begin{', '\\end{']]
3030
inline = [['\\(', '\\)'], ['$', '$']]
3131

3232
[markup.tableOfContents]
33-
startLevel = 1
33+
startLevel = 1
34+
35+
[markup.highlight]
36+
style = "gruvbox"

0 commit comments

Comments
 (0)