` +
`` + `${item.doc.title}` + `
` +
`` +
`${makeTeaser(item.doc.body, terms)}` +
`
` +
``
);
}
function search() {
var $searchInput = document.getElementById("search");
var $searchResults = document.querySelector(".search-results");
var $searchResultsItems = document.querySelector(".search-results__items");
var MAX_ITEMS = 10;
var options = {
bool: "AND",
fields: {
title: { boost: 2 },
body: { boost: 1 },
},
};
var currentTerm = "";
var index = elasticlunr.Index.load(window.searchIndex);
$searchInput.addEventListener(
"keyup",
debounce(function () {
var term = $searchInput.value.trim();
if (term === currentTerm || !index) {
return;
}
$searchResults.style.display = term === "" ? "none" : "block";
$searchResultsItems.innerHTML = "";
if (term === "") {
return;
}
var results = index.search(term, options);
if (results.length === 0) {
$searchResults.style.display = "none";
return;
}
currentTerm = term;
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
var item = document.createElement("div");
item.classList.add("mb-4");
item.innerHTML = formatSearchResultItem(results[i], term.split(" "));
$searchResultsItems.appendChild(item);
}
}, 150)
);
}
function openSearchModal() {
var baseUrl = window.location.protocol + "//" + window.location.host;
var indexScriptPath = baseUrl + '/search_index.en.js';
if (indexScriptLoaded === false) {
var indexScript = document.createElement('script');
indexScript.setAttribute('src', indexScriptPath);
document.head.appendChild(indexScript);
indexScriptLoaded = true;
}
document.getElementById('search-modal').classList.remove('hidden');
document.getElementById('search').focus();
document.getElementById('search').select();
}
function closeSearchModal() {
document.getElementById('search-modal').classList.add('hidden');
}
function documentReadyCallback() {
if (localStorage.getItem("theme") === "dark") {
document.body.setAttribute("theme", "dark");
document.querySelectorAll("img, picture, video, pre").forEach(img => img.setAttribute("theme", "dark"));
document.querySelectorAll(".vimeo, .youtube, .chart").forEach(video => video.setAttribute("theme", "dark"));
document.getElementById("dark-mode").setAttribute("title", "Switch to light theme");
}
document.addEventListener('click', function(ev) {
if (!ev.target.closest('#search-modal')) {
closeSearchModal();
}
});
document.addEventListener('keydown', function(event) {
if (event.altKey && event.key === 's') {
if (document.getElementById('search-modal').classList.contains('hidden')) {
openSearchModal();
} else {
closeSearchModal();
}
}
});
document.addEventListener('keyup', function(ev) {
if (ev.key === "Escape") {
closeSearchModal();
}
});
document.getElementById("search").addEventListener("keyup", () => {
search();
});
if (typeof mermaid !== "undefined") {
mermaid.initialize({ startOnLoad: true });
}
if (typeof renderMathInElement !== "undefined") {
renderMathInElement(document.body, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
]
});
}
};
if (document.readyState === 'loading') { // Loading hasn't finished yet
document.addEventListener('DOMContentLoaded', documentReadyCallback);
} else { // `DOMContentLoaded` has already fired
documentReadyCallback();
}