diff options
Diffstat (limited to 'assets/search.js')
-rw-r--r-- | assets/search.js | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/assets/search.js b/assets/search.js index eb38c1d..3cc9702 100644 --- a/assets/search.js +++ b/assets/search.js @@ -3,19 +3,20 @@ (function() { const input = document.querySelector("#book-search-input"); const results = document.querySelector("#book-search-results"); - const dummy = document.querySelector("#book-search-dummy"); input.addEventListener("focus", init); input.addEventListener("keyup", search); function init() { - loadScript("{{ "lunr.min.js" | relURL }}") + input.removeEventListener("focus", init); //init once + input.required = true; + + loadScript("{{ "lunr.min.js" | relURL }}"); loadScript("{{ $searchData.RelPermalink }}", function() { input.readOnly = false; + input.required = false; search(); }); - - input.removeEventListener("focus", init); } function search() { @@ -23,18 +24,18 @@ results.removeChild(results.firstChild); } - if (!input.value || !window.bookSearch) { - return + if (!input.value) { + return; } const terms = lunr.tokenizer(input.value); const searchHits = window.bookSearch.idx.query(function(query) { query.term(terms, { - boost: 100, + boost: 100 }); query.term(terms, { boost: 10, - wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING, + wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING }); query.term(terms, { editDistance: 2 @@ -43,8 +44,8 @@ searchHits.slice(0, 10).forEach(function(hit) { const page = window.bookSearch.pages[hit.ref]; - const li = dummy.querySelector("li").cloneNode(true), - a = li.querySelector("a"); + const li = document.createElement("li"), + a = li.appendChild(document.createElement("a")); a.href = page.href; a.textContent = page.title; |