feat: highlight matched keywords in search results
This commit is contained in:
@@ -146,6 +146,12 @@ function escapeHtml(s) {
|
|||||||
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
async function fulltextSearch(q) {
|
||||||
el.innerHTML = '<p>Searching...</p>';
|
el.innerHTML = '<p>Searching...</p>';
|
||||||
var results = [];
|
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">'
|
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>'
|
+ 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">'
|
+ '<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;
|
el.innerHTML = html;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user