-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCodeBlock.astro
More file actions
141 lines (120 loc) · 3.31 KB
/
CodeBlock.astro
File metadata and controls
141 lines (120 loc) · 3.31 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
import * as shiki from "shiki";
import { JSDOM } from "jsdom";
import { Code } from "astro:components";
import Tooltip from "../common/Tooltip.astro";
import Icon from "../common/Icon.astro";
import { faCheck, faCopy } from "@fortawesome/free-solid-svg-icons";
export interface Props {
lang?: shiki.BundledLanguage | shiki.PlainTextLanguage;
code?: string;
}
// ID of language as reported by Shiki
const language = Astro.props.lang ?? "plaintext";
let displayName: string | undefined;
if (language && !shiki.isPlainLang(language)) {
const langImport = await shiki.bundledLanguages[language]();
const langRegistration = langImport.default.find(
(l) => l.name === language || l.aliases?.includes(language),
);
displayName = langRegistration?.displayName;
}
let code = Astro.props.code ?? "";
// Code has been provided in a slot in a pre tag, we will need to extract the code from it
if (Astro.slots.has("default")) {
const dom = new JSDOM(await Astro.slots.render("default"));
code = dom.window.document.querySelector("pre")?.textContent ?? "";
}
---
<div class="code-block" data-language={language}>
{displayName && <span class="language">{displayName}</span>}
<button class="copy">
<Tooltip content="Copy to Clipboard">
<Icon icon={faCopy} />
<Icon icon={faCheck} />
</Tooltip>
</button>
<Code code={code} lang={language} />
</div>
<script>
const copyButtons = document.querySelectorAll("div.code-block button.copy");
for (const button of copyButtons) {
button.addEventListener("click", async () => {
const codeElement = button.parentElement?.querySelector("pre > code");
const code = codeElement?.textContent;
if (!code) {
throw new Error("Failed to copy code to clipboard!");
}
try {
await navigator.clipboard.writeText(code);
button.classList.add("success");
setTimeout(() => button.classList.remove("success"), 1000);
} catch (e) {
console.error(e);
}
});
}
</script>
<style lang="scss">
div.code-block {
position: relative;
margin: 0.5em auto;
text-align: left;
max-height: inherit;
border-radius: 0.5em;
overflow: hidden;
span.language,
button.copy {
background-color: #ffffff0e;
color: white;
}
span.language {
font-size: 0.8em;
position: absolute;
top: 0px;
left: 0px;
padding: 0px 0.5em;
border-radius: 0.3em 0px 0.3em 0px;
}
button.copy {
font-size: 1.2em;
border: none;
border-radius: 0.3em;
position: absolute;
top: 0.3em;
right: 0.3em;
width: 1.5em;
height: 1.5em;
text-align: center;
opacity: 0;
transition: opacity ease-in-out 0.3s;
:global(.icon.fa-check) {
display: none;
}
&.success {
:global(.icon.fa-copy) {
display: none;
}
:global(.icon.fa-check) {
display: block;
}
}
}
&:hover {
button.copy {
opacity: 1;
}
}
&[data-language="plaintext"] pre {
padding: 0.5em 0.5em;
}
:global(pre) {
max-height: inherit;
font-family: var(--font-source-code-pro);
tab-size: 4;
padding: 1.3em 0.5em 0.5em 0.5em;
overflow: auto;
background-color: #0d1117 !important;
}
}
</style>