Added search using elasticlunr.
This commit is contained in:
parent
205a096654
commit
32ebfc6176
7 changed files with 71 additions and 41 deletions
38
assets/search.js
Normal file
38
assets/search.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
fetch('/search_index.json')
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
const idx = elasticlunr.Index.load(data);
|
||||
const docs = idx.documentStore.docs;
|
||||
console.log(Object.values(docs)[0])
|
||||
const searchInput = document.getElementById('search');
|
||||
const out = document.getElementById('searchResults');
|
||||
const filterSelect = document.getElementById('searchMode');
|
||||
|
||||
function runSearch(q, tags) {
|
||||
return idx.search(q, { expand: true })
|
||||
.map(r => docs[r.ref] )
|
||||
.filter(d => tags === 'all' || d.tags.includes(tags));
|
||||
}
|
||||
|
||||
function render(doc) {
|
||||
return `<div>
|
||||
<img class="searchItemImage" src="${doc.image}">
|
||||
<a class="searchItemTitle" href="${doc.url}">${doc.title}</a>
|
||||
<p class="searchItemDescription">${doc.subtitle}</p>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function update() {
|
||||
const q = searchInput.value.trim();
|
||||
if (!q) {
|
||||
out.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const results = runSearch(q, filterSelect.value);
|
||||
out.innerHTML = results.map(render).join('');
|
||||
}
|
||||
|
||||
searchInput.addEventListener('input', update);
|
||||
filterSelect.addEventListener('change', update);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue