hugo-theme-relearn/static/js/search.js

105 lines
4 KiB
JavaScript
Raw Normal View History

var lunrIndex, pagesIndex;
// Initialize lunrjs using our generated index file
function initLunr() {
// First retrieve the index file
2021-08-24 22:36:26 +00:00
$.getJSON(index_url)
.done(function(index) {
pagesIndex = index;
// Set up lunrjs by declaring the fields we use
// Also provide their boost level for the ranking
lunrIndex = lunr(function() {
2021-08-29 15:11:11 +00:00
this.ref('index');
2019-03-19 15:33:54 +00:00
this.field('title', {
2021-08-29 15:03:04 +00:00
boost: 15
2019-03-19 15:33:54 +00:00
});
this.field('tags', {
2021-08-29 15:03:04 +00:00
boost: 10
2019-03-19 15:33:54 +00:00
});
2021-08-29 15:03:04 +00:00
this.field('content', {
boost: 5
2019-03-19 15:33:54 +00:00
});
2019-03-19 15:33:54 +00:00
this.pipeline.remove(lunr.stemmer);
this.searchPipeline.remove(lunr.stemmer);
2019-03-19 15:33:54 +00:00
// Feed lunr with each file and let lunr actually index them
2021-08-29 15:11:11 +00:00
pagesIndex.forEach(function(page, idx) {
page.index = idx;
2021-08-29 15:03:04 +00:00
this.add(page);
2019-03-19 15:33:54 +00:00
}, this);
})
})
.fail(function(jqxhr, textStatus, error) {
2021-08-29 15:03:04 +00:00
var err = textStatus + ', ' + error;
console.error('Error getting Hugo index file:', err);
});
}
/**
* Trigger a search in lunr and transform the result
*
2021-08-29 15:13:07 +00:00
* @param {String} term
* @return {Array} results
*/
2021-08-29 15:13:07 +00:00
function search(term) {
// Find the item in our index corresponding to the lunr one to have more info
2021-08-29 15:13:07 +00:00
// Remove Lunr special search characters: https://lunrjs.com/guides/searching.html
var searchTerm = lunr.tokenizer(term.replace(/[*:^~+-]/, ' ')).flatMap(token => searchPatterns(token.str)).join(' ');
return !searchTerm ? [] : lunrIndex.search(searchTerm).map(function(result) {
return { index: result.ref, matches: Object.keys(result.matchData.metadata) }
2021-08-29 15:03:04 +00:00
});
}
2021-08-29 15:13:07 +00:00
function searchPatterns(word) {
return [
word + '^100',
word + '*^10',
'*' + word + '^10',
word + '~' + Math.floor(word.length / 4) + '^1' // allow 1 in 4 letters to have a typo
];
}
// Let's get started
initLunr();
2021-08-29 15:03:04 +00:00
$(function() {
var searchList = new autoComplete({
/* selector for the search box element */
2021-08-29 15:03:04 +00:00
selector: $('#search-by').get(0),
/* source is the callback to perform the search */
source: function(term, response) {
response(search(term));
},
/* renderItem displays individual search results */
renderItem: function(item, term) {
2021-08-29 15:11:11 +00:00
var page = pagesIndex[item.index];
var numContextWords = 2;
2021-08-29 15:12:02 +00:00
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
item.matches.map(match => match.replace(/\W/g, '\\$&')).join('|') +
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
var context = page.content.match(new RegExp(contextPattern, 'i'));
2021-08-29 15:03:04 +00:00
var divcontext = document.createElement('div');
divcontext.className = 'context';
2021-08-29 15:11:11 +00:00
divcontext.innerText = (context || '');
2021-08-29 15:03:04 +00:00
var divsuggestion = document.createElement('div');
divsuggestion.className = 'autocomplete-suggestion';
divsuggestion.setAttribute('data-term', term);
2021-08-29 15:11:11 +00:00
divsuggestion.setAttribute('data-title', page.title);
divsuggestion.setAttribute('data-uri', baseUri + page.uri);
divsuggestion.setAttribute('data-context', context);
divsuggestion.innerText = '» ' + page.title;
divsuggestion.appendChild(divcontext);
return divsuggestion.outerHTML;
},
/* onSelect callback fires when a search suggestion is chosen */
onSelect: function(e, term, item) {
location.href = item.getAttribute('data-uri');
}
});
// JavaScript-autoComplete only registers the focus event when minChars is 0 which doesn't make sense, let's do it ourselves
// https://github.com/Pixabay/JavaScript-autoComplete/blob/master/auto-complete.js#L191
2021-08-29 15:03:04 +00:00
var selector = $('#search-by').get(0);
$(selector).focus(selector.focusHandler);
});