From 37e75bd84fce8ae65ed2ccda1dcd8fab353e8254 Mon Sep 17 00:00:00 2001 From: Accusys Date: Fri, 22 May 2026 10:15:58 +0800 Subject: [PATCH] fix: search result click scrolls to first match + highlight; left sidebar unchanged --- docs_v1.0/doc_wasm/index.html | 41 +++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/docs_v1.0/doc_wasm/index.html b/docs_v1.0/doc_wasm/index.html index ff380d5..5542f04 100644 --- a/docs_v1.0/doc_wasm/index.html +++ b/docs_v1.0/doc_wasm/index.html @@ -125,7 +125,7 @@ function md2html(md) { return h; } -async function loadDoc(name) { +async function loadDoc(name, highlightQ) { el.innerHTML = '

Loading...

'; try { const resp = await fetch('/doc-wasm/modules/' + name + '.md'); @@ -139,6 +139,35 @@ async function loadDoc(name) { var link = document.querySelector('.sidebar a[data-module="' + name + '"]'); if (link) link.classList.add('active'); history.pushState(null, '', '#' + name); + + // Scroll to first highlighted match if highlightQ provided + if (highlightQ) { + var re = new RegExp(highlightQ.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'); + var walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT, null, false); + var node, match; + while (node = walker.nextNode()) { + if (re.test(node.textContent)) { + match = node; + break; + } + } + if (match) { + re.lastIndex = 0; + var idx = match.textContent.search(re); + if (idx >= 0) { + var range = document.createRange(); + range.setStart(match, idx); + range.setEnd(match, idx + highlightQ.length); + var mark = document.createElement('mark'); + mark.style.background = '#ffeb3b'; + mark.style.color = '#000'; + mark.style.padding = '0 2px'; + mark.style.borderRadius = '2px'; + range.surroundContents(mark); + mark.scrollIntoView({ behavior: 'smooth', block: 'center' }); + } + } + } } catch(e) { el.innerHTML = '

Error: ' + e.message + '

'+e.stack+'
'; } @@ -180,7 +209,8 @@ async function fulltextSearch(q) { } var html = '

' + results.length + ' result(s) for ' + escapeHtml(q) + '

'; for (var r of results) { - html += '
' + var safeQ = q.replace(/'/g, "\\'"); + html += '
' + '' + escapeHtml(r.module) + ' ' + escapeHtml(r.title) + '' + ' line ' + r.line + '
' + '' @@ -190,11 +220,8 @@ async function fulltextSearch(q) { el.innerHTML = '

' + results.length + ' result(s) — click a result in the sidebar to open

'; } -function showSearchResult(module) { - document.getElementById('search').value = ''; - document.getElementById('search-results').style.display = 'none'; - document.getElementById('module-list').style.display = ''; - loadDoc(module); +function showSearchResult(module, q) { + loadDoc(module, q); } async function loginUser(user, pass) {