feat: highlight matched keywords in search results

This commit is contained in:
Accusys
2026-05-19 03:21:22 +08:00
parent 7bc069b806
commit 6da046e831

View File

@@ -146,6 +146,12 @@ function escapeHtml(s) {
return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
function highlightText(text, query) {
var escaped = escapeHtml(text);
var re = new RegExp('(' + query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'gi');
return escaped.replace(re, '<mark style="background:#ffeb3b;color:#000;padding:0 2px;border-radius:2px">$1</mark>');
}
async function fulltextSearch(q) {
el.innerHTML = '<p>Searching...</p>';
var results = [];
@@ -173,7 +179,7 @@ async function fulltextSearch(q) {
html += '<p style="margin:4px 0"><a href="#" onclick="loadDoc(\'' + r.module + '\');return false" style="font-weight:600">'
+ r.module + ' ' + escapeHtml(r.title) + '</a> <span style="color:#888;font-size:12px">line ' + r.line + '</span><br>'
+ '<code style="background:#f5f5f5;padding:2px 6px;border-radius:3px;font-size:12px;display:inline-block;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">'
+ escapeHtml(r.text) + '</code></p>';
+ highlightText(r.text, q) + '</code></p>';
}
el.innerHTML = html;
}