add search support using fusejs

This commit is contained in:
Mivinci 2023-05-12 20:30:35 +08:00
parent 844f9fcfa7
commit ce0ce5be25
14 changed files with 156 additions and 26 deletions

24
assets/js/search.js Normal file
View file

@ -0,0 +1,24 @@
import * as params from '@params';
const search_input = document.querySelector("#search-input");
const search_result = document.querySelector("#search-result");
let fuse;
window.onload = async function() {
const data = await fetch("../index.json").then(res => res.json());
const opts = params.search.fuse;
fuse = new Fuse(data, opts);
}
search_input.addEventListener("input", function () {
if (!fuse) return;
const results = fuse.search(this.value.trim());
let html = '';
if (results.length > 0) {
for (const v of results) {
html += `<li><a href="${v.item.permalink}">${v.item.title}</a></li>`;
}
}
search_result.innerHTML = html;
})