2020-04-14 10:54:50 +00:00
|
|
|
// TODO: remove
|
|
|
|
// <script type="text/javascript" src="{{ get_url(path="elasticlunr.min.js") | safe }}"></script>
|
|
|
|
// <script type="text/javascript" src="{{ get_url(path="search_index.en.js") | safe }}"></script>
|
|
|
|
// <script type="text/javascript" src="{{ get_url(path="search.js") | safe }}"></script>
|
|
|
|
|
2020-04-11 17:22:25 +00:00
|
|
|
function debounce(func, wait) {
|
|
|
|
var timeout;
|
|
|
|
|
|
|
|
return function () {
|
|
|
|
var context = this;
|
|
|
|
var args = arguments;
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
timeout = setTimeout(function () {
|
|
|
|
timeout = null;
|
|
|
|
func.apply(context, args);
|
|
|
|
}, wait);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Taken from mdbook
|
|
|
|
// The strategy is as follows:
|
|
|
|
// First, assign a value to each word in the document:
|
|
|
|
// Words that correspond to search terms (stemmer aware): 40
|
|
|
|
// Normal words: 2
|
|
|
|
// First word in a sentence: 8
|
|
|
|
// Then use a sliding window with a constant number of words and count the
|
|
|
|
// sum of the values of the words within the window. Then use the window that got the
|
|
|
|
// maximum sum. If there are multiple maximas, then get the last one.
|
|
|
|
// Enclose the terms in <b>.
|
|
|
|
function makeTeaser(body, terms) {
|
|
|
|
var TERM_WEIGHT = 40;
|
|
|
|
var NORMAL_WORD_WEIGHT = 2;
|
|
|
|
var FIRST_WORD_WEIGHT = 8;
|
|
|
|
var TEASER_MAX_WORDS = 30;
|
|
|
|
|
|
|
|
var stemmedTerms = terms.map(function (w) {
|
|
|
|
return elasticlunr.stemmer(w.toLowerCase());
|
|
|
|
});
|
|
|
|
var termFound = false;
|
|
|
|
var index = 0;
|
2020-04-14 08:18:17 +00:00
|
|
|
var weighted = []; // contains elements of ['word', weight, index_in_document]
|
2020-04-11 17:22:25 +00:00
|
|
|
|
|
|
|
// split in sentences, then words
|
2020-04-14 08:18:17 +00:00
|
|
|
var sentences = body.toLowerCase().split('. ');
|
2020-04-11 17:22:25 +00:00
|
|
|
|
|
|
|
for (var i in sentences) {
|
2020-04-14 08:18:17 +00:00
|
|
|
var words = sentences[i].split(' ');
|
2020-04-11 17:22:25 +00:00
|
|
|
var value = FIRST_WORD_WEIGHT;
|
|
|
|
|
|
|
|
for (var j in words) {
|
|
|
|
var word = words[j];
|
|
|
|
|
|
|
|
if (word.length > 0) {
|
|
|
|
for (var k in stemmedTerms) {
|
|
|
|
if (elasticlunr.stemmer(word).startsWith(stemmedTerms[k])) {
|
|
|
|
value = TERM_WEIGHT;
|
|
|
|
termFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
weighted.push([word, value, index]);
|
|
|
|
value = NORMAL_WORD_WEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
index += word.length;
|
|
|
|
index += 1; // ' ' or '.' if last word in sentence
|
|
|
|
}
|
|
|
|
|
|
|
|
index += 1; // because we split at a two-char boundary '. '
|
|
|
|
}
|
|
|
|
|
|
|
|
if (weighted.length === 0) {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
var windowWeights = [];
|
|
|
|
var windowSize = Math.min(weighted.length, TEASER_MAX_WORDS);
|
|
|
|
// We add a window with all the weights first
|
|
|
|
var curSum = 0;
|
|
|
|
for (var i = 0; i < windowSize; i++) {
|
|
|
|
curSum += weighted[i][1];
|
|
|
|
}
|
|
|
|
windowWeights.push(curSum);
|
|
|
|
|
|
|
|
for (var i = 0; i < weighted.length - windowSize; i++) {
|
|
|
|
curSum -= weighted[i][1];
|
|
|
|
curSum += weighted[i + windowSize][1];
|
|
|
|
windowWeights.push(curSum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find the term, just pick the first window
|
|
|
|
var maxSumIndex = 0;
|
|
|
|
if (termFound) {
|
|
|
|
var maxFound = 0;
|
|
|
|
// backwards
|
|
|
|
for (var i = windowWeights.length - 1; i >= 0; i--) {
|
|
|
|
if (windowWeights[i] > maxFound) {
|
|
|
|
maxFound = windowWeights[i];
|
|
|
|
maxSumIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var teaser = [];
|
|
|
|
var startIndex = weighted[maxSumIndex][2];
|
|
|
|
for (var i = maxSumIndex; i < maxSumIndex + windowSize; i++) {
|
|
|
|
var word = weighted[i];
|
|
|
|
if (startIndex < word[2]) {
|
|
|
|
// missing text from index to start of `word`
|
|
|
|
teaser.push(body.substring(startIndex, word[2]));
|
|
|
|
startIndex = word[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// add <em/> around search terms
|
|
|
|
if (word[1] === TERM_WEIGHT) {
|
2020-04-14 08:18:17 +00:00
|
|
|
teaser.push('<b>');
|
2020-04-11 17:22:25 +00:00
|
|
|
}
|
|
|
|
startIndex = word[2] + word[0].length;
|
|
|
|
teaser.push(body.substring(word[2], startIndex));
|
|
|
|
|
|
|
|
if (word[1] === TERM_WEIGHT) {
|
2020-04-14 08:18:17 +00:00
|
|
|
teaser.push('</b>');
|
2020-04-11 17:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 08:18:17 +00:00
|
|
|
teaser.push('…');
|
|
|
|
return teaser.join('');
|
2020-04-11 17:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatSearchResultItem(item, terms) {
|
2020-04-14 08:18:17 +00:00
|
|
|
return '<div class="result">'
|
|
|
|
+ `<a href="${item.ref}">${item.doc.title}</a>`
|
|
|
|
+ `<div class="summary">${makeTeaser(item.doc.body, terms)}</div>`
|
|
|
|
+ '</div>';
|
2020-04-11 17:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initSearch() {
|
2020-04-14 08:18:17 +00:00
|
|
|
var $searchComponent = document.getElementById('search');
|
|
|
|
if ($searchComponent === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var $searchInput = $searchComponent.querySelector('input');
|
|
|
|
var $searchResults = $searchComponent.querySelector('.results-container');
|
|
|
|
var $searchResultsItems = $searchComponent.querySelector('.results');
|
2020-04-11 17:22:25 +00:00
|
|
|
var MAX_ITEMS = 10;
|
|
|
|
|
|
|
|
var options = {
|
2020-04-14 10:54:50 +00:00
|
|
|
bool: 'AND',
|
2020-04-11 17:22:25 +00:00
|
|
|
fields: {
|
2020-04-14 08:18:17 +00:00
|
|
|
title: { boost: 2 },
|
|
|
|
body: { boost: 1 },
|
2020-04-11 17:22:25 +00:00
|
|
|
}
|
|
|
|
};
|
2020-04-14 08:18:17 +00:00
|
|
|
var currentTerm = '';
|
2020-04-11 17:22:25 +00:00
|
|
|
var index = elasticlunr.Index.load(window.searchIndex);
|
|
|
|
|
2020-04-14 08:18:17 +00:00
|
|
|
$searchInput.addEventListener("keyup", debounce(function () {
|
2020-04-11 17:22:25 +00:00
|
|
|
var term = $searchInput.value.trim();
|
|
|
|
if (term === currentTerm || !index) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-14 08:18:17 +00:00
|
|
|
$searchResults.style.display = term === '' ? 'none' : 'block';
|
|
|
|
$searchResultsItems.innerHTML = '';
|
|
|
|
if (term === '') {
|
2020-04-11 17:22:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var results = index.search(term, options);
|
|
|
|
if (results.length === 0) {
|
2020-04-14 08:18:17 +00:00
|
|
|
$searchResults.style.display = 'none';
|
2020-04-11 17:22:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTerm = term;
|
|
|
|
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
|
2020-04-14 08:18:17 +00:00
|
|
|
var item = document.createElement('li');
|
|
|
|
item.innerHTML = formatSearchResultItem(results[i], term.split(' '));
|
2020-04-11 17:22:25 +00:00
|
|
|
$searchResultsItems.appendChild(item);
|
|
|
|
}
|
|
|
|
}, 150));
|
|
|
|
}
|
|
|
|
|
2020-04-14 08:18:17 +00:00
|
|
|
if (document.readyState === 'complete' ||
|
|
|
|
(document.readyState !== 'loading' && !document.documentElement.doScroll)
|
2020-04-11 17:22:25 +00:00
|
|
|
) {
|
|
|
|
initSearch();
|
|
|
|
} else {
|
2020-04-14 08:18:17 +00:00
|
|
|
document.addEventListener('DOMContentLoaded', initSearch);
|
2020-04-11 17:22:25 +00:00
|
|
|
}
|