From 6259ac8e98fc3a4001fb986185b9ef38e1fd636e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E6=97=A5=E8=AF=AD?= Date: Tue, 19 Nov 2024 15:43:35 +0800 Subject: [PATCH] [fix] algoliasearch is not defined (#542) --- _config.yml | 1 + layout/_plugins/search/algolia_search.ejs | 23 ++-- source/js/search/algolia-search.js | 130 +++++++++++----------- 3 files changed, 78 insertions(+), 76 deletions(-) diff --git a/_config.yml b/_config.yml index b624220..f236417 100755 --- a/_config.yml +++ b/_config.yml @@ -202,6 +202,7 @@ search: appId: apiKey: indexName: + js: https://gcore.jsdelivr.net/algoliasearch/3/algoliasearch.min.js ######## Comments ######## diff --git a/layout/_plugins/search/algolia_search.ejs b/layout/_plugins/search/algolia_search.ejs index 91e7b2f..7a75434 100644 --- a/layout/_plugins/search/algolia_search.ejs +++ b/layout/_plugins/search/algolia_search.ejs @@ -1,13 +1,12 @@ - \ No newline at end of file + window.addEventListener('DOMContentLoaded', (event) => { + window.searchConfig = { + appId: '<%- conf.appId %>', + apiKey: '<%- conf.apiKey %>', + indexName: '<%- conf.indexName %>', + js: '<%- conf.js %>', + hitsPerPage: 100, + }; + utils.js('/js/search/algolia-search.js', { defer: true }); + }); + diff --git a/source/js/search/algolia-search.js b/source/js/search/algolia-search.js index 2398bf0..a81ff61 100644 --- a/source/js/search/algolia-search.js +++ b/source/js/search/algolia-search.js @@ -1,75 +1,77 @@ -utils.jq(() => { - var $inputArea = $("input#search-input"); - if ($inputArea.length === 0) { - return; - } - - var $resultArea = $("#search-result"); - var $searchWrapper = $("#search-wrapper"); - var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey); - var index = client.initIndex(window.searchConfig.indexName); - - function filterResults(hits, filterPath) { - if (!filterPath || filterPath === '/') return hits; - var regex = new RegExp(filterPath); - return hits.filter(hit => regex.test(hit.url)); - } - - function displayResults(hits) { - var $resultList = $("