Upgrade algoliasearch to v5

This commit is contained in:
Mimi 2025-03-29 14:40:42 +08:00
parent 0a170b8cd6
commit c8f828dd66
3 changed files with 20 additions and 16 deletions

View File

@ -118,9 +118,8 @@ firebase_firestore:
integrity: sha256-r1EpenNle+MZs+KB73PFJnrmIF3k29t5XGrSqfZ9PPw=
algolia_search:
name: algoliasearch
version: 4.24.0
file: dist/algoliasearch-lite.umd.js
integrity: sha256-b2n6oSgG4C1stMT/yc/ChGszs9EY/Mhs6oltEjQbFCQ=
version: 5.23.0
file: dist/lite/builds/browser.umd.js
local_search:
name: hexo-generator-searchdb
version: 1.4.1

View File

@ -106,6 +106,7 @@ if (hexo-config('local_search.enable') or hexo-config('algolia_search.enable'))
p.search-result {
border-bottom: 1px dashed $grey-light;
margin: 0 0 10px;
padding: 5px 0;
}

View File

@ -1,9 +1,8 @@
/* global CONFIG, NexT, pjax, algoliasearch */
/* global CONFIG, NexT, pjax */
document.addEventListener('DOMContentLoaded', () => {
const { indexName, appID, apiKey, hits } = CONFIG.algolia;
const client = algoliasearch(appID, apiKey);
const index = client.initIndex(indexName);
const client = window['algoliasearch/lite'].liteClient(appID, apiKey);
const input = document.querySelector('.search-input');
const container = document.querySelector('.search-result-container');
@ -11,10 +10,10 @@ document.addEventListener('DOMContentLoaded', () => {
const formatHits = data => {
const { title, excerpt, excerptStrip, contentStripTruncate } = data._highlightResult;
let result = `<li><a href="${data.permalink}" class="search-result-title">${title.value}</a>`;
const content = excerpt || excerptStrip || contentStripTruncate;
if (content && content.value) {
const content = excerpt?.value || excerptStrip?.value || contentStripTruncate?.value;
if (content) {
const div = document.createElement('div');
div.innerHTML = content.value;
div.innerHTML = content;
result += `<a href="${data.permalink}"><p class="search-result">${div.textContent.substring(0, 100)}...</p></a></li>`;
}
return result;
@ -30,14 +29,19 @@ document.addEventListener('DOMContentLoaded', () => {
}
isSearching = true;
const startTime = Date.now();
const data = await index.search(searchText, {
page,
attributesToRetrieve : ['permalink'],
attributesToHighlight: ['title', 'excerpt', 'excerptStrip', 'contentStripTruncate'],
hitsPerPage : hits.per_page || 10,
highlightPreTag : '<mark class="search-keyword">',
highlightPostTag : '</mark>'
const result = await client.search({
requests: [{
indexName,
page,
query : searchText,
hitsPerPage : hits.per_page || 10,
attributesToRetrieve : ['permalink'],
attributesToHighlight: ['title', 'excerpt', 'excerptStrip', 'contentStripTruncate'],
highlightPreTag : '<mark class="search-keyword">',
highlightPostTag : '</mark>'
}]
});
const data = result.results[0];
if (data.nbHits === 0) {
container.innerHTML = '<div class="search-result-icon"><i class="far fa-frown fa-5x"></i></div>';
} else {