-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
md_theme.js
98 lines (91 loc) · 3.69 KB
/
md_theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
document.addEventListener("zero-md-rendered", function (event) {
const zeroMd = event.target;
const shadowRoot = zeroMd.shadowRoot;
if (shadowRoot) {
const preElements = shadowRoot.querySelectorAll("pre");
preElements.forEach((pre) => {
const button = document.createElement("button");
const clipboard =
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" id="clipboard" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-clipboard"><rect width="8" height="4" x="8" y="2" rx="1" ry="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/></svg>';
const tick =
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" style="display:none;" id="tick" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check"><path d="M20 6 9 17l-5-5"/></svg>';
button.innerHTML = clipboard + tick;
button.className = "copy-button";
button.addEventListener("click", function () {
const code = pre.querySelector("code");
const text = code.textContent;
if (navigator.clipboard && window.isSecureContext) {
// Use Clipboard API if available (HTTPS only)
navigator.clipboard
.writeText(text)
.then(() => {
showCopiedFeedback(button);
})
.catch((err) => {
console.error("Failed to copy text: ", err);
});
} else {
// Fallback for older browsers or HTTP
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand("copy");
showCopiedFeedback(button);
} catch (err) {
console.error("Failed to copy text: ", err);
}
document.body.removeChild(textArea);
}
});
pre.appendChild(button);
});
function showCopiedFeedback(button) {
button.querySelector("#tick").style.display = "block";
button.querySelector("#clipboard").style.display = "none";
setTimeout(() => {
button.querySelector("#tick").style.display = "none";
button.querySelector("#clipboard").style.display = "block";
}, 2000);
}
const style = document.createElement("style");
style.textContent = `
.copy-button {
appearance: none;
display: flex;
position: fixed;
top: 0.3rem;
right: 0.3rem;
padding:3px;
background-color: transparent;
border: 1px solid;
border-color: hsl(var(--border));
border-radius: 5px;
cursor: pointer;
}
.copy-button:hover {
background-color: hsl(var(--muted));
}
`;
shadowRoot.appendChild(style);
}
});
document.addEventListener("DOMContentLoaded", () => {
if (document.body) {
document.body.addEventListener("htmx:load", () => {
elt = document.body;
path = location.pathname.split("/")[2];
if (path) {
elt.querySelectorAll("[data-ref-navlink][data-state=active]").forEach((btn) => {
if (btn.dataset.link !== path) btn.dataset.state = "inactive";
});
elt
.querySelectorAll(`[data-link=${path}]`)
.forEach((link) => (link.dataset.state = "active"));
}
});
}
});