mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-21 19:32:34 +00:00
Fix search with Pjax
This commit is contained in:
parent
7129d09d81
commit
eb194a7258
10
source/js/third-party/search/algolia-search.js
vendored
10
source/js/third-party/search/algolia-search.js
vendored
@ -1,4 +1,4 @@
|
|||||||
/* global instantsearch, algoliasearch, CONFIG */
|
/* global instantsearch, algoliasearch, CONFIG, pjax */
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
const { indexName, appID, apiKey, hits } = CONFIG.algolia;
|
const { indexName, appID, apiKey, hits } = CONFIG.algolia;
|
||||||
@ -13,9 +13,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
window.pjax && search.on('render', () => {
|
if (typeof pjax === 'object') {
|
||||||
window.pjax.refresh(document.querySelector('.algolia-hits'));
|
search.on('render', () => {
|
||||||
});
|
pjax.refresh(document.querySelector('.algolia-hits'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Registering Widgets
|
// Registering Widgets
|
||||||
search.addWidgets([
|
search.addWidgets([
|
||||||
|
|||||||
16
source/js/third-party/search/local-search.js
vendored
16
source/js/third-party/search/local-search.js
vendored
@ -1,4 +1,4 @@
|
|||||||
/* global CONFIG */
|
/* global CONFIG, pjax */
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
if (!CONFIG.path) {
|
if (!CONFIG.path) {
|
||||||
@ -192,7 +192,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
container.innerHTML = `<div class="search-stats">${stats}</div>
|
container.innerHTML = `<div class="search-stats">${stats}</div>
|
||||||
<hr>
|
<hr>
|
||||||
<ul class="search-result-list">${resultItems.map(result => result.item).join('')}</ul>`;
|
<ul class="search-result-list">${resultItems.map(result => result.item).join('')}</ul>`;
|
||||||
window.pjax && window.pjax.refresh(container);
|
if (typeof pjax === 'object') pjax.refresh(container);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -203,13 +203,11 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
.then(res => {
|
.then(res => {
|
||||||
// Get the contents from search data
|
// Get the contents from search data
|
||||||
isfetched = true;
|
isfetched = true;
|
||||||
datas = isXml ? [...new DOMParser().parseFromString(res, 'text/xml').querySelectorAll('entry')].map(element => {
|
datas = isXml ? [...new DOMParser().parseFromString(res, 'text/xml').querySelectorAll('entry')].map(element => ({
|
||||||
return {
|
title : element.querySelector('title').textContent,
|
||||||
title : element.querySelector('title').textContent,
|
content: element.querySelector('content').textContent,
|
||||||
content: element.querySelector('content').textContent,
|
url : element.querySelector('url').textContent
|
||||||
url : element.querySelector('url').textContent
|
})) : JSON.parse(res);
|
||||||
};
|
|
||||||
}) : JSON.parse(res);
|
|
||||||
// Only match articles with non-empty titles
|
// Only match articles with non-empty titles
|
||||||
datas = datas.filter(data => data.title).map(data => {
|
datas = datas.filter(data => data.title).map(data => {
|
||||||
data.title = data.title.trim();
|
data.title = data.title.trim();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user