Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(js): shorten MathJax filename and minify + fingerprint assets
Remove fingerprinting from MathJax config JS to shorten its generated filename. Reduces chance of JS filenames becoming too long for user's OS to handle due to long Sha512 asset fingerprinting. Minifies and fingerprints Academic's own JS for faster loading and to prevent old versions getting cached by web browser. Refactors Algolia search JS into its own file. Close #768 See #794
- Loading branch information
Showing
5 changed files
with
122 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/************************************************* | ||
* Academic | ||
* https://github.com/gcushen/hugo-academic | ||
* | ||
* Algolia based search algorithm. | ||
**************************************************/ | ||
|
||
if ((typeof instantsearch === 'function') && $('#search-box').length) { | ||
function getTemplate(templateName) { | ||
return document.querySelector(`#${templateName}-template`).innerHTML; | ||
} | ||
|
||
const options = { | ||
appId: algoliaConfig.appId, | ||
apiKey: algoliaConfig.apiKey, | ||
indexName: algoliaConfig.indexName, | ||
routing: true, | ||
searchParameters: { | ||
hitsPerPage: 10 | ||
}, | ||
searchFunction: function (helper) { | ||
let searchResults = document.querySelector('#search-hits') | ||
if (helper.state.query === '') { | ||
searchResults.style.display = 'none'; | ||
return; | ||
} | ||
helper.search(); | ||
searchResults.style.display = 'block'; | ||
} | ||
}; | ||
|
||
const search = instantsearch(options); | ||
|
||
// Initialize search box. | ||
search.addWidget( | ||
instantsearch.widgets.searchBox({ | ||
container: '#search-box', | ||
autofocus: false, | ||
reset: true, | ||
poweredBy: algoliaConfig.poweredBy, | ||
placeholder: i18n.placeholder | ||
}) | ||
); | ||
|
||
// Initialize search results. | ||
search.addWidget( | ||
instantsearch.widgets.infiniteHits({ | ||
container: '#search-hits', | ||
escapeHits: true, | ||
templates: { | ||
empty: '<div class="search-no-results">' + i18n.no_results + '</div>', | ||
item: getTemplate('search-hit-algolia') | ||
}, | ||
cssClasses: { | ||
showmoreButton: 'btn btn-outline-primary' | ||
} | ||
}) | ||
); | ||
|
||
// On render search results, localize the content type metadata. | ||
search.on('render', function () { | ||
$('.search-hit-type').each(function (index) { | ||
let content_key = $(this).text(); | ||
if (content_key in content_type) { | ||
$(this).text(content_type[content_key]); | ||
} | ||
}); | ||
}); | ||
|
||
// Start search. | ||
search.start(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters