From ad8f9f55ea79a4d587aecc69698808da82de3d62 Mon Sep 17 00:00:00 2001 From: somhairle Date: Tue, 21 Apr 2026 20:40:27 +0800 Subject: 二〇二六年四月二十一日 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/theme/source/js/search.js | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 themes/theme/source/js/search.js (limited to 'themes/theme/source/js/search.js') diff --git a/themes/theme/source/js/search.js b/themes/theme/source/js/search.js new file mode 100644 index 00000000..27abb4e0 --- /dev/null +++ b/themes/theme/source/js/search.js @@ -0,0 +1,68 @@ +// Search functionality for standalone search page + +(function () { + 'use strict'; + + let localSearch = null; + let isDataLoaded = false; + + function initSearch() { + const input = document.getElementById('search-input'); + const container = document.getElementById('search-results'); + + if (!input || !container || typeof LocalSearch === 'undefined') { + container.innerHTML = '
搜索功能初始化失败
'; + return; + } + + localSearch = new LocalSearch({ + path: searchConfig.path, + top_n_per_article: searchConfig.top_n_per_article, + unescape: searchConfig.unescape + }); + + localSearch.fetchData(); + + window.addEventListener('search:loaded', function () { + isDataLoaded = true; + container.innerHTML = '
'; + }); + + function displaySearchResult() { + if (!isDataLoaded) { + container.innerHTML = '
加载中...
'; + return; + } + + const searchText = input.value.trim().toLowerCase(); + + if (searchText.length === 0) { + container.innerHTML = '
'; + return; + } + + const keywords = searchText.split(/[-\s]+/); + const resultItems = localSearch.getResultItems(keywords); + + if (resultItems.length === 0) { + container.innerHTML = '
未找到相关结果
'; + } else { + container.innerHTML = ` +
找到 ${resultItems.length} 个结果
+ + `; + } + } + + input.addEventListener('input', displaySearchResult); + input.focus(); + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', initSearch); + } else { + initSearch(); + } +})(); -- cgit v1.2.3