2016-10-21 00:08:51 +00:00
|
|
|
var lunrIndex, pagesIndex;
|
|
|
|
|
2017-07-27 19:42:07 +00:00
|
|
|
function endsWith(str, suffix) {
|
|
|
|
return str.indexOf(suffix, str.length - suffix.length) !== -1;
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:08:51 +00:00
|
|
|
// Initialize lunrjs using our generated index file
|
|
|
|
function initLunr() {
|
2017-07-27 19:42:07 +00:00
|
|
|
if (!endsWith(baseurl,"/")){
|
|
|
|
baseurl = baseurl+'/'
|
|
|
|
};
|
|
|
|
|
2016-10-21 00:08:51 +00:00
|
|
|
// First retrieve the index file
|
2017-07-27 19:42:07 +00:00
|
|
|
$.getJSON(baseurl +"index.json")
|
2016-10-21 00:08:51 +00:00
|
|
|
.done(function(index) {
|
2017-07-27 19:42:07 +00:00
|
|
|
pagesIndex = index;
|
2016-10-21 00:08:51 +00:00
|
|
|
// Set up lunrjs by declaring the fields we use
|
|
|
|
// Also provide their boost level for the ranking
|
|
|
|
lunrIndex = new lunr.Index
|
|
|
|
lunrIndex.ref("uri");
|
|
|
|
lunrIndex.field('title', {
|
|
|
|
boost: 15
|
|
|
|
});
|
|
|
|
lunrIndex.field('tags', {
|
|
|
|
boost: 10
|
|
|
|
});
|
|
|
|
lunrIndex.field("content", {
|
|
|
|
boost: 5
|
|
|
|
});
|
|
|
|
|
|
|
|
// Feed lunr with each file and let lunr actually index them
|
|
|
|
pagesIndex.forEach(function(page) {
|
|
|
|
lunrIndex.add(page);
|
|
|
|
});
|
|
|
|
lunrIndex.pipeline.remove(lunrIndex.stemmer)
|
|
|
|
})
|
|
|
|
.fail(function(jqxhr, textStatus, error) {
|
|
|
|
var err = textStatus + ", " + error;
|
|
|
|
console.error("Error getting Hugo index flie:", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a search in lunr and transform the result
|
|
|
|
*
|
|
|
|
* @param {String} query
|
|
|
|
* @return {Array} results
|
|
|
|
*/
|
|
|
|
function search(query) {
|
|
|
|
// Find the item in our index corresponding to the lunr one to have more info
|
|
|
|
return lunrIndex.search(query).map(function(result) {
|
|
|
|
return pagesIndex.filter(function(page) {
|
|
|
|
return page.uri === result.ref;
|
|
|
|
})[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's get started
|
|
|
|
initLunr();
|
|
|
|
$( document ).ready(function() {
|
2016-10-24 22:45:15 +00:00
|
|
|
var horseyList = horsey($("#search-by").get(0), {
|
2016-10-21 00:08:51 +00:00
|
|
|
suggestions: function (value, done) {
|
|
|
|
var query = $("#search-by").val();
|
|
|
|
var results = search(query);
|
|
|
|
done(results);
|
|
|
|
},
|
|
|
|
filter: function (q, suggestion) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
2017-07-27 19:42:07 +00:00
|
|
|
location.href=value.uri;
|
2016-10-21 00:08:51 +00:00
|
|
|
},
|
|
|
|
render: function (li, suggestion) {
|
|
|
|
var uri = suggestion.uri.substring(1,suggestion.uri.length);
|
|
|
|
|
2017-07-27 19:42:07 +00:00
|
|
|
suggestion.href = baseurl + uri;
|
2016-10-21 00:08:51 +00:00
|
|
|
|
|
|
|
var query = $("#search-by").val();
|
|
|
|
var numWords = 2;
|
|
|
|
var text = suggestion.content.match("(?:\\s?(?:[\\w]+)\\s?){0,"+numWords+"}"+query+"(?:\\s?(?:[\\w]+)\\s?){0,"+numWords+"}");
|
|
|
|
suggestion.context = text;
|
|
|
|
var image = '<div>' + '» ' + suggestion.title + '</div><div style="font-size:12px">' + (suggestion.context || '') +'</div>';
|
|
|
|
li.innerHTML = image;
|
|
|
|
},
|
|
|
|
limit: 10
|
|
|
|
});
|
2016-10-24 22:45:15 +00:00
|
|
|
horseyList.refreshPosition();
|
2016-10-21 00:08:51 +00:00
|
|
|
});
|