-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCodeBlock.astro
171 lines (146 loc) · 4.02 KB
/
CodeBlock.astro
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
import hljs from "highlight.js";
import jsdom from "jsdom";
import Icon from "./Icon.astro";
import Tooltip from "./Tooltip.astro";
const LANGUAGE_REGEX = /language-([^\s]+)/;
const getLanguageFromCodeElement = (element: Element): string | undefined => {
const classes = element.classList.toString();
const result = LANGUAGE_REGEX.exec(classes);
if (!result) return undefined;
return result[1];
};
const getHighlightedLinesFromCodeElement = (
element: Element
): number[] | undefined => {
const attribute = element.getAttribute("metastring");
if (!attribute) return;
return JSON.parse(attribute);
};
// Rendering the slot gives us an HTML string, but it still contains the <pre>.
let code = await Astro.slots.render("default");
// Using jsdom, we can pass everything escaped properly in a <pre> element and
// then extract just the code as text.
const DOM = new jsdom.JSDOM(code);
const element = DOM.window.document.body.children[0];
const html = element.textContent;
if (!html) {
throw new Error("Failed to parse code!");
}
let language = Astro.props.language ?? getLanguageFromCodeElement(element);
if (language) {
code = hljs.highlight(html, { language }).value;
} else {
const result = hljs.highlightAuto(html);
code = result.value;
language = result.language;
}
const highlightLines =
Astro.props.highlightLines ?? getHighlightedLinesFromCodeElement(element);
const lines = code.split("\n");
if (highlightLines) {
highlightLines.forEach((line) => {
lines[line - 1] = `<span class="highlight">${lines[line - 1]}</span>`;
});
}
export interface Props {
language?: string;
highlightLines?: number[];
}
---
<div class="code-block">
<span class="language">{language}</span>
<button class="copy">
<Tooltip content="Copy to Clipboard">
<Icon name="copy" group="solid" />
<Icon name="check" group="solid" />
</Tooltip>
</button>
<pre><code class="hljs" class={`language-${language}`} set:html={lines.join("\n")} /></pre>
</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 {
font-size: 0.8em;
position: absolute;
top: 0px;
left: 0px;
padding: 0px 0.5em;
background-color: #222233;
border-radius: 0.3em 0px 0.3em 0px;
}
button.copy {
font-size: 1.2em;
background-color: #222233;
color: white;
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(i.fa-check) {
display: none;
}
&.success {
:global(i.fa-copy) {
display: none;
}
:global(i.fa-check) {
display: block;
}
}
}
&:hover {
button.copy {
opacity: 1;
}
}
pre {
max-height: inherit;
}
pre > code {
font-family: "Source Code Pro", "Consolas", monospace;
padding: 1.3em 0.5em 0.5em 0.5em;
tab-size: 4;
overflow: auto;
:global(span.highlight) {
display: inline-block;
width: 100%;
background-color: #3392ff45;
border-radius: 0.1em;
padding: 0px 0.1em;
margin: 0px -0.1em;
}
}
}
</style>