summaryrefslogtreecommitdiffstats
path: root/assets/js/fuji.js
diff options
context:
space:
mode:
authoramzrk22020-06-15 23:50:54 +0800
committeramzrk22020-06-15 23:50:54 +0800
commita3376d1ad133455b73ca893e222c1aec89ae6d44 (patch)
tree0f7aecb84d6322972c1580fea2ed3ee60f8eaf25 /assets/js/fuji.js
parent94233f18bd32d6871288f578df1ecdffe833c02f (diff)
downloadhugo-theme-fuji-a3376d1ad133455b73ca893e222c1aec89ae6d44.tar.gz
hugo-theme-fuji-a3376d1ad133455b73ca893e222c1aec89ae6d44.tar.bz2
hugo-theme-fuji-a3376d1ad133455b73ca893e222c1aec89ae6d44.zip
Update searching based on Fuse.js
Diffstat (limited to 'assets/js/fuji.js')
-rw-r--r--assets/js/fuji.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/assets/js/fuji.js b/assets/js/fuji.js
index b72c30e..d1be458 100644
--- a/assets/js/fuji.js
+++ b/assets/js/fuji.js
@@ -83,3 +83,40 @@ document.querySelector('.btn .btn-toggle-mode').addEventListener('click', () =>
}
}
});
+
+// search by fuse.js
+function searchAll(key, index) {
+ let fuse = new Fuse(index, {
+ keys: ['title', 'tags'],
+ });
+ let result = fuse.search(key);
+ console.log(result);
+ if (result.length > 0) {
+ document.getElementById('search-result').innerHTML = template('search-result-template', result);
+ }
+}
+
+let urlParams = new URLSearchParams(window.location.search); // get params from URL
+if (urlParams.has('search')) {
+ let key = urlParams.get('search'); // get search keyword
+ document.querySelector('.search-input input').setAttribute('value', key);
+ // get search index from json
+ let xhr = new XMLHttpRequest();
+ xhr.open('GET', '/index.json', true);
+ xhr.responseType = 'json';
+ xhr.onerror = function (e) {
+ console.error(`${xhr.status} ${xhr.statusText}`);
+ };
+ xhr.onload = function () {
+ if (xhr.readyState === 4) {
+ if (xhr.status === 200) {
+ // use index json to search
+ console.log(xhr.response);
+ searchAll(key, xhr.response);
+ } else {
+ console.error(`${xhr.status} ${xhr.statusText}`);
+ }
+ }
+ };
+ xhr.send(null);
+}