2020-12-14 23:54:59 +00:00
|
|
|
var lunrIndex;
|
2020-11-03 23:02:47 +00:00
|
|
|
|
|
|
|
// Initialize lunrjs using our generated index file
|
|
|
|
function initLunr() {
|
|
|
|
var request = new XMLHttpRequest();
|
2020-12-14 23:57:48 +00:00
|
|
|
request.open('GET', 'https://darktable-org.github.io/luadocs/index.json', true);
|
2020-11-03 23:02:47 +00:00
|
|
|
|
|
|
|
request.onload = function () {
|
|
|
|
if (request.status >= 200 && request.status < 400) {
|
|
|
|
|
|
|
|
pagesIndex = JSON.parse(request.responseText);
|
|
|
|
console.log("index:", pagesIndex);
|
|
|
|
|
|
|
|
// Set up lunrjs by declaring the fields we use
|
|
|
|
// Also provide their boost level for the ranking
|
|
|
|
lunrIndex = lunr(function () {
|
|
|
|
this.field("title", {
|
2020-12-14 23:54:59 +00:00
|
|
|
boost: 20
|
2020-11-03 23:02:47 +00:00
|
|
|
});
|
|
|
|
this.field("tags", {
|
|
|
|
boost: 5
|
|
|
|
});
|
|
|
|
|
|
|
|
this.field("content", {
|
2020-12-14 23:54:59 +00:00
|
|
|
boost: 1
|
2020-11-03 23:02:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// ref is the result item identifier (I chose the page URL)
|
|
|
|
this.ref("href");
|
|
|
|
this.add({ field: "test", text: 'hello' });
|
|
|
|
for (var i = 0; i < pagesIndex.length; ++i) {
|
|
|
|
this.add(pagesIndex[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var err = textStatus + ", " + error;
|
|
|
|
console.error("Error getting Hugo index flie:", err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
function search(query) {
|
|
|
|
return lunrIndex.search(query).map(function (result) {
|
|
|
|
return pagesIndex.filter(function (page) {
|
|
|
|
return page.href === result.ref;
|
|
|
|
})[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderResults(results) {
|
2020-12-14 23:54:59 +00:00
|
|
|
|
|
|
|
if (!results.length) {
|
|
|
|
$("#search-results").hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only show the first twenty results
|
|
|
|
$results = document.getElementById("search-results");
|
|
|
|
|
|
|
|
results.slice(0, 20).forEach(function (result) {
|
|
|
|
var p = document.createElement('p');
|
|
|
|
var ahref = document.createElement('a');
|
|
|
|
ahref.href = result.href;
|
|
|
|
ahref.text = "» " + result.title;
|
|
|
|
p.append(ahref);
|
|
|
|
$results.appendChild(p);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#search-results").show();
|
2020-11-03 23:02:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 23:54:59 +00:00
|
|
|
$(document).ready(function() {
|
|
|
|
$("#search-results").hide();
|
|
|
|
$("#search-input").focus(function() {
|
|
|
|
//load index file if it's not already been loaded
|
|
|
|
if (! lunrIndex) { initLunr(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#search-input").keydown(function (e) {
|
|
|
|
if(e.keyCode == 27)
|
|
|
|
{
|
|
|
|
$("#search-results").hide();
|
|
|
|
$("#search-input").val("");
|
|
|
|
}
|
2021-06-23 15:57:32 +00:00
|
|
|
if(e.keyCode ==13)
|
|
|
|
{
|
|
|
|
//enter key pressed - go to first search result if it exists
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
$hreference = $("#search-results a").first().attr('href');
|
|
|
|
|
|
|
|
if($hreference != undefined)
|
|
|
|
{
|
|
|
|
document.location.href = $hreference;
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 23:54:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#search-input").keyup(function (e) {
|
|
|
|
//trigger a search if a search term longer than two characters has been entered
|
|
|
|
$("#search-results").empty().hide();
|
|
|
|
query = $("#search-input").val();
|
|
|
|
if (query.length < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
results = search(query + '~1');
|
|
|
|
renderResults(results);
|
|
|
|
});
|
|
|
|
});
|
2020-11-03 23:02:47 +00:00
|
|
|
|