mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2024-11-27 01:33:04 +00:00
Format search.js
This commit is contained in:
parent
9bc3f4e416
commit
00e6ca63bc
1 changed files with 26 additions and 30 deletions
|
@ -1,9 +1,5 @@
|
|||
var lunrIndex, pagesIndex;
|
||||
|
||||
function endsWith(str, suffix) {
|
||||
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
||||
}
|
||||
|
||||
// Initialize lunrjs using our generated index file
|
||||
function initLunr() {
|
||||
// First retrieve the index file
|
||||
|
@ -13,14 +9,14 @@ function initLunr() {
|
|||
// Set up lunrjs by declaring the fields we use
|
||||
// Also provide their boost level for the ranking
|
||||
lunrIndex = lunr(function() {
|
||||
this.ref("uri");
|
||||
this.ref('uri');
|
||||
this.field('title', {
|
||||
boost: 15
|
||||
});
|
||||
this.field('tags', {
|
||||
boost: 10
|
||||
});
|
||||
this.field("content", {
|
||||
this.field('content', {
|
||||
boost: 5
|
||||
});
|
||||
|
||||
|
@ -34,8 +30,8 @@ function initLunr() {
|
|||
})
|
||||
})
|
||||
.fail(function(jqxhr, textStatus, error) {
|
||||
var err = textStatus + ", " + error;
|
||||
console.error("Error getting Hugo index file:", err);
|
||||
var err = textStatus + ', ' + error;
|
||||
console.error('Error getting Hugo index file:', err);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -47,7 +43,7 @@ function initLunr() {
|
|||
*/
|
||||
function search(queryTerm) {
|
||||
// Find the item in our index corresponding to the lunr one to have more info
|
||||
var searchTerm = queryTerm.match(/\w+/g).map(word => word+"^100"+" "+word+"*^10"+" "+"*"+word+"^10"+" "+word+"~2^1").join(" ");
|
||||
var searchTerm = queryTerm.match(/\w+/g).map(word => word + '^100' + ' ' + word + '*^10' + ' ' + '*' + word + '^10' + ' ' + word + '~2^1').join(' ');
|
||||
return lunrIndex.search(searchTerm).map(function(result) {
|
||||
return pagesIndex.filter(function(page) {
|
||||
return page.uri === result.ref;
|
||||
|
@ -57,10 +53,10 @@ function search(queryTerm) {
|
|||
|
||||
// Let's get started
|
||||
initLunr();
|
||||
$( document ).ready(function() {
|
||||
$(function() {
|
||||
var searchList = new autoComplete({
|
||||
/* selector for the search box element */
|
||||
selector: $("#search-by").get(0),
|
||||
selector: $('#search-by').get(0),
|
||||
/* source is the callback to perform the search */
|
||||
source: function(term, response) {
|
||||
response(search(term));
|
||||
|
@ -69,18 +65,18 @@ $( document ).ready(function() {
|
|||
renderItem: function(item, term) {
|
||||
var numContextWords = 2;
|
||||
var text = item.content.match(
|
||||
"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}" +
|
||||
term.trim()+"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}");
|
||||
'(?:\\s?(?:[\\w]+)\\s?){0,' + numContextWords + '}' +
|
||||
term.trim() + '(?:\\s?(?:[\\w]+)\\s?){0,' + numContextWords + '}');
|
||||
item.context = text;
|
||||
var divcontext = document.createElement("div");
|
||||
divcontext.className = "context";
|
||||
var divcontext = document.createElement('div');
|
||||
divcontext.className = 'context';
|
||||
divcontext.innerText = (item.context || '');
|
||||
var divsuggestion = document.createElement("div");
|
||||
divsuggestion.className = "autocomplete-suggestion";
|
||||
divsuggestion.setAttribute("data-term", term);
|
||||
divsuggestion.setAttribute("data-title", item.title);
|
||||
divsuggestion.setAttribute("data-uri", baseUri + item.uri);
|
||||
divsuggestion.setAttribute("data-context", item.context);
|
||||
var divsuggestion = document.createElement('div');
|
||||
divsuggestion.className = 'autocomplete-suggestion';
|
||||
divsuggestion.setAttribute('data-term', term);
|
||||
divsuggestion.setAttribute('data-title', item.title);
|
||||
divsuggestion.setAttribute('data-uri', baseUri + item.uri);
|
||||
divsuggestion.setAttribute('data-context', item.context);
|
||||
divsuggestion.innerText = '» ' + item.title;
|
||||
divsuggestion.appendChild(divcontext);
|
||||
return divsuggestion.outerHTML;
|
||||
|
@ -93,6 +89,6 @@ $( document ).ready(function() {
|
|||
|
||||
// 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
|
||||
var selector = $("#search-by").get(0);
|
||||
var selector = $('#search-by').get(0);
|
||||
$(selector).focus(selector.focusHandler);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue