md2site-cli 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- md2site/__init__.py +7 -0
- md2site/__main__.py +10 -0
- md2site/assets.py +211 -0
- md2site/build.py +152 -0
- md2site/cli.py +354 -0
- md2site/config.py +444 -0
- md2site/diagnostics.py +167 -0
- md2site/frontmatter.py +245 -0
- md2site/hook.py +129 -0
- md2site/init_cmd.py +153 -0
- md2site/links.py +1403 -0
- md2site/markdown.py +408 -0
- md2site/model.py +80 -0
- md2site/nav.py +286 -0
- md2site/paths.py +130 -0
- md2site/render.py +306 -0
- md2site/scanner.py +273 -0
- md2site/search.py +96 -0
- md2site/server.py +267 -0
- md2site/skill/SKILL.md +50 -0
- md2site/slug.py +40 -0
- md2site/static/css/md2site.css +974 -0
- md2site/static/js/app.js +301 -0
- md2site/static/js/mermaid-init.js +93 -0
- md2site/static/js/search.js +577 -0
- md2site/static/js/theme-init.js +24 -0
- md2site/static/vendor/mermaid.min.js +4329 -0
- md2site/templates/base.html +82 -0
- md2site/templates/landing.html +30 -0
- md2site/templates/page.html +13 -0
- md2site/templates/partials/breadcrumbs.html +15 -0
- md2site/templates/partials/nav.html +37 -0
- md2site/templates/partials/prevnext.html +20 -0
- md2site/templates/partials/toc.html +14 -0
- md2site/titles.py +96 -0
- md2site/watcher.py +447 -0
- md2site/wikilinks.py +314 -0
- md2site_cli-0.2.0.dist-info/METADATA +280 -0
- md2site_cli-0.2.0.dist-info/RECORD +43 -0
- md2site_cli-0.2.0.dist-info/WHEEL +4 -0
- md2site_cli-0.2.0.dist-info/entry_points.txt +3 -0
- md2site_cli-0.2.0.dist-info/licenses/LICENSE +21 -0
- md2site_cli-0.2.0.dist-info/licenses/THIRD_PARTY_LICENSES.md +36 -0
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
function tokenize(text) {
|
|
5
|
+
if (!text) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
var matches = String(text).toLowerCase().match(/[\p{L}\p{N}_]+/gu);
|
|
9
|
+
return matches ? matches : [];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function extractWordsWithOffsets(text) {
|
|
13
|
+
var result = [];
|
|
14
|
+
if (!text) {
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
var re = /[\p{L}\p{N}_]+/gu;
|
|
18
|
+
var match;
|
|
19
|
+
while ((match = re.exec(text)) !== null) {
|
|
20
|
+
result.push({
|
|
21
|
+
word: match[0].toLowerCase(),
|
|
22
|
+
index: match.index,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function findPrefixMatch(words, token) {
|
|
29
|
+
var best = null;
|
|
30
|
+
for (var i = 0; i < words.length; i++) {
|
|
31
|
+
var w = words[i].word;
|
|
32
|
+
if (w.indexOf(token) === 0) {
|
|
33
|
+
var exact = w === token;
|
|
34
|
+
if (
|
|
35
|
+
best === null ||
|
|
36
|
+
(exact && !best.exact) ||
|
|
37
|
+
(exact === best.exact && i === 0 && best.wordIndex > 0)
|
|
38
|
+
) {
|
|
39
|
+
best = {
|
|
40
|
+
exact: exact,
|
|
41
|
+
wordIndex: i,
|
|
42
|
+
charIndex: words[i].index,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return best;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function searchEntries(entries, query) {
|
|
51
|
+
var tokens = tokenize(query);
|
|
52
|
+
if (!tokens.length || !entries || !entries.length) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
var results = [];
|
|
57
|
+
for (var i = 0; i < entries.length; i++) {
|
|
58
|
+
var entry = entries[i];
|
|
59
|
+
var titleWords = extractWordsWithOffsets(entry.t || "");
|
|
60
|
+
var bodyText = entry.b || "";
|
|
61
|
+
var bodyWords = extractWordsWithOffsets(bodyText);
|
|
62
|
+
var headings = entry.h || [];
|
|
63
|
+
var headingWordSets = [];
|
|
64
|
+
for (var hIdx = 0; hIdx < headings.length; hIdx++) {
|
|
65
|
+
headingWordSets.push({
|
|
66
|
+
text: headings[hIdx][0] || "",
|
|
67
|
+
id: headings[hIdx][1] || "",
|
|
68
|
+
words: extractWordsWithOffsets(headings[hIdx][0] || ""),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
var allMatched = true;
|
|
73
|
+
var score = 0;
|
|
74
|
+
var titleScore = 0;
|
|
75
|
+
var bestHeading = null;
|
|
76
|
+
var bestHeadingScore = 0;
|
|
77
|
+
var firstBodyChar = -1;
|
|
78
|
+
|
|
79
|
+
for (var tIdx = 0; tIdx < tokens.length; tIdx++) {
|
|
80
|
+
var tok = tokens[tIdx];
|
|
81
|
+
var matchedAnywhere = false;
|
|
82
|
+
|
|
83
|
+
var titleHit = findPrefixMatch(titleWords, tok);
|
|
84
|
+
if (titleHit) {
|
|
85
|
+
matchedAnywhere = true;
|
|
86
|
+
var tPts = 100 + (titleHit.wordIndex === 0 ? 25 : 10) + (titleHit.exact ? 15 : 0);
|
|
87
|
+
score += tPts;
|
|
88
|
+
titleScore += tPts;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
var tokBestHeading = null;
|
|
92
|
+
var tokBestHeadingPts = 0;
|
|
93
|
+
for (var j = 0; j < headingWordSets.length; j++) {
|
|
94
|
+
var hHit = findPrefixMatch(headingWordSets[j].words, tok);
|
|
95
|
+
if (hHit) {
|
|
96
|
+
matchedAnywhere = true;
|
|
97
|
+
var hPts = 30 + (hHit.wordIndex === 0 ? 10 : 5) + (hHit.exact ? 5 : 0);
|
|
98
|
+
if (hPts > tokBestHeadingPts) {
|
|
99
|
+
tokBestHeadingPts = hPts;
|
|
100
|
+
tokBestHeading = headingWordSets[j];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (tokBestHeading) {
|
|
105
|
+
score += tokBestHeadingPts;
|
|
106
|
+
if (tokBestHeadingPts > bestHeadingScore) {
|
|
107
|
+
bestHeadingScore = tokBestHeadingPts;
|
|
108
|
+
bestHeading = tokBestHeading;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
var bodyHit = findPrefixMatch(bodyWords, tok);
|
|
113
|
+
if (bodyHit) {
|
|
114
|
+
matchedAnywhere = true;
|
|
115
|
+
score += 5 + (bodyHit.exact ? 3 : 0);
|
|
116
|
+
if (firstBodyChar === -1 || bodyHit.charIndex < firstBodyChar) {
|
|
117
|
+
firstBodyChar = bodyHit.charIndex;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!matchedAnywhere) {
|
|
122
|
+
allMatched = false;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!allMatched) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
var url = entry.u || "";
|
|
132
|
+
score += 2 / (1 + url.length / 20);
|
|
133
|
+
var targetUrl = url;
|
|
134
|
+
var headingLabel = null;
|
|
135
|
+
if (bestHeading && bestHeading.id && titleScore === 0) {
|
|
136
|
+
targetUrl = url + "#" + bestHeading.id;
|
|
137
|
+
headingLabel = bestHeading.text;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
results.push({
|
|
141
|
+
entry: entry,
|
|
142
|
+
url: targetUrl,
|
|
143
|
+
headingLabel: headingLabel,
|
|
144
|
+
bodyOffset: firstBodyChar,
|
|
145
|
+
score: score,
|
|
146
|
+
tokens: tokens,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
results.sort(function (a, b) {
|
|
151
|
+
if (b.score !== a.score) {
|
|
152
|
+
return b.score - a.score;
|
|
153
|
+
}
|
|
154
|
+
return a.entry.u < b.entry.u ? -1 : a.entry.u > b.entry.u ? 1 : 0;
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
return results.slice(0, 10);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
161
|
+
module.exports = {
|
|
162
|
+
tokenize: tokenize,
|
|
163
|
+
searchEntries: searchEntries,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (typeof document === "undefined") {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getRootPrefix() {
|
|
172
|
+
var meta = document.querySelector('meta[name="md2site-root"]');
|
|
173
|
+
return (meta && meta.getAttribute("content")) || "./";
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function buildHighlightedFragment(text, tokens) {
|
|
177
|
+
var frag = document.createDocumentFragment();
|
|
178
|
+
if (!text) {
|
|
179
|
+
return frag;
|
|
180
|
+
}
|
|
181
|
+
var re = /[\p{L}\p{N}_]+/gu;
|
|
182
|
+
var lastIdx = 0;
|
|
183
|
+
var match;
|
|
184
|
+
while ((match = re.exec(text)) !== null) {
|
|
185
|
+
if (match.index > lastIdx) {
|
|
186
|
+
frag.appendChild(document.createTextNode(text.slice(lastIdx, match.index)));
|
|
187
|
+
}
|
|
188
|
+
var rawWord = match[0];
|
|
189
|
+
var lowerWord = rawWord.toLowerCase();
|
|
190
|
+
var matchedLen = 0;
|
|
191
|
+
for (var i = 0; i < tokens.length; i++) {
|
|
192
|
+
if (lowerWord.indexOf(tokens[i]) === 0 && tokens[i].length > matchedLen) {
|
|
193
|
+
matchedLen = tokens[i].length;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (matchedLen > 0) {
|
|
197
|
+
var mark = document.createElement("mark");
|
|
198
|
+
mark.textContent = rawWord.slice(0, matchedLen);
|
|
199
|
+
frag.appendChild(mark);
|
|
200
|
+
if (matchedLen < rawWord.length) {
|
|
201
|
+
frag.appendChild(document.createTextNode(rawWord.slice(matchedLen)));
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
frag.appendChild(document.createTextNode(rawWord));
|
|
205
|
+
}
|
|
206
|
+
lastIdx = re.lastIndex;
|
|
207
|
+
}
|
|
208
|
+
if (lastIdx < text.length) {
|
|
209
|
+
frag.appendChild(document.createTextNode(text.slice(lastIdx)));
|
|
210
|
+
}
|
|
211
|
+
return frag;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function extractSnippet(body, offset) {
|
|
215
|
+
if (!body) {
|
|
216
|
+
return "";
|
|
217
|
+
}
|
|
218
|
+
var maxLen = 120;
|
|
219
|
+
var start = offset > 40 ? offset - 40 : 0;
|
|
220
|
+
if (start > 0) {
|
|
221
|
+
var spaceIdx = body.indexOf(" ", start);
|
|
222
|
+
if (spaceIdx !== -1 && spaceIdx - start < 15) {
|
|
223
|
+
start = spaceIdx + 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
var end = start + maxLen;
|
|
227
|
+
var slice = body.slice(start, end);
|
|
228
|
+
if (start > 0) {
|
|
229
|
+
slice = "\u2026" + slice;
|
|
230
|
+
}
|
|
231
|
+
if (end < body.length) {
|
|
232
|
+
slice = slice + "\u2026";
|
|
233
|
+
}
|
|
234
|
+
return slice;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function initSearch() {
|
|
238
|
+
var openBtn = document.getElementById("search-open");
|
|
239
|
+
if (!openBtn || openBtn.getAttribute("data-search-enabled") !== "true") {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
openBtn.disabled = false;
|
|
244
|
+
openBtn.hidden = false;
|
|
245
|
+
|
|
246
|
+
var rootPrefix = getRootPrefix();
|
|
247
|
+
var backdrop = null;
|
|
248
|
+
var dialog = null;
|
|
249
|
+
var input = null;
|
|
250
|
+
var closeBtn = null;
|
|
251
|
+
var statusEl = null;
|
|
252
|
+
var listEl = null;
|
|
253
|
+
var activeIndex = -1;
|
|
254
|
+
var currentResults = [];
|
|
255
|
+
var indexState = "idle"; // idle | loading | ready | error
|
|
256
|
+
var lastFocused = null;
|
|
257
|
+
|
|
258
|
+
function ensureModal() {
|
|
259
|
+
if (backdrop) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
backdrop = document.createElement("div");
|
|
264
|
+
backdrop.className = "search-backdrop";
|
|
265
|
+
backdrop.hidden = true;
|
|
266
|
+
|
|
267
|
+
dialog = document.createElement("div");
|
|
268
|
+
dialog.className = "search-modal";
|
|
269
|
+
dialog.setAttribute("role", "dialog");
|
|
270
|
+
dialog.setAttribute("aria-modal", "true");
|
|
271
|
+
dialog.setAttribute("aria-labelledby", "search-modal-title");
|
|
272
|
+
|
|
273
|
+
var title = document.createElement("h2");
|
|
274
|
+
title.id = "search-modal-title";
|
|
275
|
+
title.className = "sr-only";
|
|
276
|
+
title.textContent = "Search documentation";
|
|
277
|
+
dialog.appendChild(title);
|
|
278
|
+
|
|
279
|
+
var bar = document.createElement("div");
|
|
280
|
+
bar.className = "search-input-row";
|
|
281
|
+
|
|
282
|
+
var label = document.createElement("label");
|
|
283
|
+
label.setAttribute("for", "search-input");
|
|
284
|
+
label.className = "sr-only";
|
|
285
|
+
label.textContent = "Search documentation";
|
|
286
|
+
bar.appendChild(label);
|
|
287
|
+
|
|
288
|
+
input = document.createElement("input");
|
|
289
|
+
input.id = "search-input";
|
|
290
|
+
input.type = "search";
|
|
291
|
+
input.className = "search-input";
|
|
292
|
+
input.placeholder = "Search documentation\u2026";
|
|
293
|
+
input.setAttribute("role", "combobox");
|
|
294
|
+
input.setAttribute("aria-expanded", "false");
|
|
295
|
+
input.setAttribute("aria-controls", "search-results");
|
|
296
|
+
input.setAttribute("aria-autocomplete", "list");
|
|
297
|
+
bar.appendChild(input);
|
|
298
|
+
|
|
299
|
+
closeBtn = document.createElement("button");
|
|
300
|
+
closeBtn.type = "button";
|
|
301
|
+
closeBtn.className = "search-close";
|
|
302
|
+
closeBtn.setAttribute("aria-label", "Close search");
|
|
303
|
+
closeBtn.textContent = "Esc";
|
|
304
|
+
bar.appendChild(closeBtn);
|
|
305
|
+
|
|
306
|
+
dialog.appendChild(bar);
|
|
307
|
+
|
|
308
|
+
statusEl = document.createElement("div");
|
|
309
|
+
statusEl.id = "search-status";
|
|
310
|
+
statusEl.className = "search-status";
|
|
311
|
+
statusEl.setAttribute("aria-live", "polite");
|
|
312
|
+
dialog.appendChild(statusEl);
|
|
313
|
+
|
|
314
|
+
listEl = document.createElement("ul");
|
|
315
|
+
listEl.id = "search-results";
|
|
316
|
+
listEl.className = "search-results";
|
|
317
|
+
listEl.setAttribute("role", "listbox");
|
|
318
|
+
dialog.appendChild(listEl);
|
|
319
|
+
|
|
320
|
+
backdrop.appendChild(dialog);
|
|
321
|
+
document.body.appendChild(backdrop);
|
|
322
|
+
|
|
323
|
+
backdrop.addEventListener("click", function (ev) {
|
|
324
|
+
if (ev.target === backdrop) {
|
|
325
|
+
closeModal();
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
closeBtn.addEventListener("click", closeModal);
|
|
330
|
+
|
|
331
|
+
input.addEventListener("input", function () {
|
|
332
|
+
renderResults();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
dialog.addEventListener("keydown", function (ev) {
|
|
336
|
+
if (ev.key === "Escape") {
|
|
337
|
+
ev.preventDefault();
|
|
338
|
+
closeModal();
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (ev.key === "ArrowDown") {
|
|
342
|
+
if (currentResults.length) {
|
|
343
|
+
ev.preventDefault();
|
|
344
|
+
setActive((activeIndex + 1) % currentResults.length);
|
|
345
|
+
}
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (ev.key === "ArrowUp") {
|
|
349
|
+
if (currentResults.length) {
|
|
350
|
+
ev.preventDefault();
|
|
351
|
+
setActive(
|
|
352
|
+
activeIndex <= 0 ? currentResults.length - 1 : activeIndex - 1
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (ev.key === "Enter" && activeIndex >= 0 && currentResults[activeIndex]) {
|
|
358
|
+
ev.preventDefault();
|
|
359
|
+
window.location.href = rootPrefix + currentResults[activeIndex].url;
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (ev.key === "Tab") {
|
|
363
|
+
var focusables = getFocusableElements();
|
|
364
|
+
if (!focusables.length) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
var first = focusables[0];
|
|
368
|
+
var last = focusables[focusables.length - 1];
|
|
369
|
+
if (ev.shiftKey && document.activeElement === first) {
|
|
370
|
+
ev.preventDefault();
|
|
371
|
+
last.focus();
|
|
372
|
+
} else if (!ev.shiftKey && document.activeElement === last) {
|
|
373
|
+
ev.preventDefault();
|
|
374
|
+
first.focus();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function getFocusableElements() {
|
|
381
|
+
if (!dialog) {
|
|
382
|
+
return [];
|
|
383
|
+
}
|
|
384
|
+
var nodes = dialog.querySelectorAll(
|
|
385
|
+
'input, button, a[href], [tabindex]:not([tabindex="-1"])'
|
|
386
|
+
);
|
|
387
|
+
var visible = [];
|
|
388
|
+
for (var i = 0; i < nodes.length; i++) {
|
|
389
|
+
if (!nodes[i].disabled && nodes[i].offsetParent !== null) {
|
|
390
|
+
visible.push(nodes[i]);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return visible;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function setActive(newIndex) {
|
|
397
|
+
activeIndex = newIndex;
|
|
398
|
+
var items = listEl.querySelectorAll('[role="option"]');
|
|
399
|
+
for (var i = 0; i < items.length; i++) {
|
|
400
|
+
var selected = i === activeIndex;
|
|
401
|
+
items[i].classList.toggle("is-selected", selected);
|
|
402
|
+
items[i].setAttribute("aria-selected", selected ? "true" : "false");
|
|
403
|
+
if (selected) {
|
|
404
|
+
input.setAttribute("aria-activedescendant", items[i].id);
|
|
405
|
+
if (typeof items[i].scrollIntoView === "function") {
|
|
406
|
+
items[i].scrollIntoView({ block: "nearest" });
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if (activeIndex < 0) {
|
|
411
|
+
input.removeAttribute("aria-activedescendant");
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function loadIndexIfNeeded() {
|
|
416
|
+
if (window.__MD2SITE_INDEX__) {
|
|
417
|
+
indexState = "ready";
|
|
418
|
+
renderResults();
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (indexState === "loading") {
|
|
422
|
+
statusEl.textContent = "Loading\u2026";
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
indexState = "loading";
|
|
426
|
+
statusEl.textContent = "Loading\u2026";
|
|
427
|
+
|
|
428
|
+
var script = document.createElement("script");
|
|
429
|
+
script.src = rootPrefix + "assets/search-index.js";
|
|
430
|
+
script.async = true;
|
|
431
|
+
script.onload = function () {
|
|
432
|
+
if (Array.isArray(window.__MD2SITE_INDEX__)) {
|
|
433
|
+
indexState = "ready";
|
|
434
|
+
renderResults();
|
|
435
|
+
} else {
|
|
436
|
+
indexState = "error";
|
|
437
|
+
statusEl.textContent = "Failed to load search index.";
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
script.onerror = function () {
|
|
441
|
+
indexState = "error";
|
|
442
|
+
statusEl.textContent = "Failed to load search index.";
|
|
443
|
+
if (script.parentNode) {
|
|
444
|
+
script.parentNode.removeChild(script);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
document.head.appendChild(script);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function renderResults() {
|
|
451
|
+
while (listEl.firstChild) {
|
|
452
|
+
listEl.removeChild(listEl.firstChild);
|
|
453
|
+
}
|
|
454
|
+
currentResults = [];
|
|
455
|
+
activeIndex = -1;
|
|
456
|
+
input.removeAttribute("aria-activedescendant");
|
|
457
|
+
|
|
458
|
+
if (indexState === "loading") {
|
|
459
|
+
statusEl.textContent = "Loading\u2026";
|
|
460
|
+
input.setAttribute("aria-expanded", "false");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
if (indexState === "error") {
|
|
464
|
+
statusEl.textContent = "Failed to load search index.";
|
|
465
|
+
input.setAttribute("aria-expanded", "false");
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
var query = input.value.trim();
|
|
470
|
+
if (!query) {
|
|
471
|
+
statusEl.textContent = "Type to search pages and headings.";
|
|
472
|
+
input.setAttribute("aria-expanded", "false");
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
var matches = searchEntries(window.__MD2SITE_INDEX__ || [], query);
|
|
477
|
+
currentResults = matches;
|
|
478
|
+
|
|
479
|
+
if (!matches.length) {
|
|
480
|
+
statusEl.textContent = "No matching pages found.";
|
|
481
|
+
input.setAttribute("aria-expanded", "false");
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
statusEl.textContent =
|
|
486
|
+
matches.length + (matches.length === 1 ? " result" : " results");
|
|
487
|
+
input.setAttribute("aria-expanded", "true");
|
|
488
|
+
|
|
489
|
+
for (var i = 0; i < matches.length; i++) {
|
|
490
|
+
var res = matches[i];
|
|
491
|
+
var li = document.createElement("li");
|
|
492
|
+
li.id = "search-opt-" + i;
|
|
493
|
+
li.className = "search-result-item";
|
|
494
|
+
li.setAttribute("role", "option");
|
|
495
|
+
li.setAttribute("aria-selected", "false");
|
|
496
|
+
|
|
497
|
+
var link = document.createElement("a");
|
|
498
|
+
link.className = "search-result-link";
|
|
499
|
+
link.href = rootPrefix + res.url;
|
|
500
|
+
|
|
501
|
+
var titleEl = document.createElement("div");
|
|
502
|
+
titleEl.className = "search-result-title";
|
|
503
|
+
titleEl.appendChild(
|
|
504
|
+
buildHighlightedFragment(res.entry.t || res.entry.u, res.tokens)
|
|
505
|
+
);
|
|
506
|
+
link.appendChild(titleEl);
|
|
507
|
+
|
|
508
|
+
var pathEl = document.createElement("div");
|
|
509
|
+
pathEl.className = "search-result-path";
|
|
510
|
+
var pathText = res.entry.u;
|
|
511
|
+
if (res.headingLabel) {
|
|
512
|
+
pathText += " \u203a " + res.headingLabel;
|
|
513
|
+
}
|
|
514
|
+
pathEl.textContent = pathText;
|
|
515
|
+
link.appendChild(pathEl);
|
|
516
|
+
|
|
517
|
+
if (res.entry.b) {
|
|
518
|
+
var snippet = extractSnippet(res.entry.b, res.bodyOffset);
|
|
519
|
+
if (snippet) {
|
|
520
|
+
var snipEl = document.createElement("div");
|
|
521
|
+
snipEl.className = "search-result-snippet";
|
|
522
|
+
snipEl.appendChild(buildHighlightedFragment(snippet, res.tokens));
|
|
523
|
+
link.appendChild(snipEl);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
li.appendChild(link);
|
|
528
|
+
listEl.appendChild(li);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
setActive(0);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function openModal() {
|
|
535
|
+
ensureModal();
|
|
536
|
+
lastFocused = document.activeElement;
|
|
537
|
+
backdrop.hidden = false;
|
|
538
|
+
document.body.classList.add("search-open");
|
|
539
|
+
if (indexState === "error") {
|
|
540
|
+
indexState = "idle";
|
|
541
|
+
}
|
|
542
|
+
loadIndexIfNeeded();
|
|
543
|
+
input.focus();
|
|
544
|
+
input.select();
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function closeModal() {
|
|
548
|
+
if (!backdrop || backdrop.hidden) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
backdrop.hidden = true;
|
|
552
|
+
document.body.classList.remove("search-open");
|
|
553
|
+
if (lastFocused && typeof lastFocused.focus === "function") {
|
|
554
|
+
lastFocused.focus();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
openBtn.addEventListener("click", openModal);
|
|
559
|
+
|
|
560
|
+
document.addEventListener("keydown", function (ev) {
|
|
561
|
+
if ((ev.ctrlKey || ev.metaKey) && ev.key.toLowerCase() === "k") {
|
|
562
|
+
ev.preventDefault();
|
|
563
|
+
if (backdrop && !backdrop.hidden) {
|
|
564
|
+
closeModal();
|
|
565
|
+
} else {
|
|
566
|
+
openModal();
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
if (document.readyState === "loading") {
|
|
573
|
+
document.addEventListener("DOMContentLoaded", initSearch);
|
|
574
|
+
} else {
|
|
575
|
+
initSearch();
|
|
576
|
+
}
|
|
577
|
+
})();
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
"use strict";
|
|
3
|
+
var STORAGE_KEY = "md2site-theme";
|
|
4
|
+
var fallback = window.__MD2SITE_DEFAULT_THEME__ || "system";
|
|
5
|
+
var mode = fallback;
|
|
6
|
+
try {
|
|
7
|
+
var stored = window.localStorage.getItem(STORAGE_KEY);
|
|
8
|
+
if (stored === "light" || stored === "dark" || stored === "system") {
|
|
9
|
+
mode = stored;
|
|
10
|
+
}
|
|
11
|
+
} catch (e) {}
|
|
12
|
+
|
|
13
|
+
var effective = mode;
|
|
14
|
+
if (mode === "system") {
|
|
15
|
+
var prefersDark =
|
|
16
|
+
window.matchMedia &&
|
|
17
|
+
window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
18
|
+
effective = prefersDark ? "dark" : "light";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
var root = document.documentElement;
|
|
22
|
+
root.dataset.theme = effective;
|
|
23
|
+
root.dataset.themeMode = mode;
|
|
24
|
+
})();
|