Add search support
Only supports elasticlunr_json
This commit is contained in:
@ -46,7 +46,12 @@
|
||||
{%- include "partials/copy_button.html" %}
|
||||
{%- endif %}
|
||||
|
||||
{%- set scripts = [] %}
|
||||
{%- if config.build_search_index %}
|
||||
{%- include "partials/search.html" %}
|
||||
{%- set scripts = ["elasticlunr.min.js"] %}
|
||||
{%- else %}
|
||||
{%- set scripts = [""] %}
|
||||
{%- endif %}
|
||||
|
||||
{%- if config.extra.scripts %}
|
||||
{%- set scripts = config.extra.scripts %}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{#- Based on https://github.com/welpo/tabi/blob/main/templates/partials/language_switcher.html -#}
|
||||
|
||||
<li id="dropdown">
|
||||
<li id="language-switcher">
|
||||
<details>
|
||||
<summary>
|
||||
<i class="icon"></i>
|
||||
|
@ -3,9 +3,13 @@
|
||||
<a href="#main" id="main-content" tabindex="0">
|
||||
{{ macros_translate::translate(key="skip_to_content", default="Skip to main content", language_strings=language_strings) }}
|
||||
</a>
|
||||
<div id="search-container">
|
||||
<input id="search-bar" type="search" placeholder="{{ macros_translate::translate(key='search_for', default='Search for…', language_strings=language_strings) }}">
|
||||
<div id="search-results"></div>
|
||||
</div>
|
||||
<ul>
|
||||
<li id="home">
|
||||
<a href="{{ get_url(path="/", lang=lang) }}"
|
||||
<a href="{{ get_url(path='/', lang=lang) }}"
|
||||
{%- if current_url | default(value="/") | trim_end_matches(pat="/") | safe == get_url(path="/", lang=lang) | trim_end_matches(pat='/') | safe -%}
|
||||
class="active"
|
||||
{%- endif -%}>
|
||||
@ -33,6 +37,16 @@
|
||||
</li>
|
||||
{%- endif %}
|
||||
{%- endfor -%}
|
||||
{%- if config.build_search_index %}
|
||||
<li id="search">
|
||||
<a onclick="toggleSearch()" onkeypress="toggleSearch()" tabindex="0">
|
||||
<i class="icon"></i>
|
||||
<span class="hidden">
|
||||
{{ macros_translate::translate(key="search", default="Search", language_strings=language_strings) }}
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
{%- endif %}
|
||||
{%- if config.languages | length > 0 %}
|
||||
{%- include "partials/language_switcher.html" %}
|
||||
{%- endif -%}
|
||||
|
204
templates/partials/search.html
Normal file
204
templates/partials/search.html
Normal file
@ -0,0 +1,204 @@
|
||||
{#- Based on https://github.com/getzola/zola/blob/master/docs/static/search.js -#}
|
||||
|
||||
<script>
|
||||
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;
|
||||
var weighted = []; // contains elements of ["word", weight, index_in_document]
|
||||
|
||||
// split in sentences, then words
|
||||
var sentences = body.toLowerCase().split(". ");
|
||||
|
||||
for (var i in sentences) {
|
||||
var words = sentences[i].split(" ");
|
||||
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) {
|
||||
teaser.push("<b>");
|
||||
}
|
||||
startIndex = word[2] + word[0].length;
|
||||
teaser.push(body.substring(word[2], startIndex));
|
||||
|
||||
if (word[1] === TERM_WEIGHT) {
|
||||
teaser.push("</b>");
|
||||
}
|
||||
}
|
||||
teaser.push("…");
|
||||
return teaser.join("");
|
||||
}
|
||||
|
||||
function formatSearchResultItem(item, terms) {
|
||||
return '<div class="item">'
|
||||
+ `<a href="${item.ref}">${item.doc.title}</a>`
|
||||
+ `<div>${makeTeaser(item.doc.body, terms)}</div>`
|
||||
+ '</div>';
|
||||
}
|
||||
|
||||
function initSearch() {
|
||||
var $searchInput = document.getElementById("search-bar");
|
||||
var $searchResults = document.getElementById("search-results");
|
||||
var MAX_ITEMS = 10;
|
||||
|
||||
var options = {
|
||||
bool: "AND",
|
||||
fields: {
|
||||
title: { boost: 2 },
|
||||
body: { boost: 1 },
|
||||
}
|
||||
};
|
||||
var currentTerm = "";
|
||||
var index;
|
||||
|
||||
var initIndex = async function () {
|
||||
if (index === undefined) {
|
||||
index = fetch("{{ get_url(path='/', lang=lang) }}/search_index.{{ config.default_language }}.json")
|
||||
.then(
|
||||
async function (response) {
|
||||
return await elasticlunr.Index.load(await response.json());
|
||||
}
|
||||
);
|
||||
}
|
||||
let res = await index;
|
||||
return res;
|
||||
}
|
||||
|
||||
$searchInput.addEventListener("keyup", debounce(async function () {
|
||||
var term = $searchInput.value.trim();
|
||||
if (term === currentTerm) {
|
||||
return;
|
||||
}
|
||||
$searchResults.style.display = term === "" ? "none" : "flex";
|
||||
$searchResults.innerHTML = "";
|
||||
currentTerm = term;
|
||||
if (term === "") {
|
||||
return;
|
||||
}
|
||||
|
||||
var results = (await initIndex()).search(term, options);
|
||||
if (results.length === 0) {
|
||||
$searchResults.style.display = "none";
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
|
||||
$searchResults.innerHTML += formatSearchResultItem(results[i], term.split(" "));
|
||||
}
|
||||
}, 150));
|
||||
|
||||
window.addEventListener('click', function (e) {
|
||||
if ($searchResults.style.display == "flex" && !$searchResults.contains(e.target)) {
|
||||
$searchResults.style.display = "none";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function toggleSearch() {
|
||||
var searchContainer = document.getElementById("search-container");
|
||||
searchContainer.classList.toggle("active");
|
||||
}
|
||||
|
||||
if (document.readyState === "complete" ||
|
||||
(document.readyState !== "loading" && !document.documentElement.doScroll)
|
||||
) {
|
||||
initSearch();
|
||||
} else {
|
||||
document.addEventListener("DOMContentLoaded", initSearch);
|
||||
}
|
||||
</script>
|
Reference in New Issue
Block a user