Update index.js

doks was displaying only the very last search result.
This commit is contained in:
Soichi Hayashi 2021-08-10 09:53:17 -04:00 committed by GitHub
parent dfdc28ac50
commit b575ca0eea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 24 deletions

View File

@ -117,39 +117,36 @@ Source:
suggestions.addEventListener('click', accept_suggestion, true); suggestions.addEventListener('click', accept_suggestion, true);
function show_results(){ function show_results(){
const maxResult = 5;
var value = this.value; var value = this.value;
var results = index.search(value, { limit: 5, index: ["content"], enrich: true }); var results = index.search(value, {limit: maxResult, enrich: true});
var entry, childs = suggestions.childNodes;
var i = 0, len = results.length;
suggestions.classList.remove('d-none'); suggestions.classList.remove('d-none');
suggestions.innerHTML = "";
results.forEach(function(results) { //flatSearch now returns results for each index field. create a single list
const flatResults = {}; //keyed by href to dedupe results
entry = document.createElement('div'); results.forEach(result=>{
result.result.forEach(r=>{
entry.innerHTML = '<a href><span></span><span></span></a>'; flatResults[r.doc.href] = r.doc;
});
a = entry.querySelector('a'),
t = entry.querySelector('span:first-child'),
d = entry.querySelector('span:nth-child(2)');
// console.log(results);
a.href = results.result[i].doc.href;
t.textContent = results.result[i].doc.title;
d.textContent = results.result[i].doc.description;
suggestions.appendChild(entry);
}); });
while(childs.length > len){ //construct a list of suggestions list
for(const href in flatResults) {
const doc = flatResults[href];
suggestions.removeChild(childs[i]) const entry = document.createElement('div');
entry.innerHTML = '<a href><span></span><span></span></a>';
entry.querySelector('a').href = href;
entry.querySelector('span:first-child').textContent = doc.title;
entry.querySelector('span:nth-child(2)').textContent = doc.description;
suggestions.appendChild(entry);
if(suggestions.childElementCount == maxResult) break;
} }
} }
function accept_suggestion(){ function accept_suggestion(){