thomwolf HF Staff commited on
Commit
adc659d
·
1 Parent(s): 903172a

Search inside article bodies from the top-left box; ⌘K focuses it (#2)

Browse files

- Search inside article bodies from the top-left box; ⌘K focuses it (f35e14bee7df1af6500a05966586292715923784)
- Search refinements: jump-to-section, ✕ clear badge, title + in-document highlighting (1feae6d14242388da3cd48e27540ed9ab9f80919)
- Search: list all in-document hits per topic, blue-rectangle highlights, body-only index (a15f9c48c14c1bbe851f39f2a5c044ce118ff123)

Files changed (3) hide show
  1. app.js +141 -81
  2. index.html +2 -15
  3. styles.css +38 -49
app.js CHANGED
@@ -26,10 +26,8 @@ const state = {
26
  collapsed: new Set(JSON.parse(localStorage.getItem('viz-collapsed') || '[]')),
27
  treeCollapsed: new Set(JSON.parse(localStorage.getItem('viz-tree-collapsed') || '[]')),
28
  srcNs: null, // selected source-namespace filter (null = all)
29
- palItems: [], // command-palette index
30
- palFiltered: [],
31
- palSel: 0,
32
  pageContent: new Map(), // path -> article markdown (cached)
 
33
  citedBy: new Map(), // sourceId -> Set(topic path) that cite it
34
  citeCount: new Map(), // sourceId -> # distinct articles citing it
35
  wordCount: new Map(), // topic path -> prose word count
@@ -297,12 +295,51 @@ function enhanceProse(root, currentPath) {
297
  acronymHovers(root); // known acronyms get a hover definition
298
  // remaining external links open in a new tab
299
  $$('a[href^="http"]', root).forEach(a => { a.target = '_blank'; a.rel = 'noopener'; });
 
300
  return toc;
301
  }
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  // Walk visible text nodes under root, skipping code/math/links/headings/abbr.
304
- function walkTextNodes(root, fn) {
305
- const SKIP = { CODE: 1, PRE: 1, A: 1, ABBR: 1, SCRIPT: 1, STYLE: 1, H1: 1, H2: 1, H3: 1, H4: 1, H5: 1, H6: 1 };
306
  const w = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
307
  acceptNode(n) {
308
  if (!n.nodeValue.trim()) return NodeFilter.FILTER_REJECT;
@@ -463,7 +500,7 @@ async function softRefresh() {
463
  ============================================================ */
464
  function layoutAside(on) { $('#layout').classList.toggle('has-aside', !!on); }
465
  function setView(html) { $('#view').innerHTML = html; }
466
- function setAside(html) { $('#aside').innerHTML = html || ''; }
467
  function showError(msg) {
468
  layoutAside(false);
469
  setView(`<div class="error-box">⚠ ${esc(msg)}<br><br><a class="btn" href="#/">← back to home</a></div>`);
@@ -957,8 +994,27 @@ function renderTopicsNav(list, q) {
957
  matches.forEach(n => {
958
  const it = el('div', 'nav-item');
959
  it.dataset.path = n.page.path;
960
- it.innerHTML = `${esc(n.page.title || n.slug)}`;
961
- it.addEventListener('click', () => go(`#/topic/${encodeURIComponent(n.page.path)}`));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
962
  body.append(it);
963
  });
964
  wrap.append(body);
@@ -1049,7 +1105,60 @@ function sortSources(items, mode) {
1049
  else arr.sort((a, b) => (state.citeCount.get(b.id) || 0) - (state.citeCount.get(a.id) || 0) || (a.title || a.id).localeCompare(b.title || b.id));
1050
  return arr;
1051
  }
1052
- const matchPage = (p, q) => (p.title || '').toLowerCase().includes(q) || p.path.toLowerCase().includes(q);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1053
  const matchSource = (s, q) => (s.title || '').toLowerCase().includes(q) || s.id.toLowerCase().includes(q);
1054
  function toggleCat(cat) {
1055
  if (state.collapsed.has(cat)) state.collapsed.delete(cat); else state.collapsed.add(cat);
@@ -1071,30 +1180,6 @@ function highlightActive() {
1071
  Filters an in-memory index, so it stays instant at thousands of items.
1072
  ============================================================ */
1073
  // `text` and `q` must already be lowercased (callers pre-lower for speed).
1074
- function fuzzyScore(q, text) {
1075
- const idx = text.indexOf(q);
1076
- if (idx === 0) return 1000; // prefix match — best
1077
- if (idx > 0) return 700 - Math.min(idx, 300);
1078
- // subsequence fallback — start at the first occurrence of q[0]; bail cheaply if absent.
1079
- let start = text.indexOf(q[0]);
1080
- if (start < 0) return -1;
1081
- let qi = 1, gaps = 0, last = start;
1082
- for (let ti = start + 1; ti < text.length && qi < q.length; ti++) {
1083
- if (text[ti] === q[qi]) { gaps += ti - last - 1; last = ti; qi++; }
1084
- }
1085
- return qi === q.length ? 300 - Math.min(gaps, 200) - Math.min(start, 50) : -1;
1086
- }
1087
- function paletteResults(raw) {
1088
- const q = raw.toLowerCase().trim();
1089
- if (!q) return state.palItems.slice(0, 40);
1090
- const scored = [];
1091
- for (const it of state.palItems) {
1092
- const s = Math.max(fuzzyScore(q, it._l), fuzzyScore(q, it._s) - 60);
1093
- if (s > -1) scored.push({ it, s });
1094
- }
1095
- scored.sort((a, b) => b.s - a.s || a.it.label.localeCompare(b.it.label));
1096
- return scored.slice(0, 40).map(x => x.it);
1097
- }
1098
  function highlightMatch(text, raw) {
1099
  const q = raw.trim();
1100
  if (!q) return esc(text);
@@ -1102,37 +1187,6 @@ function highlightMatch(text, raw) {
1102
  if (i < 0) return esc(text);
1103
  return esc(text.slice(0, i)) + '<b>' + esc(text.slice(i, i + q.length)) + '</b>' + esc(text.slice(i + q.length));
1104
  }
1105
- function renderPalette() {
1106
- const q = $('#palInput').value;
1107
- state.palFiltered = paletteResults(q);
1108
- if (state.palSel >= state.palFiltered.length) state.palSel = 0;
1109
- const box = $('#palResults');
1110
- if (!state.palFiltered.length) { box.innerHTML = `<div class="pal-empty">No matches for “${esc(q)}”.</div>`; return; }
1111
- box.innerHTML = state.palFiltered.map((it, i) => `
1112
- <div class="pal-item${i === state.palSel ? ' sel' : ''}" data-i="${i}">
1113
- <span class="pal-kind ${it.kind}">${it.kind}</span>
1114
- <span class="pal-text"><span class="pal-label">${highlightMatch(it.label, q)}</span><span class="pal-sub">${esc(it.sub)}</span></span>
1115
- </div>`).join('');
1116
- $$('#palResults .pal-item').forEach(n => {
1117
- n.addEventListener('mousemove', () => { if (state.palSel !== +n.dataset.i) { state.palSel = +n.dataset.i; markPalSel(); } });
1118
- n.addEventListener('click', () => choosePal(+n.dataset.i));
1119
- });
1120
- }
1121
- function markPalSel() { $$('#palResults .pal-item').forEach((n, i) => n.classList.toggle('sel', i === state.palSel)); }
1122
- function scrollPalSel() { $$('#palResults .pal-item')[state.palSel]?.scrollIntoView({ block: 'nearest' }); }
1123
- function choosePal(i) { const it = state.palFiltered[i]; if (it) { closePalette(); go(it.hash); } }
1124
- function openPalette() {
1125
- const sc = $('#palScrim'); sc.hidden = false;
1126
- const inp = $('#palInput'); inp.value = ''; state.palSel = 0;
1127
- renderPalette(); inp.focus();
1128
- }
1129
- function closePalette() { $('#palScrim').hidden = true; }
1130
- function palKeydown(e) {
1131
- if (e.key === 'ArrowDown') { e.preventDefault(); state.palSel = Math.min(state.palSel + 1, state.palFiltered.length - 1); markPalSel(); scrollPalSel(); }
1132
- else if (e.key === 'ArrowUp') { e.preventDefault(); state.palSel = Math.max(state.palSel - 1, 0); markPalSel(); scrollPalSel(); }
1133
- else if (e.key === 'Enter') { e.preventDefault(); choosePal(state.palSel); }
1134
- else if (e.key === 'Escape') { e.preventDefault(); closePalette(); }
1135
- }
1136
 
1137
  /* ── mobile nav ────────────────────────────────────────── */
1138
  function closeNav() { document.body.classList.remove('nav-open'); }
@@ -1181,7 +1235,6 @@ async function loadCore() {
1181
  }
1182
  try { state.taxonomy = taxT && window.jsyaml ? jsyaml.load(taxT) : null; } catch (e) { state.taxonomy = null; }
1183
  buildTaxonomy();
1184
- buildPaletteIndex();
1185
  }
1186
  const SOURCES_CACHE_KEY = 'viz-sources-v1';
1187
  function setSources(items) {
@@ -1192,7 +1245,7 @@ function setSources(items) {
1192
  if (items.some(s => s.title)) state.sourceTitlesLoaded = true; // tree stage has none yet
1193
  }
1194
  function refreshSourceViews() {
1195
- buildPaletteIndex(); updateCounts();
1196
  softRefresh(); // re-renders the current view (incl. the sources browser), upgrading titles/resolution
1197
  }
1198
  // Reverse the filename sanitization (`:`/`/` → `-`): `arxiv-1707.06347.md` → `arxiv:1707.06347`.
@@ -1273,13 +1326,6 @@ function buildTaxonomy() {
1273
  state.tax = cats;
1274
  }
1275
 
1276
- function buildPaletteIndex() {
1277
- const mk = (kind, label, sub, hash) => ({ kind, label, sub, hash, _l: label.toLowerCase(), _s: sub.toLowerCase() });
1278
- state.palItems = [
1279
- ...state.pages.map(p => mk('topic', p.title || p.path, catLabel(p._cat || p.parent || ''), `#/topic/${encodeURIComponent(p.path)}`)),
1280
- ...state.sources.map(s => mk('source', s.title || s.id, s.id, `#/source/${encodeURIComponent(s.id)}`)),
1281
- ];
1282
- }
1283
  const catLabel = (c) => prettyTitle((c || '').replace(/-/g, ' '));
1284
  // Sentence-cased category for headings, e.g. "reward-modeling" → "Reward modeling".
1285
  const catDisplay = (c) => { const s = catLabel(c); return s.charAt(0).toUpperCase() + s.slice(1); };
@@ -1327,6 +1373,8 @@ async function ensureCitemap() {
1327
  ids.forEach(id => { if (!state.citedBy.has(id)) state.citedBy.set(id, new Set()); state.citedBy.get(id).add(path); });
1328
  state.refCount.set(path, refs);
1329
  state.wordCount.set(path, countWords(content));
 
 
1330
  } catch (e) { /* skip unreadable article */ }
1331
  }
1332
  };
@@ -1379,18 +1427,30 @@ async function boot() {
1379
  $('#navScrim').addEventListener('click', closeNav);
1380
  $('#brandHome').addEventListener('click', () => go('#/'));
1381
  let st;
1382
- $('#navSearch').addEventListener('input', () => { clearTimeout(st); st = setTimeout(renderNav, 120); });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1383
  window.addEventListener('hashchange', route);
1384
 
1385
- // command palette
1386
- $('#paletteOpen').addEventListener('click', openPalette);
1387
- $('#palInput').addEventListener('input', renderPalette);
1388
- $('#palInput').addEventListener('keydown', palKeydown);
1389
- $('#palScrim').addEventListener('click', (e) => { if (e.target === $('#palScrim')) closePalette(); });
1390
  window.addEventListener('keydown', (e) => {
1391
- if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { e.preventDefault(); $('#palScrim').hidden ? openPalette() : closePalette(); return; }
 
1392
  const typing = /^(input|textarea)$/i.test(document.activeElement?.tagName || '');
1393
- if (e.key === '/' && !typing && $('#palScrim').hidden) { e.preventDefault(); openPalette(); }
1394
  });
1395
  initHovercards();
1396
 
 
26
  collapsed: new Set(JSON.parse(localStorage.getItem('viz-collapsed') || '[]')),
27
  treeCollapsed: new Set(JSON.parse(localStorage.getItem('viz-tree-collapsed') || '[]')),
28
  srcNs: null, // selected source-namespace filter (null = all)
 
 
 
29
  pageContent: new Map(), // path -> article markdown (cached)
30
+ searchText: new Map(), // topic path -> lowercased body, for nav content search
31
  citedBy: new Map(), // sourceId -> Set(topic path) that cite it
32
  citeCount: new Map(), // sourceId -> # distinct articles citing it
33
  wordCount: new Map(), // topic path -> prose word count
 
295
  acronymHovers(root); // known acronyms get a hover definition
296
  // remaining external links open in a new tab
297
  $$('a[href^="http"]', root).forEach(a => { a.target = '_blank'; a.rel = 'noopener'; });
298
+ highlightDocMatches(root, ($('#navSearch') || {}).value); // mark active search terms in the article
299
  return toc;
300
  }
301
 
302
+ // In-document search highlighting: <mark> occurrences of the active query in the
303
+ // rendered article. Cleared when the search is cleared (see clearNavSearch).
304
+ function highlightDocMatches(root, query) {
305
+ clearDocHighlights(root);
306
+ const q = (query || '').trim().toLowerCase();
307
+ if (!root || q.length < 2) return; // skip empty / single-char to avoid noise
308
+ walkTextNodes(root, (node) => {
309
+ const text = node.nodeValue, lc = text.toLowerCase();
310
+ let idx = lc.indexOf(q);
311
+ if (idx < 0) return;
312
+ const frag = document.createDocumentFragment();
313
+ let last = 0;
314
+ while (idx >= 0) {
315
+ if (idx > last) frag.append(text.slice(last, idx));
316
+ const mark = document.createElement('mark');
317
+ mark.className = 'search-hl';
318
+ mark.textContent = text.slice(idx, idx + q.length);
319
+ frag.append(mark);
320
+ last = idx + q.length;
321
+ idx = lc.indexOf(q, last);
322
+ }
323
+ if (last < text.length) frag.append(text.slice(last));
324
+ node.replaceWith(frag);
325
+ }, { SCRIPT: 1, STYLE: 1, PRE: 1 }); // search highlights headings/inline-code/links too; only fenced code, scripts & KaTeX (via classList) are skipped
326
+ }
327
+ function clearDocHighlights(root) {
328
+ root = root || $('#prose');
329
+ if (!root) return;
330
+ const marks = root.querySelectorAll('mark.search-hl');
331
+ marks.forEach(m => m.replaceWith(document.createTextNode(m.textContent)));
332
+ if (marks.length) root.normalize(); // merge split text nodes so re-highlighting stays clean
333
+ }
334
+ // (Re)highlight both the article body and the aside (open questions, toc) for query q.
335
+ function refreshDocHighlights(q) {
336
+ highlightDocMatches($('#prose'), q);
337
+ highlightDocMatches($('#aside'), q);
338
+ }
339
+
340
  // Walk visible text nodes under root, skipping code/math/links/headings/abbr.
341
+ function walkTextNodes(root, fn, skip) {
342
+ const SKIP = skip || { CODE: 1, PRE: 1, A: 1, ABBR: 1, SCRIPT: 1, STYLE: 1, H1: 1, H2: 1, H3: 1, H4: 1, H5: 1, H6: 1 };
343
  const w = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
344
  acceptNode(n) {
345
  if (!n.nodeValue.trim()) return NodeFilter.FILTER_REJECT;
 
500
  ============================================================ */
501
  function layoutAside(on) { $('#layout').classList.toggle('has-aside', !!on); }
502
  function setView(html) { $('#view').innerHTML = html; }
503
+ function setAside(html) { $('#aside').innerHTML = html || ''; highlightDocMatches($('#aside'), ($('#navSearch') || {}).value); }
504
  function showError(msg) {
505
  layoutAside(false);
506
  setView(`<div class="error-box">⚠ ${esc(msg)}<br><br><a class="btn" href="#/">← back to home</a></div>`);
 
994
  matches.forEach(n => {
995
  const it = el('div', 'nav-item');
996
  it.dataset.path = n.page.path;
997
+ const base = `#/topic/${encodeURIComponent(n.page.path)}`;
998
+ // All body occurrences of the query in this article (empty for a title-only match).
999
+ const { infos, total } = q && (state.searchText.get(n.page.path) || '').includes(q)
1000
+ ? bodyMatchInfos(n.page.path, q) : { infos: [], total: 0 };
1001
+ const titleTarget = base + (infos[0] && infos[0].section ? `~${infos[0].section}` : '');
1002
+
1003
+ const title = el('span', 'nav-item-title', highlightMatch(n.page.title || n.slug, q));
1004
+ if (total > 1) title.append(el('span', 'nav-hit-count', String(total)));
1005
+ it.append(title);
1006
+ it.addEventListener('click', () => go(titleTarget)); // clicking the row → first hit / top
1007
+
1008
+ if (infos.length) {
1009
+ const snips = el('div', 'nav-snippets');
1010
+ infos.forEach(h => {
1011
+ const s = el('div', 'nav-snippet', h.snippet);
1012
+ s.addEventListener('click', (e) => { e.stopPropagation(); go(base + (h.section ? `~${h.section}` : '')); });
1013
+ snips.append(s);
1014
+ });
1015
+ if (total > infos.length) snips.append(el('div', 'nav-snippet more', `+${total - infos.length} more match${total - infos.length === 1 ? '' : 'es'}`));
1016
+ it.append(snips);
1017
+ }
1018
  body.append(it);
1019
  });
1020
  wrap.append(body);
 
1105
  else arr.sort((a, b) => (state.citeCount.get(b.id) || 0) - (state.citeCount.get(a.id) || 0) || (a.title || a.id).localeCompare(b.title || b.id));
1106
  return arr;
1107
  }
1108
+ // A page matches on its title, its path, or anywhere in its (background-indexed)
1109
+ // body. `q` is already lowercased by callers.
1110
+ const matchPage = (p, q) =>
1111
+ (p.title || '').toLowerCase().includes(q) ||
1112
+ p.path.toLowerCase().includes(q) ||
1113
+ (state.searchText.get(p.path) || '').includes(q);
1114
+ // Every body occurrence of q in a page: a highlighted excerpt + its nearest heading,
1115
+ // so the sidebar can list all in-document hits and each can jump to its own section.
1116
+ // Returns { infos: [...], total } — total is the true count even when infos is capped.
1117
+ function bodyMatchInfos(path, q, cap = 20) {
1118
+ const raw = state.pageContent.get(path), lc = state.searchText.get(path);
1119
+ if (raw == null || lc == null) return { infos: [], total: 0 };
1120
+ const text = parseFrontmatter(raw).body; // same body the index was built from → indices align with lc and hits map to visible prose
1121
+ const infos = []; let total = 0, i = lc.indexOf(q);
1122
+ while (i >= 0) {
1123
+ total++;
1124
+ if (infos.length < cap) {
1125
+ const start = Math.max(0, i - 30);
1126
+ let clip = text.slice(start, i + q.length + 40)
1127
+ .replace(/\[source:[^\]]*\]/g, ' ')
1128
+ .replace(/[#>*_`~|]+/g, ' ')
1129
+ .replace(/\s+/g, ' ').trim();
1130
+ clip = (start > 0 ? '…' : '') + clip + '…';
1131
+ infos.push({ snippet: highlightMatch(clip, q), section: headingSlugBefore(text, i) });
1132
+ }
1133
+ i = lc.indexOf(q, i + q.length);
1134
+ }
1135
+ return { infos, total };
1136
+ }
1137
+ // Slug of the last ##/###/#### heading before `pos`, replicating enhanceProse's
1138
+ // slugify + de-dup (and skipping fenced code) so the id matches the rendered anchor.
1139
+ function headingSlugBefore(md, pos) {
1140
+ const seen = {};
1141
+ let found = '', inFence = false, offset = 0;
1142
+ for (const line of md.split('\n')) {
1143
+ if (/^\s*(```|~~~)/.test(line)) inFence = !inFence;
1144
+ else if (!inFence) {
1145
+ const h = /^(#{2,4})\s+(.+?)\s*#*$/.exec(line);
1146
+ if (h) {
1147
+ let slug = h[2].trim().toLowerCase().replace(/[^\w\s-]/g, '').trim().replace(/\s+/g, '-').slice(0, 60) || 'sec';
1148
+ if (seen[slug] != null) slug = `${slug}-${++seen[slug]}`; else seen[slug] = 0;
1149
+ if (offset <= pos) found = slug; // last heading at/before the match wins
1150
+ }
1151
+ }
1152
+ offset += line.length + 1; // +1 for the consumed '\n'
1153
+ }
1154
+ return found;
1155
+ }
1156
+ // Throttled nav re-render, used while background indexing fills in during a search.
1157
+ let _navRefreshT = null;
1158
+ function scheduleNavRefresh() {
1159
+ if (_navRefreshT) return;
1160
+ _navRefreshT = setTimeout(() => { _navRefreshT = null; if (($('#navSearch').value || '').trim()) renderNav(); }, 200);
1161
+ }
1162
  const matchSource = (s, q) => (s.title || '').toLowerCase().includes(q) || s.id.toLowerCase().includes(q);
1163
  function toggleCat(cat) {
1164
  if (state.collapsed.has(cat)) state.collapsed.delete(cat); else state.collapsed.add(cat);
 
1180
  Filters an in-memory index, so it stays instant at thousands of items.
1181
  ============================================================ */
1182
  // `text` and `q` must already be lowercased (callers pre-lower for speed).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1183
  function highlightMatch(text, raw) {
1184
  const q = raw.trim();
1185
  if (!q) return esc(text);
 
1187
  if (i < 0) return esc(text);
1188
  return esc(text.slice(0, i)) + '<b>' + esc(text.slice(i, i + q.length)) + '</b>' + esc(text.slice(i + q.length));
1189
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1190
 
1191
  /* ── mobile nav ────────────────────────────────────────── */
1192
  function closeNav() { document.body.classList.remove('nav-open'); }
 
1235
  }
1236
  try { state.taxonomy = taxT && window.jsyaml ? jsyaml.load(taxT) : null; } catch (e) { state.taxonomy = null; }
1237
  buildTaxonomy();
 
1238
  }
1239
  const SOURCES_CACHE_KEY = 'viz-sources-v1';
1240
  function setSources(items) {
 
1245
  if (items.some(s => s.title)) state.sourceTitlesLoaded = true; // tree stage has none yet
1246
  }
1247
  function refreshSourceViews() {
1248
+ updateCounts();
1249
  softRefresh(); // re-renders the current view (incl. the sources browser), upgrading titles/resolution
1250
  }
1251
  // Reverse the filename sanitization (`:`/`/` → `-`): `arxiv-1707.06347.md` → `arxiv:1707.06347`.
 
1326
  state.tax = cats;
1327
  }
1328
 
 
 
 
 
 
 
 
1329
  const catLabel = (c) => prettyTitle((c || '').replace(/-/g, ' '));
1330
  // Sentence-cased category for headings, e.g. "reward-modeling" → "Reward modeling".
1331
  const catDisplay = (c) => { const s = catLabel(c); return s.charAt(0).toUpperCase() + s.slice(1); };
 
1373
  ids.forEach(id => { if (!state.citedBy.has(id)) state.citedBy.set(id, new Set()); state.citedBy.get(id).add(path); });
1374
  state.refCount.set(path, refs);
1375
  state.wordCount.set(path, countWords(content));
1376
+ state.searchText.set(path, parseFrontmatter(content).body.toLowerCase()); // body only (frontmatter/open-questions excluded) so every hit maps to visible prose
1377
+ if (($('#navSearch').value || '').trim()) scheduleNavRefresh(); // live-fill an in-progress search
1378
  } catch (e) { /* skip unreadable article */ }
1379
  }
1380
  };
 
1427
  $('#navScrim').addEventListener('click', closeNav);
1428
  $('#brandHome').addEventListener('click', () => go('#/'));
1429
  let st;
1430
+ const searchBadge = $('#paletteOpen');
1431
+ const focusNavSearch = () => { const s = $('#navSearch'); s.focus(); s.select(); };
1432
+ const clearNavSearch = () => { const s = $('#navSearch'); s.value = ''; syncSearchBadge(); renderNav(); refreshDocHighlights(''); s.focus(); };
1433
+ // The badge is a ⌘K focus hint while empty, and a ✕ clear button once you've typed.
1434
+ const syncSearchBadge = () => {
1435
+ const has = !!$('#navSearch').value;
1436
+ searchBadge.textContent = has ? '✕' : '⌘K';
1437
+ searchBadge.classList.toggle('is-clear', has);
1438
+ searchBadge.title = has ? 'Clear search' : 'Focus search (⌘K)';
1439
+ };
1440
+ $('#navSearch').addEventListener('input', () => {
1441
+ ensureCitemap(); // kick off body indexing on first keystroke so content matches fill in fast
1442
+ syncSearchBadge();
1443
+ clearTimeout(st); st = setTimeout(() => { renderNav(); refreshDocHighlights($('#navSearch').value); }, 120);
1444
+ });
1445
  window.addEventListener('hashchange', route);
1446
 
1447
+ // ⌘K (and "/") focus the top-left search — one box that filters titles + bodies.
1448
+ searchBadge.addEventListener('click', () => { $('#navSearch').value ? clearNavSearch() : focusNavSearch(); });
 
 
 
1449
  window.addEventListener('keydown', (e) => {
1450
+ if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { e.preventDefault(); focusNavSearch(); return; }
1451
+ if (e.key === 'Escape' && document.activeElement === $('#navSearch') && $('#navSearch').value) { clearNavSearch(); return; }
1452
  const typing = /^(input|textarea)$/i.test(document.activeElement?.tagName || '');
1453
+ if (e.key === '/' && !typing) { e.preventDefault(); focusNavSearch(); }
1454
  });
1455
  initHovercards();
1456
 
index.html CHANGED
@@ -63,8 +63,8 @@
63
  <aside class="sidebar" id="sidebar">
64
  <div class="side-head">Topics</div>
65
  <div class="search-wrap">
66
- <input class="search" id="navSearch" type="text" placeholder="Search topics…" autocomplete="off">
67
- <kbd class="search-k" id="paletteOpen" title="Search everything — topics &amp; sources (⌘K)">⌘K</kbd>
68
  </div>
69
  <nav id="navList"></nav>
70
  </aside>
@@ -76,19 +76,6 @@
76
  <aside class="aside" id="aside"></aside>
77
  </div>
78
 
79
- <!-- Command palette: universal jump to any topic or source -->
80
- <div class="palette-scrim" id="palScrim" hidden>
81
- <div class="palette" role="dialog" aria-label="Search">
82
- <div class="palette-search">
83
- <span class="pal-icon">⌕</span>
84
- <input id="palInput" class="palette-input" type="text" placeholder="Jump to a topic or source…" autocomplete="off" spellcheck="false">
85
- <kbd class="pal-esc">esc</kbd>
86
- </div>
87
- <div id="palResults" class="palette-results"></div>
88
- <div class="palette-foot"><span><kbd>↑</kbd><kbd>↓</kbd> navigate</span><span><kbd>↵</kbd> open</span><span><span class="pal-dot pal-t"></span> topic <span class="pal-dot pal-s"></span> source</span></div>
89
- </div>
90
- </div>
91
-
92
  <script defer src="app.js"></script>
93
  </body>
94
  </html>
 
63
  <aside class="sidebar" id="sidebar">
64
  <div class="side-head">Topics</div>
65
  <div class="search-wrap">
66
+ <input class="search" id="navSearch" type="text" placeholder="Search topics &amp; text…" autocomplete="off">
67
+ <kbd class="search-k" id="paletteOpen" title="Focus search (⌘K)">⌘K</kbd>
68
  </div>
69
  <nav id="navList"></nav>
70
  </aside>
 
76
  <aside class="aside" id="aside"></aside>
77
  </div>
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  <script defer src="app.js"></script>
80
  </body>
81
  </html>
styles.css CHANGED
@@ -24,6 +24,8 @@
24
  --accent-deep: #0a275f;
25
  --accent-soft: #dde6f5;
26
  --accent-hover-row: #c8d6ee;
 
 
27
  --good: #1c7c3f;
28
  --warn: #9a6b00;
29
  --danger: #a12d2d;
@@ -53,6 +55,8 @@
53
  --accent-deep: #b6cdf7;
54
  --accent-soft: #20304d;
55
  --accent-hover-row: #28395a;
 
 
56
  --good: #6fcf8f;
57
  --warn: #e0b15a;
58
  --danger: #e08585;
@@ -159,7 +163,7 @@ a:hover { color: var(--accent-deep); }
159
  content: "⌕"; position: absolute; left: 9px; top: 50%; transform: translateY(-50%);
160
  color: var(--muted-3); font-size: 15px;
161
  }
162
- /* the one global-search affordance: click (or ⌘K) → command palette */
163
  .search-k {
164
  position: absolute; right: 7px; top: 50%; transform: translateY(-50%);
165
  font-family: var(--mono); font-size: 10px; padding: 2px 6px; border-radius: 4px;
@@ -167,6 +171,9 @@ a:hover { color: var(--accent-deep); }
167
  cursor: pointer; transition: all .12s;
168
  }
169
  .search-k:hover { border-color: var(--accent); color: var(--accent-deep); }
 
 
 
170
 
171
  .nav-group { margin-bottom: 14px; }
172
  .nav-group-title {
@@ -191,6 +198,25 @@ a:hover { color: var(--accent-deep); }
191
  display: block; font-family: var(--mono); font-size: 9.5px;
192
  color: var(--muted-4); margin-top: 1px; letter-spacing: 0.2px;
193
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  .nav-empty { color: var(--muted-4); font-size: 11.5px; padding: 8px; font-style: italic; }
195
  .mini-spin { display: inline-block; width: 10px; height: 10px; vertical-align: -1px; margin-right: 4px;
196
  border: 1.5px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@@ -315,11 +341,20 @@ a:hover { color: var(--accent-deep); }
315
  /* cross-reference: a `category/node` code span linked to its topic page */
316
  .prose a.xref { border-bottom: none; text-decoration: none; }
317
  .prose a.xref code {
318
- background: var(--accent-soft); border-color: var(--accent); color: var(--accent-deep);
319
  cursor: pointer; transition: background .12s, color .12s;
320
  }
321
  .prose a.xref:hover code { background: var(--accent); color: #fff; }
322
  [data-theme="dark"] .prose a.xref:hover code { color: #0b1220; }
 
 
 
 
 
 
 
 
 
323
 
324
  /* blockquote */
325
  .prose blockquote {
@@ -560,52 +595,6 @@ a:hover { color: var(--accent-deep); }
560
  }
561
  @media (min-width: 901px) { .nav-scrim { display: none !important; } }
562
 
563
- /* ── command palette ───────────────────────────────────── */
564
- .palette-foot kbd, .pal-esc {
565
- font-family: var(--mono); font-size: 10px; padding: 1.5px 5px; border-radius: 4px;
566
- border: 1px solid var(--border); background: var(--bg-soft); color: var(--muted-2); line-height: 1.4;
567
- }
568
-
569
- .palette-scrim {
570
- position: fixed; inset: 0; z-index: 200; background: rgba(15,18,22,0.42);
571
- backdrop-filter: blur(2px); display: flex; align-items: flex-start; justify-content: center;
572
- padding: 12vh 16px 16px;
573
- }
574
- .palette-scrim[hidden] { display: none; }
575
- .palette {
576
- width: 100%; max-width: 580px; background: var(--bg-card); border: 1px solid var(--border);
577
- border-radius: 12px; box-shadow: 0 12px 48px rgba(0,0,0,0.28); overflow: hidden;
578
- display: flex; flex-direction: column; max-height: 70vh;
579
- }
580
- .palette-search { display: flex; align-items: center; gap: 10px; padding: 12px 15px; border-bottom: 1px solid var(--border); }
581
- .pal-icon { color: var(--muted-3); font-size: 17px; }
582
- .palette-input {
583
- flex: 1; border: none; background: none; outline: none; color: var(--ink);
584
- font-family: var(--sans); font-size: 15px; font-weight: 300;
585
- }
586
- .palette-results { overflow-y: auto; padding: 6px; }
587
- .pal-item {
588
- display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 7px; cursor: pointer;
589
- }
590
- .pal-item.sel { background: var(--accent-soft); }
591
- .pal-kind {
592
- flex: 0 0 auto; font-family: var(--mono); font-size: 9px; font-weight: 600; text-transform: uppercase;
593
- letter-spacing: 0.5px; padding: 2px 6px; border-radius: 4px; width: 50px; text-align: center;
594
- }
595
- .pal-kind.topic { background: var(--accent-soft); color: var(--accent-deep); }
596
- .pal-kind.source { background: var(--bg-soft); color: var(--muted-2); border: 1px solid var(--border-soft); }
597
- .pal-text { flex: 1; min-width: 0; }
598
- .pal-label { font-size: 13.5px; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
599
- .pal-label b { color: var(--accent-deep); font-weight: 600; }
600
- .pal-sub { font-family: var(--mono); font-size: 10px; color: var(--muted-4); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
601
- .pal-empty { padding: 24px; text-align: center; color: var(--muted-3); font-size: 13px; }
602
- .palette-foot {
603
- display: flex; gap: 14px; align-items: center; padding: 8px 14px; border-top: 1px solid var(--border-soft);
604
- font-size: 10.5px; color: var(--muted-3); font-family: var(--mono); flex-wrap: wrap;
605
- }
606
- .pal-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; vertical-align: middle; margin: 0 2px 0 6px; }
607
- .pal-dot.pal-t { background: var(--accent); }
608
- .pal-dot.pal-s { background: var(--muted-4); }
609
 
610
  /* ── Collapsible taxonomy tree (topics sidebar) ────────── */
611
  .nav-cat { margin-bottom: 10px; }
@@ -779,7 +768,7 @@ body.book-mode .main { padding: 0; }
779
 
780
  /* ── Print (browser "Save as PDF") ─────────────────────── */
781
  @media print {
782
- .topbar, .sidebar, .aside, .nav-scrim, .book-toolbar, .palette-scrim, #hovercard, #toast, .foot, .menu-toggle { display: none !important; }
783
  html, body { background: #fff; color: #000; }
784
  .layout, .main, body.book-mode .main { display: block; padding: 0; margin: 0; }
785
  .book { max-width: none; margin: 0; padding: 0; color: #111; font-size: 10.5pt; }
 
24
  --accent-deep: #0a275f;
25
  --accent-soft: #dde6f5;
26
  --accent-hover-row: #c8d6ee;
27
+ --hl-bg: #0f3787; /* search-hit rectangle */
28
+ --hl-ink: #ffffff;
29
  --good: #1c7c3f;
30
  --warn: #9a6b00;
31
  --danger: #a12d2d;
 
55
  --accent-deep: #b6cdf7;
56
  --accent-soft: #20304d;
57
  --accent-hover-row: #28395a;
58
+ --hl-bg: #4f7fe0;
59
+ --hl-ink: #ffffff;
60
  --good: #6fcf8f;
61
  --warn: #e0b15a;
62
  --danger: #e08585;
 
163
  content: "⌕"; position: absolute; left: 9px; top: 50%; transform: translateY(-50%);
164
  color: var(--muted-3); font-size: 15px;
165
  }
166
+ /* the ⌘K badge on the search box: click (or ⌘K) → focus the search input */
167
  .search-k {
168
  position: absolute; right: 7px; top: 50%; transform: translateY(-50%);
169
  font-family: var(--mono); font-size: 10px; padding: 2px 6px; border-radius: 4px;
 
171
  cursor: pointer; transition: all .12s;
172
  }
173
  .search-k:hover { border-color: var(--accent); color: var(--accent-deep); }
174
+ /* once a query is entered, the badge becomes a ✕ clear button */
175
+ .search-k.is-clear { font-family: var(--sans); font-size: 12px; line-height: 1; padding: 3px 6.5px; }
176
+ .search-k.is-clear:hover { background: var(--accent-soft); }
177
 
178
  .nav-group { margin-bottom: 14px; }
179
  .nav-group-title {
 
198
  display: block; font-family: var(--mono); font-size: 9.5px;
199
  color: var(--muted-4); margin-top: 1px; letter-spacing: 0.2px;
200
  }
201
+ /* content-search: a matched-body excerpt under the topic name, query bolded */
202
+ .nav-item .nav-item-title { display: block; }
203
+ .nav-item .nav-item-title b { color: var(--accent-deep); font-weight: 600; }
204
+ .nav-item .nav-snippets { display: block; margin-top: 2px; }
205
+ .nav-item .nav-snippet {
206
+ display: block; margin-top: 2px; padding: 2px 6px; font-size: 11px; line-height: 1.4;
207
+ color: var(--muted-3); font-weight: 400; cursor: pointer;
208
+ border-left: 2px solid var(--border); border-radius: 0 4px 4px 0;
209
+ transition: background .12s, border-color .12s, color .12s;
210
+ }
211
+ .nav-item .nav-snippet:hover { background: var(--accent-soft); border-left-color: var(--accent); color: var(--ink); }
212
+ .nav-item .nav-snippet b { color: var(--accent-deep); font-weight: 600; }
213
+ .nav-item .nav-snippet.more { font-style: italic; cursor: default; border-left-color: transparent; color: var(--muted-4); }
214
+ .nav-item .nav-snippet.more:hover { background: none; }
215
+ .nav-hit-count {
216
+ display: inline-block; margin-left: 5px; padding: 0 5px; font-family: var(--mono);
217
+ font-size: 9px; line-height: 15px; vertical-align: 1px; border-radius: 8px;
218
+ background: var(--accent-soft); color: var(--accent-deep); font-weight: 600;
219
+ }
220
  .nav-empty { color: var(--muted-4); font-size: 11.5px; padding: 8px; font-style: italic; }
221
  .mini-spin { display: inline-block; width: 10px; height: 10px; vertical-align: -1px; margin-right: 4px;
222
  border: 1.5px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
 
341
  /* cross-reference: a `category/node` code span linked to its topic page */
342
  .prose a.xref { border-bottom: none; text-decoration: none; }
343
  .prose a.xref code {
344
+ background: var(--accent-soft); border-color: transparent; color: var(--accent-deep);
345
  cursor: pointer; transition: background .12s, color .12s;
346
  }
347
  .prose a.xref:hover code { background: var(--accent); color: #fff; }
348
  [data-theme="dark"] .prose a.xref:hover code { color: #0b1220; }
349
+ /* active-search term highlighting inside the article (cleared with the search) —
350
+ bold, slightly larger blue text (no pill, so it doesn't read like a link chip) */
351
+ mark.search-hl {
352
+ background: var(--hl-bg); color: var(--hl-ink);
353
+ font-weight: 600; border-radius: 3px;
354
+ padding: 0.5px 3.5px; margin: 0 0.5px;
355
+ box-shadow: 0 0 0 1px var(--hl-bg);
356
+ -webkit-box-decoration-break: clone; box-decoration-break: clone;
357
+ }
358
 
359
  /* blockquote */
360
  .prose blockquote {
 
595
  }
596
  @media (min-width: 901px) { .nav-scrim { display: none !important; } }
597
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
 
599
  /* ── Collapsible taxonomy tree (topics sidebar) ────────── */
600
  .nav-cat { margin-bottom: 10px; }
 
768
 
769
  /* ── Print (browser "Save as PDF") ─────────────────────── */
770
  @media print {
771
+ .topbar, .sidebar, .aside, .nav-scrim, .book-toolbar, #hovercard, #toast, .foot, .menu-toggle { display: none !important; }
772
  html, body { background: #fff; color: #000; }
773
  .layout, .main, body.book-mode .main { display: block; padding: 0; margin: 0; }
774
  .book { max-width: none; margin: 0; padding: 0; color: #111; font-size: 10.5pt; }