From ba48765e2c8cdda86724c474bb29d35bb364c1ba Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Sat, 10 Apr 2021 00:58:23 +0530 Subject: [PATCH] Use js.Build to fetch fuseOpts; also load Search on window load --- assets/js/fastsearch.js | 8 +++++--- layouts/partials/head.html | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/assets/js/fastsearch.js b/assets/js/fastsearch.js index 2546310a..4b9fdb64 100644 --- a/assets/js/fastsearch.js +++ b/assets/js/fastsearch.js @@ -1,11 +1,13 @@ +import * as params from '@params'; + var fuse; // holds our search engine var resList = document.getElementById('searchResults'); var sInput = document.getElementById('searchInput'); var first, last = null var resultsAvailable = false; -// load our search index, only executed onload -function loadSearch() { +// load our search index +window.onload = function () { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { @@ -24,7 +26,7 @@ function loadSearch() { 'content' ] }; - {{ if . }}options = {{ jsonify . }}{{ end }} // load custom options from .Site.Params.fuseOpts + if (params.fuseOpts) options = params.fuseOpts; fuse = new Fuse(data, options); // build the index from the json file } } else { diff --git a/layouts/partials/head.html b/layouts/partials/head.html index efcdf1a6..042f9dd2 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -61,14 +61,14 @@ {{- /* Search */}} {{- if (eq .Layout `search`) -}} -{{- $fastsearch := resources.Get "js/fastsearch.js" | resources.ExecuteAsTemplate "js/fastsearch.js" .Site.Params.fuseOpts }} +{{- $fastsearch := resources.Get "js/fastsearch.js" | js.Build (dict "params" (dict "fuseOpts" .Site.Params.fuseOpts)) }} {{- $fusejs := resources.Get "js/fuse.js" }} {{- if not .Site.Params.assets.disableFingerprinting }} {{- $search := (slice $fusejs $fastsearch ) | resources.Concat "assets/js/search.js" | minify | fingerprint }} - + {{- else }} {{- $search := (slice $fusejs $fastsearch ) | resources.Concat "assets/js/search.js" | minify }} - + {{- end }} {{- end -}}