From 29fcd231e3da9b33c237886f3c1bb30a557ad641 Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 19 Mar 2022 16:54:23 +0530 Subject: Initial Commit --- themes/PaperMod/assets/js/fastsearch.js | 147 ++++++++++++++++++++++++++++ themes/PaperMod/assets/js/fuse.basic.min.js | 9 ++ themes/PaperMod/assets/js/highlight.min.js | 44 +++++++++ 3 files changed, 200 insertions(+) create mode 100644 themes/PaperMod/assets/js/fastsearch.js create mode 100644 themes/PaperMod/assets/js/fuse.basic.min.js create mode 100644 themes/PaperMod/assets/js/highlight.min.js (limited to 'themes/PaperMod/assets/js') diff --git a/themes/PaperMod/assets/js/fastsearch.js b/themes/PaperMod/assets/js/fastsearch.js new file mode 100644 index 0000000..ea11022 --- /dev/null +++ b/themes/PaperMod/assets/js/fastsearch.js @@ -0,0 +1,147 @@ +import * as params from '@params'; + +var fuse; // holds our search engine +var resList = document.getElementById('searchResults'); +var sInput = document.getElementById('searchInput'); +var first, last, current_elem = null +var resultsAvailable = false; + +// load our search index +window.onload = function () { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var data = JSON.parse(xhr.responseText); + if (data) { + // fuse.js options; check fuse.js website for details + var options = { + distance: 100, + threshold: 0.4, + ignoreLocation: true, + keys: [ + 'title', + 'permalink', + 'summary', + 'content' + ] + }; + if (params.fuseOpts) { + options = { + isCaseSensitive: params.fuseOpts.iscasesensitive ? params.fuseOpts.iscasesensitive : false, + includeScore: params.fuseOpts.includescore ? params.fuseOpts.includescore : false, + includeMatches: params.fuseOpts.includematches ? params.fuseOpts.includematches : false, + minMatchCharLength: params.fuseOpts.minmatchcharlength ? params.fuseOpts.minmatchcharlength : 1, + shouldSort: params.fuseOpts.shouldsort ? params.fuseOpts.shouldsort : true, + findAllMatches: params.fuseOpts.findallmatches ? params.fuseOpts.findallmatches : false, + keys: params.fuseOpts.keys ? params.fuseOpts.keys : ['title', 'permalink', 'summary', 'content'], + location: params.fuseOpts.location ? params.fuseOpts.location : 0, + threshold: params.fuseOpts.threshold ? params.fuseOpts.threshold : 0.4, + distance: params.fuseOpts.distance ? params.fuseOpts.distance : 100, + ignoreLocation: params.fuseOpts.ignorelocation ? params.fuseOpts.ignorelocation : true + } + } + fuse = new Fuse(data, options); // build the index from the json file + } + } else { + console.log(xhr.responseText); + } + } + }; + xhr.open('GET', "../index.json"); + xhr.send(); +} + +function activeToggle(ae) { + document.querySelectorAll('.focus').forEach(function (element) { + // rm focus class + element.classList.remove("focus") + }); + if (ae) { + ae.focus() + document.activeElement = current_elem = ae; + ae.parentElement.classList.add("focus") + } else { + document.activeElement.parentElement.classList.add("focus") + } +} + +function reset() { + resultsAvailable = false; + resList.innerHTML = sInput.value = ''; // clear inputbox and searchResults + sInput.focus(); // shift focus to input box +} + +// execute search as each character is typed +sInput.onkeyup = function (e) { + // run a search query (for "term") every time a letter is typed + // in the search box + if (fuse) { + const results = fuse.search(this.value.trim()); // the actual query being run using fuse.js + if (results.length !== 0) { + // build our html if result exists + let resultSet = ''; // our results bucket + + for (let item in results) { + resultSet += `