diff options
| author | Serghei Iakovlev <egrep@protonmail.ch> | 2022-07-11 09:44:01 +0200 |
|---|---|---|
| committer | Serghei Iakovlev <egrep@protonmail.ch> | 2022-07-11 09:44:01 +0200 |
| commit | 0e5fe09b09bf9491732748520e6ad37b74453488 (patch) | |
| tree | 10e6a9b070626353efcdba9f9bc69b3007d8a57d /assets | |
| parent | 477e47f4eda5344b799839d0d645d125c903b099 (diff) | |
| download | gohugo-theme-ed-0e5fe09b09bf9491732748520e6ad37b74453488.tar.gz | |
Rework site search to pass config params
Diffstat (limited to 'assets')
| -rw-r--r-- | assets/js/.eslintrc.yml | 2 | ||||
| -rw-r--r-- | assets/js/ga.js | 2 | ||||
| -rw-r--r-- | assets/js/search.js | 52 |
3 files changed, 11 insertions, 45 deletions
diff --git a/assets/js/.eslintrc.yml b/assets/js/.eslintrc.yml new file mode 100644 index 0000000..add5a78 --- /dev/null +++ b/assets/js/.eslintrc.yml @@ -0,0 +1,2 @@ +parserOptions: + sourceType: module diff --git a/assets/js/ga.js b/assets/js/ga.js index ae324bb..83d1b6e 100644 --- a/assets/js/ga.js +++ b/assets/js/ga.js @@ -1,5 +1,3 @@ -'use strict'; - function isDoNotTrackEnabled() { if (typeof window === 'undefined') { return false; diff --git a/assets/js/search.js b/assets/js/search.js index 484f3d9..bfb91c8 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -1,10 +1,6 @@ -'use strict'; +import {searchConfig, i18n} from '@params'; -let config, pagesIndex, searchIndex; - -// Maximum length (in words) of each text blurb. You can change this -// value if you find that 100 is too short or too long for your taste. -const MAX_SUMMARY_LENGTH = 100; +let pagesIndex, searchIndex; // Since the blurb is comprised of full sentences containing any search // term, we need a way to identify where each sentence begins/ends. This @@ -17,38 +13,9 @@ const SENTENCE_BOUNDARY_REGEX = /\b\.\s/gm; // in the blurb as it is being built. const WORD_REGEX = /\b(\w*)[\W|\s|\b]?/gm; -function buildPath(...args) { - return args.map((part, i) => { - if (i === 0) { - return part.trim().replace(/[/]*$/g, ''); - } - return part.trim().replace(/(^[/]*|[/]*$)/g, ''); - }).filter(x=>x.length).join('/'); -} - -function initConfig() { - const defaults = { - strings: { - searchEnterTerm: 'Please enter a search term.', - searchNoResults: 'No results found.' - }, - site: { - baseUrl: '/' - } - }; - - try { - const config = JSON.parse(document.querySelector('#ed-data').innerHTML); - return Object.assign({}, defaults, config); - } catch (e) { - return defaults; - } -} - async function initSearchIndex() { try { - const url = buildPath(config.site.baseUrl, 'index.json'); - const response = await fetch(url); + const response = await fetch(searchConfig.indexURI); if (response.status !== 200) return; @@ -58,6 +25,7 @@ async function initSearchIndex() { searchIndex = lunr(function () { // eslint-disable-line no-undef this.use(lunr.multiLanguage('de', 'en', 'es', 'fr', 'it', 'pt', 'ru')); // eslint-disable-line no-undef + this.field('objectID'); this.field('title'); this.field('categories'); this.field('tags'); @@ -76,14 +44,14 @@ function handleSearchQuery(event) { const query = document.getElementById('search').value.trim().toLowerCase(); if (!query) { - displayErrorMessage(config.strings.searchEnterTerm); + displayErrorMessage(i18n.enterTerm); hideSearchResults(); return; } const results = searchSite(query); if (!results.length) { - displayErrorMessage(config.strings.searchNoResults); + displayErrorMessage(i18n.noResults); hideSearchResults(); return; } @@ -220,9 +188,9 @@ function createSearchResultBlurb(query, pageContent) { if (pageBreakers.length > 0) { searchResultText = fixPageBreakers(searchResultText, pageBreakers); } - if (searchResultWords.length >= MAX_SUMMARY_LENGTH) break; + if (searchResultWords.length >= searchConfig.maxSummaryLength) break; } - return ellipsize(searchResultText, MAX_SUMMARY_LENGTH).replace( + return ellipsize(searchResultText, searchConfig.maxSummaryLength).replace( searchQueryRegex, '<span class="search-item">$&</span>' ); @@ -324,6 +292,7 @@ if (!String.prototype.matchAll) { }; } +initSearchIndex(); document.addEventListener('DOMContentLoaded', function () { const searchForm = document.getElementById('search-form'); const searchInput = document.getElementById('search'); @@ -332,9 +301,6 @@ document.addEventListener('DOMContentLoaded', function () { return; } - config = initConfig(); - initSearchIndex(); - searchForm.addEventListener('submit', (e) => { e.preventDefault(); }); |
