-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
302 lines (252 loc) · 8.56 KB
/
plugin.js
File metadata and controls
302 lines (252 loc) · 8.56 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var __init;
__init();
__init = function() {
function argsFromHash(hash) {
const index = hash.indexOf("?");
if (index >= 0) {
return new URLSearchParams(hash.slice(index + 1));
}
return new URLSearchParams();
}
let timer;
const list = [];
function scrolling(_, delay = 300) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
timer = undefined;
let last = list[0];
const root = document.querySelector(".content");
const view = root.getBoundingClientRect();
const mid = (view.top + view.bottom) / 2;
for (const el of list) {
const rect = el.getBoundingClientRect();
if (rect.top > view.top - 3) {
if (rect.top < mid) {
last = el;
}
break;
}
last = el;
}
const id = last.id;
const hash = location.hash.replace(/\?.*/, "");
history.replaceState(null, null, `${hash}?id=${id}`);
const current = document.querySelector(".sidebar .active");
if (current) {
current.classList.remove("active");
}
for (const el of document.querySelectorAll(".sidebar a")) {
if (el.href === location.href) {
el.parentElement.classList.add("active");
}
}
}, delay);
}
function scrollToPart() {
const args = argsFromHash(location.hash);
const id = args.get("id") || "_top";
const target = document.getElementById(id);
const scroller = document.querySelector(".content");
if (target) {
target.scrollIntoView({
block: "start",
behavior: "instant"
});
return;
}
scroller.scrollTo({
top: 0,
behavior: "instant"
});
}
addEventListener("hashchange", scrollToPart);
if (location.hash === "") {
history.replaceState(null, null, "#/home/README.md");
}
let cleanup;
function updateHash() {
if (cleanup) {
cleanup();
}
const root = document.querySelector(".content");
root.addEventListener("scroll", scrolling, { passive: true });
cleanup = function() {
list.length = 0;
root.removeEventListener("scroll", scrolling);
if (timer) {
clearTimeout(timer);
timer = undefined;
}
};
for (const el of document.querySelectorAll(".content h1, .content h2, .content h3")) {
list.push(el);
}
scrolling(null, 0);
}
function addCopyButtons() {
for (const pre of document.querySelectorAll(".content pre code")) {
let text = [];
let line = [];
let prefix = "";
const lines = [];
const walker = document.createTreeWalker(pre, NodeFilter.SHOW_ALL);
while (walker.nextNode()) {
const node = walker.currentNode;
if (node.parentNode === pre) {
line.push(node);
}
if (node.nodeType === Node.TEXT_NODE) {
if (prefix) {
node.data = prefix + node.data;
prefix = "";
}
const parts = node.data.split("\n");
if (parts.length > 1) {
prefix = parts.pop();
node.data = parts.shift();
text.push(node.data);
lines.push([text.join(""), line]);
for (const part of parts) {
lines.push([part, [part]]);
}
text = [];
line = [];
}
else {
text.push(node.data);
}
}
}
if (line.length > 0) {
lines.push([text.join(""), line]);
}
if (prefix) {
lines.push([prefix, [prefix]]);
}
const fullText = lines.map(function([text]) {
return text;
}).join("\n");
pre.innerHTML = "";
const fullCopy = document.createElement("button");
fullCopy.className = "copy-button full-copy-button";
fullCopy.dataset.label = "copy all";
fullCopy.addEventListener("click", function() {
navigator.clipboard.writeText(fullText);
const root = pre.parentElement;
root.classList.add("copying");
setTimeout(function() {
root.classList.remove("copying");
}, 100);
});
pre.parentElement.append(fullCopy);
let group;
for (const [text, line] of lines) {
let comment = false;
let single = false;
let space = false;
let end = false;
if (text[0] === "#") {
end = true;
single = true;
comment = true;
}
if (text.trim() === "") {
end = true;
single = true;
space = true;
}
if (!space && text[0].trim()) {
end = true;
}
if (group && end) {
pre.append(group);
group = undefined;
}
let first = false;
if (!group) {
first = true;
group = document.createElement("div");
group.className = "code-segment";
}
else {
group.append("\n");
}
if (comment) {
group.className = "code-comment";
}
else if (space) {
group.className = "code-space";
}
if (space) {
group.append("\n");
}
else {
for (const node of line) {
group.append(node);
}
}
if (single) {
pre.append(group);
group = undefined;
}
else if (first) {
const copy = document.createElement("button");
copy.className = "copy-button";
copy.dataset.label = "copy";
group.append(copy);
const which = group;
copy.addEventListener("click", function() {
const text = which.innerText;
navigator.clipboard.writeText(text);
which.classList.add("copying");
setTimeout(function() {
which.classList.remove("copying");
}, 100);
});
}
}
if (group) {
pre.append(group);
}
}
}
function fixLinks() {
const root = "" + new URL("./", location.href);
for (const a of document.querySelectorAll("a")) {
const url = new URL(a.href);
const args = argsFromHash(url.hash);
url.search = url.hash = "";
if (url.href.startsWith(root)) {
args.delete("id");
a.target = "";
if (args.size) {
a.href = "?" + args;
}
}
}
}
const config = window.$docsify || {};
const alias = config.alias || {};
const plugins = config.plugins || [];
window.$docsify = Object.assign(config, {
loadNavbar: true,
maxLevel: 3,
routerMode: "hash",
relativePath: true,
alias: {
".*/_navbar.md": "_navbar.md",
...alias,
},
plugins,
});
plugins.push(function(hook) {
hook.doneEach(scrollToPart);
hook.doneEach(updateHash);
hook.doneEach(addCopyButtons);
hook.doneEach(fixLinks);
});
};
__init();
__init = undefined;