aboutsummaryrefslogtreecommitdiff
path: root/assets/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/search.js')
-rw-r--r--assets/search.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/assets/search.js b/assets/search.js
new file mode 100644
index 0000000..84e522a
--- /dev/null
+++ b/assets/search.js
@@ -0,0 +1,41 @@
+addEventListener("load", function() {
+ let input = document.querySelector("#book-search");
+ let results = document.querySelector("#book-search-results");
+
+ Promise.all([
+ loadScript("{{ "lunr.min.js" | relURL }}"),
+ loadScript("{{ "index.json" | relURL }}")
+ ]).then(enableLunr);
+
+ function enableLunr() {
+ results.idx = lunr(function() {
+ this.ref('href')
+ this.field('title')
+ this.field('content')
+
+ window.lunrData.forEach(function (page) {
+ this.add(page)
+ }, this)
+ });
+ input.addEventListener("keyup", search);
+ }
+
+ function search() {
+ if (input.value) {
+ var hits = results.idx.search(`${input.value}*`);
+ results.innerHTML = JSON.stringify(hits);
+ } else {
+ results.innerHTML = '';
+ }
+ }
+
+ function loadScript(src) {
+ return new Promise(function(resolve, reject) {
+ let script = document.createElement('script');
+ script.src = src;
+ script.onload = () => resolve(script);
+
+ document.head.append(script);
+ });
+ }
+});