mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-19 18:42:34 +00:00
Upgrade algoliasearch to v5
This commit is contained in:
parent
0a170b8cd6
commit
c8f828dd66
@ -118,9 +118,8 @@ firebase_firestore:
|
|||||||
integrity: sha256-r1EpenNle+MZs+KB73PFJnrmIF3k29t5XGrSqfZ9PPw=
|
integrity: sha256-r1EpenNle+MZs+KB73PFJnrmIF3k29t5XGrSqfZ9PPw=
|
||||||
algolia_search:
|
algolia_search:
|
||||||
name: algoliasearch
|
name: algoliasearch
|
||||||
version: 4.24.0
|
version: 5.23.0
|
||||||
file: dist/algoliasearch-lite.umd.js
|
file: dist/lite/builds/browser.umd.js
|
||||||
integrity: sha256-b2n6oSgG4C1stMT/yc/ChGszs9EY/Mhs6oltEjQbFCQ=
|
|
||||||
local_search:
|
local_search:
|
||||||
name: hexo-generator-searchdb
|
name: hexo-generator-searchdb
|
||||||
version: 1.4.1
|
version: 1.4.1
|
||||||
|
|||||||
@ -106,6 +106,7 @@ if (hexo-config('local_search.enable') or hexo-config('algolia_search.enable'))
|
|||||||
|
|
||||||
p.search-result {
|
p.search-result {
|
||||||
border-bottom: 1px dashed $grey-light;
|
border-bottom: 1px dashed $grey-light;
|
||||||
|
margin: 0 0 10px;
|
||||||
padding: 5px 0;
|
padding: 5px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
source/js/third-party/search/algolia-search.js
vendored
30
source/js/third-party/search/algolia-search.js
vendored
@ -1,9 +1,8 @@
|
|||||||
/* global CONFIG, NexT, pjax, algoliasearch */
|
/* global CONFIG, NexT, pjax */
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const { indexName, appID, apiKey, hits } = CONFIG.algolia;
|
const { indexName, appID, apiKey, hits } = CONFIG.algolia;
|
||||||
const client = algoliasearch(appID, apiKey);
|
const client = window['algoliasearch/lite'].liteClient(appID, apiKey);
|
||||||
const index = client.initIndex(indexName);
|
|
||||||
|
|
||||||
const input = document.querySelector('.search-input');
|
const input = document.querySelector('.search-input');
|
||||||
const container = document.querySelector('.search-result-container');
|
const container = document.querySelector('.search-result-container');
|
||||||
@ -11,10 +10,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const formatHits = data => {
|
const formatHits = data => {
|
||||||
const { title, excerpt, excerptStrip, contentStripTruncate } = data._highlightResult;
|
const { title, excerpt, excerptStrip, contentStripTruncate } = data._highlightResult;
|
||||||
let result = `<li><a href="${data.permalink}" class="search-result-title">${title.value}</a>`;
|
let result = `<li><a href="${data.permalink}" class="search-result-title">${title.value}</a>`;
|
||||||
const content = excerpt || excerptStrip || contentStripTruncate;
|
const content = excerpt?.value || excerptStrip?.value || contentStripTruncate?.value;
|
||||||
if (content && content.value) {
|
if (content) {
|
||||||
const div = document.createElement('div');
|
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>`;
|
result += `<a href="${data.permalink}"><p class="search-result">${div.textContent.substring(0, 100)}...</p></a></li>`;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -30,14 +29,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
}
|
}
|
||||||
isSearching = true;
|
isSearching = true;
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const data = await index.search(searchText, {
|
const result = await client.search({
|
||||||
page,
|
requests: [{
|
||||||
attributesToRetrieve : ['permalink'],
|
indexName,
|
||||||
attributesToHighlight: ['title', 'excerpt', 'excerptStrip', 'contentStripTruncate'],
|
page,
|
||||||
hitsPerPage : hits.per_page || 10,
|
query : searchText,
|
||||||
highlightPreTag : '<mark class="search-keyword">',
|
hitsPerPage : hits.per_page || 10,
|
||||||
highlightPostTag : '</mark>'
|
attributesToRetrieve : ['permalink'],
|
||||||
|
attributesToHighlight: ['title', 'excerpt', 'excerptStrip', 'contentStripTruncate'],
|
||||||
|
highlightPreTag : '<mark class="search-keyword">',
|
||||||
|
highlightPostTag : '</mark>'
|
||||||
|
}]
|
||||||
});
|
});
|
||||||
|
const data = result.results[0];
|
||||||
if (data.nbHits === 0) {
|
if (data.nbHits === 0) {
|
||||||
container.innerHTML = '<div class="search-result-icon"><i class="far fa-frown fa-5x"></i></div>';
|
container.innerHTML = '<div class="search-result-icon"><i class="far fa-frown fa-5x"></i></div>';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user