diff --git a/settings.yaml b/settings.yaml index 80aa426..2cd906b 100644 --- a/settings.yaml +++ b/settings.yaml @@ -1101,7 +1101,7 @@ spec: label: 关闭 - $formkit: group name: search - label: 搜索 + label: 搜索页面 help: 使用新版搜索需要 halo >= 2.17.0 & 搜索插件 >= 1.5.0。 value: search_enable: false diff --git a/src/css/style.less b/src/css/style.less index b66f9b2..bdf5ca1 100644 --- a/src/css/style.less +++ b/src/css/style.less @@ -5227,6 +5227,7 @@ button.swiper-pagination-bullet { /* 搜索界面 */ .search { + margin-top: 1.4rem !important; .search-form-inner { width: 100%; @@ -5292,7 +5293,7 @@ button.swiper-pagination-bullet { display: inline-block; } - b { + b, mark { color: var(--light-z); background: var(--theme); } diff --git a/src/js/search.js b/src/js/search.js new file mode 100644 index 0000000..ba96452 --- /dev/null +++ b/src/js/search.js @@ -0,0 +1,91 @@ +$(function () { + var searchForm = document.getElementById('dream-search-form') + var target = DreamConfig.search_target + var searchInput = document.getElementById('halo-search-form-text-input') + var searchResult = $('#dream-search-result') + var searchResultEmpty = $('#dream-search-result-empty') + searchForm.addEventListener('submit', function (event) { + event.preventDefault() + findResult(searchInput.value) + }) + document.addEventListener('keydown', function (event) { + if (event.key === 'Enter') { + if (searchForm.contains(document.activeElement)) { + event.preventDefault() + findResult(searchInput.value) + } + } + }) + // 监听输入事件 + searchInput.addEventListener('input', function (event) { + findResult(event.target.value) + }) + + function removeHTMLTags(str) { + // 保留 标签,移除其他所有标签 + return str.replace(/<(?!(?:mark\b[^<>]*)<\/mark>|mark\b)[^<>]*>/gi, '') + } + + function findResult(keyword) { + if (!keyword) { + searchResult.empty() + searchResultEmpty.show() + return + } + Utils.request({ + url: '/apis/api.halo.run/v1alpha1/indices/post', + contentType: 'application/json;charset=UTF-8', + returnRaw: true, + data: { + keyword, + limit: $('#halo-search-form-limit').val(), + highlightPreTag: '', + highlightPostTag: '' + } + }) + .then((_res) => { + console.log(_res) + if (_res.hits.length > 0) { + searchResultEmpty.hide() + searchResult.empty() + for (var i = 0; i < _res.hits.length; i++) { + var hit = _res.hits[i] + searchResult.append(' -