-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
224 lines (178 loc) · 6.22 KB
/
Copy pathapp.js
File metadata and controls
224 lines (178 loc) · 6.22 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function setupCopyButtons() {
const buttons = document.querySelectorAll("[data-copy-target]");
for (const button of buttons) {
button.addEventListener("click", async () => {
const targetId = button.getAttribute("data-copy-target");
const target = targetId ? document.getElementById(targetId) : null;
if (!target) {
return;
}
const content = target.textContent ?? "";
try {
await navigator.clipboard.writeText(content.trim());
const original = button.textContent;
button.textContent = "Copied";
window.setTimeout(() => {
button.textContent = original;
}, 1600);
} catch {
button.textContent = "Copy failed";
}
});
}
}
function escapeHtml(value) {
return value
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">");
}
function applyPatternSegments(input, pattern, className) {
const segments = [];
let output = "";
let lastIndex = 0;
let match;
const regex = new RegExp(pattern.source, pattern.flags);
while ((match = regex.exec(input)) !== null) {
const [fullMatch] = match;
const startIndex = match.index;
const endIndex = startIndex + fullMatch.length;
output += input.slice(lastIndex, startIndex);
const token = `__TOKEN_${segments.length}__`;
segments.push(`<span class="${className}">${fullMatch}</span>`);
output += token;
lastIndex = endIndex;
if (fullMatch.length === 0) {
regex.lastIndex += 1;
}
}
output += input.slice(lastIndex);
return {
output,
segments,
};
}
function restorePatternSegments(input, segments) {
return segments.reduce(
(restored, segment, index) => restored.replace(`__TOKEN_${index}__`, segment),
input,
);
}
function highlightShellLine(line) {
if (/^\s*#/.test(line)) {
return `<span class="token-comment">${escapeHtml(line)}</span>`;
}
let html = escapeHtml(line);
const protectedSegments = [
{ pattern: /\$\{\{[^}]+\}\}/g, className: "token-variable" },
{ pattern: /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/g, className: "token-string" },
{ pattern: /\$\{[^}]+\}|\$[A-Za-z_][A-Za-z0-9_]*/g, className: "token-variable" },
];
const segmentGroups = [];
for (const entry of protectedSegments) {
const result = applyPatternSegments(html, entry.pattern, entry.className);
html = result.output;
segmentGroups.push(result.segments);
}
html = html
.replace(/(^|\s)(--[A-Za-z0-9-]+)/g, '$1<span class="token-flag">$2</span>')
.replace(
/(^|\s)(curl|bash|mkdir|printf|chmod|rsync|echo|if|then|fi|do|done|for|in)(?=\s|$)/g,
'$1<span class="token-keyword">$2</span>',
)
.replace(/(^|\s)([A-Z][A-Z0-9_]*)(=)/g, '$1<span class="token-key">$2</span><span class="token-punctuation">$3</span>');
for (let index = segmentGroups.length - 1; index >= 0; index -= 1) {
html = restorePatternSegments(html, segmentGroups[index]);
}
return html;
}
function highlightEnvLine(line) {
const escapedLine = escapeHtml(line);
const match = escapedLine.match(/^([A-Z][A-Z0-9_]*)(=)(.*)$/);
if (!match) {
return escapedLine;
}
const [, key, separator, value] = match;
const highlightedValue = value.replace(
/(\$\{[^}]+\}|\$[A-Za-z_][A-Za-z0-9_]*)/g,
'<span class="token-variable">$1</span>',
);
return `<span class="token-key">${key}</span><span class="token-punctuation">${separator}</span><span class="token-string">${highlightedValue}</span>`;
}
function highlightJsonLine(line) {
let html = escapeHtml(line);
html = html
.replace(
/^(\s*)"([^"]+)"(\s*:)/,
'$1<span class="token-key">"$2"</span><span class="token-punctuation">$3</span>',
)
.replace(/:\s*"([^"]*)"/g, ': <span class="token-string">"$1"</span>')
.replace(/\b(true|false|null)\b/g, '<span class="token-constant">$1</span>')
.replace(/\b-?\d+(?:\.\d+)?\b/g, '<span class="token-number">$&</span>');
return html;
}
function highlightYamlLine(line) {
if (/^\s*#/.test(line)) {
return `<span class="token-comment">${escapeHtml(line)}</span>`;
}
let html = escapeHtml(line);
html = html
.replace(/(\$\{\{[^}]+\}\})/g, '<span class="token-variable">$1</span>')
.replace(
/^(\s*-?\s*)([A-Za-z0-9_.-]+)(:\s*)/,
'$1<span class="token-key">$2</span><span class="token-punctuation">$3</span>',
)
.replace(/:\s*"([^"]*)"/g, ': <span class="token-string">"$1"</span>')
.replace(/:\s*'([^']*)'/g, ": <span class=\"token-string\">'$1'</span>")
.replace(/\b(true|false|null)\b/g, '<span class="token-constant">$1</span>')
.replace(/\b\d+\b/g, '<span class="token-number">$&</span>')
.replace(/\b(actions\/checkout@v\d+|actions\/setup-node@v\d+|Selflify\/[A-Za-z0-9-]+@v\d+)\b/g, '<span class="token-string">$1</span>');
return html;
}
function highlightCodeBlock(code) {
const language = code.getAttribute("data-lang");
if (!language) {
return;
}
const raw = code.textContent ?? "";
const lines = raw.split("\n");
const highlighter =
language === "json"
? highlightJsonLine
: language === "yaml"
? highlightYamlLine
: language === "env"
? highlightEnvLine
: highlightShellLine;
code.innerHTML = lines.map((line) => highlighter(line)).join("\n");
}
function setupSyntaxHighlighting() {
const blocks = document.querySelectorAll("pre code[data-lang]");
for (const block of blocks) {
if (block instanceof HTMLElement) {
highlightCodeBlock(block);
}
}
}
function setupExpandableCodeExamples() {
const containers = document.querySelectorAll("[data-expandable-code]");
for (const container of containers) {
container.addEventListener("click", (event) => {
const target = event.target;
if (!(target instanceof Element) || target.closest(".copy-button")) {
return;
}
const button = container.querySelector(".code-example-summary");
if (!(button instanceof HTMLButtonElement)) {
return;
}
const isOpen = container.classList.toggle("is-open");
button.setAttribute("aria-expanded", isOpen ? "true" : "false");
});
}
}
document.addEventListener("DOMContentLoaded", () => {
setupSyntaxHighlighting();
setupCopyButtons();
setupExpandableCodeExamples();
});