app.js
12.1 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* ============================================================
LemonBoss doc-site shell — menu + hash router + search
============================================================ */
(async () => {
// ── Menu (loaded synchronously via separate script tag if available) ──
await loadScript('./assets/menu.js');
const MENU = window.LEMONBOSS_MENU || [];
const menuEl = document.getElementById('menu');
const readerEl = document.getElementById('reader');
const searchEl = document.getElementById('search');
const resultsEl = document.getElementById('results');
const summaryEl = document.getElementById('search-summary');
const tabBtns = document.querySelectorAll('.tab');
const tabSearch = document.getElementById('tab-search');
const countEl = document.getElementById('search-count');
const burger = document.getElementById('burger');
const themeBtn = document.getElementById('theme');
// ──────────────────────────────────────────────
// THEME
// ──────────────────────────────────────────────
const storedTheme = localStorage.getItem('lemonboss-theme');
if (storedTheme) document.documentElement.dataset.theme = storedTheme;
themeBtn.addEventListener('click', () => {
const next = document.documentElement.dataset.theme === 'dark' ? 'light' : 'dark';
document.documentElement.dataset.theme = next;
localStorage.setItem('lemonboss-theme', next);
});
// ──────────────────────────────────────────────
// MENU RENDER
// ──────────────────────────────────────────────
function renderMenu() {
MENU.forEach(ch => menuEl.appendChild(renderNode(ch, 'chapter')));
}
function renderNode(node, kind) {
const wrap = document.createElement('div');
wrap.className = `menu-${kind}`;
wrap.dataset.id = node.id;
const link = document.createElement('a');
link.className = 'menu-link';
link.href = '#/' + node.href.replace(/#.*$/, '') + (node.href.includes('#') ? node.href.slice(node.href.indexOf('#')) : '');
link.dataset.href = node.href;
if (node.children) {
const caret = document.createElement('span');
caret.className = 'caret';
caret.textContent = '▾';
caret.addEventListener('click', (e) => {
e.preventDefault(); e.stopPropagation();
wrap.classList.toggle('collapsed');
});
link.appendChild(caret);
} else {
const dot = document.createElement('span');
dot.className = 'caret';
dot.textContent = '·';
link.appendChild(dot);
}
const num = document.createElement('span');
num.className = 'menu-num';
num.textContent = node.num;
link.appendChild(num);
const txt = document.createElement('span');
txt.textContent = node.title;
link.appendChild(txt);
link.addEventListener('click', (e) => {
e.preventDefault();
navigateTo(node.href);
if (window.innerWidth < 768) document.body.classList.remove('sidebar-open');
});
wrap.appendChild(link);
if (node.children) {
const cs = document.createElement('div');
cs.className = 'children';
const childKind = kind === 'chapter' ? 'section' : 'sub';
node.children.forEach(c => cs.appendChild(renderNode(c, childKind)));
wrap.appendChild(cs);
}
return wrap;
}
function highlightMenu(href) {
document.querySelectorAll('.menu-link').forEach(a => a.classList.remove('active'));
const target = stripHash(href);
let best = null;
document.querySelectorAll('.menu-link').forEach(a => {
const ah = a.dataset.href || '';
if (stripHash(ah) === target) {
if (!best || (ah.includes('#') && !best.dataset.href.includes('#'))) best = a;
if (ah === href) best = a; // exact match wins
}
});
if (best) {
best.classList.add('active');
// expand all ancestors
let p = best.parentElement;
while (p && p !== menuEl) {
if (p.classList.contains('collapsed')) p.classList.remove('collapsed');
p = p.parentElement;
}
best.scrollIntoView({ block: 'nearest' });
}
}
const stripHash = (h) => h.split('#')[0];
// ──────────────────────────────────────────────
// HASH ROUTER
// ──────────────────────────────────────────────
function navigateTo(href) {
location.hash = '#/' + href;
}
function onHashChange() {
const raw = location.hash.replace(/^#\//, '');
const href = raw || '01-overview/index.html';
readerEl.src = './chapters/' + href;
highlightMenu(href);
// switch back to reader pane
switchTab('reader');
}
window.addEventListener('hashchange', onHashChange);
// ──────────────────────────────────────────────
// TABS
// ──────────────────────────────────────────────
function switchTab(name) {
tabBtns.forEach(b => b.classList.toggle('active', b.dataset.pane === name));
document.querySelectorAll('.pane').forEach(p =>
p.classList.toggle('active', p.classList.contains('pane-' + name))
);
}
tabBtns.forEach(b => b.addEventListener('click', () => switchTab(b.dataset.pane)));
// ──────────────────────────────────────────────
// SEARCH
// ─ Loads search-index.json (built by scripts/build-search-index.js)
// ─ Tokenization: lowercase, split on whitespace+punct, + Chinese bigram
// ──────────────────────────────────────────────
let INDEX = null;
async function loadIndex() {
if (INDEX) return INDEX;
try {
const r = await fetch('./assets/search-index.json');
INDEX = await r.json();
} catch (e) {
INDEX = { docs: [], invIndex: {} };
console.warn('[search] no index available — run scripts/build-search-index.js');
}
return INDEX;
}
function tokenize(text) {
const tokens = [];
const lower = text.toLowerCase();
// ASCII words / digits
(lower.match(/[a-z0-9_]+/g) || []).forEach(w => tokens.push(w));
// CJK bigrams (一-鿿 plus extensions)
const cjk = lower.match(/[㐀-鿿]+/g) || [];
cjk.forEach(run => {
if (run.length === 1) tokens.push(run);
else {
for (let i = 0; i < run.length - 1; i++) tokens.push(run.slice(i, i + 2));
}
});
return tokens;
}
async function runSearch(query) {
if (!query || query.trim().length === 0) {
resultsEl.innerHTML = '';
summaryEl.textContent = '输入关键词开始搜索';
countEl.textContent = '0';
return;
}
const idx = await loadIndex();
const terms = tokenize(query);
if (terms.length === 0) {
resultsEl.innerHTML = '';
summaryEl.textContent = `「${query}」无可识别 token`;
countEl.textContent = '0';
return;
}
// score docs by term frequency sum
const scores = new Map();
let hitDocs = null;
for (const t of terms) {
const posting = idx.invIndex[t];
if (!posting) continue;
const docs = new Set();
for (const [docId, tf] of posting) {
scores.set(docId, (scores.get(docId) || 0) + tf);
docs.add(docId);
}
hitDocs = hitDocs ? new Set([...hitDocs].filter(d => docs.has(d))) : docs;
}
// fallback: if intersection empty, use union
const candidates = (hitDocs && hitDocs.size > 0) ? [...hitDocs] : [...scores.keys()];
candidates.sort((a, b) => scores.get(b) - scores.get(a));
const top = candidates.slice(0, 50);
renderResults(top, query, idx);
}
function renderResults(docIds, query, idx) {
resultsEl.innerHTML = '';
const docs = idx.docs;
if (docIds.length === 0) {
summaryEl.textContent = `「${query}」没有命中`;
countEl.textContent = '0';
return;
}
summaryEl.textContent = `「${query}」命中 ${docIds.length} 条`;
countEl.textContent = String(docIds.length);
docIds.forEach(id => {
const d = docs[id];
if (!d) return;
const li = document.createElement('li');
li.className = 'result-item';
li.innerHTML = `
<div class="result-path">${d.crumbs.join(' › ')}</div>
<div class="result-title">${escapeHtml(d.title)}</div>
<div class="result-snippet">${snippet(d.text, query)}</div>
`;
li.addEventListener('click', () => {
navigateTo(d.href);
});
resultsEl.appendChild(li);
});
}
function escapeHtml(s) {
return String(s).replace(/[&<>"']/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"', "'":''' }[c]));
}
function snippet(text, query) {
const lower = text.toLowerCase();
const q = query.toLowerCase();
let i = lower.indexOf(q);
if (i < 0) {
const t = tokenize(query)[0];
if (t) i = lower.indexOf(t);
}
if (i < 0) i = 0;
const start = Math.max(0, i - 60);
const end = Math.min(text.length, i + 140);
let frag = text.slice(start, end);
if (start > 0) frag = '…' + frag;
if (end < text.length) frag = frag + '…';
// highlight
const re = new RegExp(query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
return escapeHtml(frag).replace(re, m => `<mark>${escapeHtml(m)}</mark>`);
}
let timer = null;
searchEl.addEventListener('input', () => {
clearTimeout(timer);
const v = searchEl.value;
timer = setTimeout(async () => {
await runSearch(v);
if (v.trim()) switchTab('search');
}, 120);
});
searchEl.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
runSearch(searchEl.value);
switchTab('search');
} else if (e.key === 'Escape') {
searchEl.value = '';
runSearch('');
switchTab('reader');
}
});
document.getElementById('clear-search').addEventListener('click', () => {
searchEl.value = '';
runSearch('');
switchTab('reader');
searchEl.focus();
});
// ──────────────────────────────────────────────
// GLOBAL HOTKEYS
// ──────────────────────────────────────────────
document.addEventListener('keydown', (e) => {
const tag = (e.target.tagName || '').toLowerCase();
if (tag === 'input' || tag === 'textarea') return;
if (e.key === '/') {
e.preventDefault();
searchEl.focus();
searchEl.select();
}
});
// ──────────────────────────────────────────────
// MOBILE
// ──────────────────────────────────────────────
burger.addEventListener('click', () => document.body.classList.toggle('sidebar-open'));
// ──────────────────────────────────────────────
// BOOT
// ──────────────────────────────────────────────
renderMenu();
if (!location.hash) location.hash = '#/01-overview/index.html';
else onHashChange();
onHashChange();
// helper: load script
function loadScript(src) {
return new Promise((res, rej) => {
const s = document.createElement('script');
s.src = src;
s.onload = () => res();
s.onerror = () => rej(new Error('Failed to load ' + src));
document.head.appendChild(s);
});
}
})();