From c8f828dd66c4138439dabea23a52dcfcdea58c9e Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 29 Mar 2025 14:40:42 +0800 Subject: [PATCH] Upgrade algoliasearch to v5 --- _vendors.yml | 5 ++-- .../components/third-party/search.styl | 1 + .../js/third-party/search/algolia-search.js | 30 +++++++++++-------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/_vendors.yml b/_vendors.yml index cab0944..9fb3524 100644 --- a/_vendors.yml +++ b/_vendors.yml @@ -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 diff --git a/source/css/_common/components/third-party/search.styl b/source/css/_common/components/third-party/search.styl index 5b229ad..b11ba6e 100644 --- a/source/css/_common/components/third-party/search.styl +++ b/source/css/_common/components/third-party/search.styl @@ -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; } diff --git a/source/js/third-party/search/algolia-search.js b/source/js/third-party/search/algolia-search.js index 6a07078..933ba08 100644 --- a/source/js/third-party/search/algolia-search.js +++ b/source/js/third-party/search/algolia-search.js @@ -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 = `
  • ${title.value}`; - 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 += `

    ${div.textContent.substring(0, 100)}...

  • `; } 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 : '', - highlightPostTag : '' + const result = await client.search({ + requests: [{ + indexName, + page, + query : searchText, + hitsPerPage : hits.per_page || 10, + attributesToRetrieve : ['permalink'], + attributesToHighlight: ['title', 'excerpt', 'excerptStrip', 'contentStripTruncate'], + highlightPreTag : '', + highlightPostTag : '' + }] }); + const data = result.results[0]; if (data.nbHits === 0) { container.innerHTML = '
    '; } else {