2023-08-06 01:06:32 +01:00
|
|
|
// Search functionality using FlexSearch.
|
|
|
|
|
|
|
|
// Render the search data as JSON.
|
2023-08-04 01:11:31 +01:00
|
|
|
// {{ $searchDataFile := printf "%s.search-data.json" .Language.Lang }}
|
2023-08-06 01:06:32 +01:00
|
|
|
// {{ $searchData := resources.Get "json/search-data.json" | resources.ExecuteAsTemplate $searchDataFile . }}
|
|
|
|
// {{ if hugo.IsProduction }}
|
|
|
|
// {{ $searchData := $searchData | minify | fingerprint }}
|
|
|
|
// {{ end }}
|
2023-08-27 17:32:44 +01:00
|
|
|
// {{ $noResultsFound := (T "noResultsFound") | default "No results found." }}
|
2023-08-04 01:11:31 +01:00
|
|
|
|
|
|
|
(function () {
|
2023-08-12 22:48:41 +01:00
|
|
|
const searchDataURL = '{{ $searchData.RelPermalink }}';
|
2023-08-06 01:06:32 +01:00
|
|
|
|
2023-08-06 15:23:37 +01:00
|
|
|
const inputElements = document.querySelectorAll('.search-input');
|
|
|
|
for (const el of inputElements) {
|
|
|
|
el.addEventListener('focus', init);
|
|
|
|
el.addEventListener('keyup', search);
|
|
|
|
el.addEventListener('keydown', handleKeyDown);
|
|
|
|
}
|
2023-08-06 01:06:32 +01:00
|
|
|
|
2023-08-06 15:23:37 +01:00
|
|
|
// Get the search wrapper, input, and results elements.
|
|
|
|
function getActiveSearchElement() {
|
|
|
|
const inputs = Array.from(document.querySelectorAll('.search-wrapper')).filter(el => el.clientHeight > 0);
|
|
|
|
if (inputs.length === 1) {
|
|
|
|
return {
|
|
|
|
wrapper: inputs[0],
|
|
|
|
inputElement: inputs[0].querySelector('.search-input'),
|
|
|
|
resultsElement: inputs[0].querySelector('.search-results')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2023-08-06 01:06:32 +01:00
|
|
|
|
|
|
|
const INPUTS = ['input', 'select', 'button', 'textarea']
|
|
|
|
|
|
|
|
// Focus the search input when pressing ctrl+k/cmd+k or /.
|
|
|
|
document.addEventListener('keydown', function (e) {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { inputElement } = getActiveSearchElement();
|
|
|
|
if (!inputElement) return;
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
const activeElement = document.activeElement;
|
|
|
|
const tagName = activeElement && activeElement.tagName;
|
|
|
|
if (
|
|
|
|
inputElement === activeElement ||
|
|
|
|
!tagName ||
|
|
|
|
INPUTS.includes(tagName) ||
|
|
|
|
(activeElement && activeElement.isContentEditable))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (
|
|
|
|
e.key === '/' ||
|
|
|
|
(e.key === 'k' &&
|
|
|
|
(e.metaKey /* for Mac */ || /* for non-Mac */ e.ctrlKey))
|
|
|
|
) {
|
|
|
|
e.preventDefault();
|
|
|
|
inputElement.focus();
|
|
|
|
} else if (e.key === 'Escape' && inputElement.value) {
|
|
|
|
inputElement.blur();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-06 15:23:37 +01:00
|
|
|
// Dismiss the search results when clicking outside the search box.
|
|
|
|
document.addEventListener('mousedown', function (e) {
|
|
|
|
const { inputElement, resultsElement } = getActiveSearchElement();
|
|
|
|
if (!inputElement || !resultsElement) return;
|
|
|
|
if (
|
|
|
|
e.target !== inputElement &&
|
|
|
|
e.target !== resultsElement &&
|
|
|
|
!resultsElement.contains(e.target)
|
|
|
|
) {
|
|
|
|
hideSearchResults();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
// Get the currently active result and its index.
|
|
|
|
function getActiveResult() {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { resultsElement } = getActiveSearchElement();
|
|
|
|
if (!resultsElement) return { result: undefined, index: -1 };
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
const result = resultsElement.querySelector('.active');
|
2023-08-06 15:23:37 +01:00
|
|
|
if (!result) return { result: undefined, index: -1 };
|
|
|
|
|
2023-08-12 19:53:14 +01:00
|
|
|
const index = parseInt(result.dataset.index, 10);
|
2023-08-06 15:23:37 +01:00
|
|
|
return { result, index };
|
2023-08-06 01:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the active result by index.
|
|
|
|
function setActiveResult(index) {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { resultsElement } = getActiveSearchElement();
|
|
|
|
if (!resultsElement) return;
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
const { result: activeResult } = getActiveResult();
|
|
|
|
activeResult && activeResult.classList.remove('active');
|
|
|
|
const result = resultsElement.querySelector(`[data-index="${index}"]`);
|
|
|
|
if (result) {
|
|
|
|
result.classList.add('active');
|
|
|
|
result.focus();
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
// Get the number of search results from the DOM.
|
|
|
|
function getResultsLength() {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { resultsElement } = getActiveSearchElement();
|
|
|
|
if (!resultsElement) return 0;
|
2023-08-12 19:53:14 +01:00
|
|
|
return resultsElement.dataset.count;
|
2023-08-06 01:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finish the search by hiding the results and clearing the input.
|
|
|
|
function finishSearch() {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { inputElement } = getActiveSearchElement();
|
|
|
|
if (!inputElement) return;
|
2023-08-06 01:06:32 +01:00
|
|
|
hideSearchResults();
|
|
|
|
inputElement.value = '';
|
|
|
|
inputElement.blur();
|
|
|
|
}
|
2023-08-04 01:11:31 +01:00
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
function hideSearchResults() {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { resultsElement } = getActiveSearchElement();
|
|
|
|
if (!resultsElement) return;
|
2023-08-06 01:06:32 +01:00
|
|
|
resultsElement.classList.add('hidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle keyboard events.
|
|
|
|
function handleKeyDown(e) {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { inputElement } = getActiveSearchElement();
|
|
|
|
if (!inputElement) return;
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
const resultsLength = getResultsLength();
|
|
|
|
const { result: activeResult, index: activeIndex } = getActiveResult();
|
2023-08-04 01:11:31 +01:00
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
switch (e.key) {
|
|
|
|
case 'ArrowUp':
|
|
|
|
e.preventDefault();
|
|
|
|
if (activeIndex > 0) setActiveResult(activeIndex - 1);
|
|
|
|
break;
|
|
|
|
case 'ArrowDown':
|
|
|
|
e.preventDefault();
|
|
|
|
if (activeIndex + 1 < resultsLength) setActiveResult(activeIndex + 1);
|
|
|
|
break;
|
|
|
|
case 'Enter':
|
|
|
|
e.preventDefault();
|
|
|
|
if (activeResult) {
|
|
|
|
activeResult.click();
|
|
|
|
}
|
|
|
|
finishSearch();
|
|
|
|
case 'Escape':
|
|
|
|
e.preventDefault();
|
|
|
|
hideSearchResults();
|
|
|
|
inputElement.blur();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-08-04 01:11:31 +01:00
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
// Initializes the search.
|
2023-08-06 15:23:37 +01:00
|
|
|
function init(e) {
|
|
|
|
e.target.removeEventListener('focus', init);
|
|
|
|
if (!(window.pageIndex && window.sectionIndex)) {
|
|
|
|
preloadIndex();
|
|
|
|
}
|
2023-08-06 01:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preload the search index.
|
|
|
|
async function preloadIndex() {
|
|
|
|
window.pageIndex = new FlexSearch.Document({
|
|
|
|
tokenize: 'forward',
|
|
|
|
cache: 100,
|
|
|
|
document: {
|
|
|
|
id: 'id',
|
|
|
|
store: ['title'],
|
|
|
|
index: "content"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.sectionIndex = new FlexSearch.Document({
|
|
|
|
tokenize: 'forward',
|
|
|
|
cache: 100,
|
|
|
|
document: {
|
|
|
|
id: 'id',
|
|
|
|
store: ['title', 'content', 'url', 'display'],
|
|
|
|
index: "content",
|
|
|
|
tag: 'pageId'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const resp = await fetch(searchDataURL);
|
|
|
|
const data = await resp.json();
|
|
|
|
let pageId = 0;
|
|
|
|
for (const route in data) {
|
|
|
|
let pageContent = '';
|
|
|
|
++pageId;
|
|
|
|
|
|
|
|
for (const heading in data[route].data) {
|
|
|
|
const [hash, text] = heading.split('#');
|
|
|
|
const url = route.trimEnd('/') + (hash ? '#' + hash : '');
|
|
|
|
const title = text || data[route].title;
|
2023-08-04 01:11:31 +01:00
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
const content = data[route].data[heading] || '';
|
|
|
|
const paragraphs = content.split('\n').filter(Boolean);
|
|
|
|
|
|
|
|
sectionIndex.add({
|
|
|
|
id: url,
|
|
|
|
url,
|
|
|
|
title,
|
|
|
|
pageId: `page_${pageId}`,
|
|
|
|
content: title,
|
|
|
|
...(paragraphs[0] && { display: paragraphs[0] })
|
|
|
|
});
|
|
|
|
|
|
|
|
for (let i = 0; i < paragraphs.length; i++) {
|
|
|
|
sectionIndex.add({
|
|
|
|
id: `${url}_${i}`,
|
|
|
|
url,
|
|
|
|
title,
|
|
|
|
pageId: `page_${pageId}`,
|
|
|
|
content: paragraphs[i]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pageContent += ` ${title} ${content}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.pageIndex.add({
|
|
|
|
id: pageId,
|
|
|
|
title: data[route].title,
|
|
|
|
content: pageContent
|
2023-08-04 01:11:31 +01:00
|
|
|
});
|
2023-08-06 01:06:32 +01:00
|
|
|
|
|
|
|
}
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 15:23:37 +01:00
|
|
|
function search(e) {
|
|
|
|
const query = e.target.value;
|
|
|
|
if (!e.target.value) {
|
2023-08-06 01:06:32 +01:00
|
|
|
hideSearchResults();
|
|
|
|
return;
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 15:23:37 +01:00
|
|
|
const { resultsElement } = getActiveSearchElement();
|
2023-08-06 01:06:32 +01:00
|
|
|
while (resultsElement.firstChild) {
|
|
|
|
resultsElement.removeChild(resultsElement.firstChild);
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
2023-08-06 01:06:32 +01:00
|
|
|
resultsElement.classList.remove('hidden');
|
|
|
|
|
|
|
|
const pageResults = window.pageIndex.search(query, 5, { enrich: true, suggest: true })[0]?.result || [];
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
const pageTitleMatches = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < pageResults.length; i++) {
|
|
|
|
const result = pageResults[i];
|
|
|
|
pageTitleMatches[i] = 0;
|
|
|
|
|
|
|
|
// Show the top 5 results for each page
|
|
|
|
const sectionResults = window.sectionIndex.search(query, 5, { enrich: true, suggest: true, tag: `page_${result.id}` })[0]?.result || [];
|
|
|
|
let isFirstItemOfPage = true
|
|
|
|
const occurred = {}
|
2023-08-04 01:11:31 +01:00
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
for (let j = 0; j < sectionResults.length; j++) {
|
|
|
|
const { doc } = sectionResults[j]
|
|
|
|
const isMatchingTitle = doc.display !== undefined
|
|
|
|
if (isMatchingTitle) {
|
|
|
|
pageTitleMatches[i]++
|
|
|
|
}
|
|
|
|
const { url, title } = doc
|
|
|
|
const content = doc.display || doc.content
|
|
|
|
|
|
|
|
if (occurred[url + '@' + content]) continue
|
|
|
|
occurred[url + '@' + content] = true
|
|
|
|
results.push({
|
|
|
|
_page_rk: i,
|
|
|
|
_section_rk: j,
|
|
|
|
route: url,
|
|
|
|
prefix: isFirstItemOfPage ? result.doc.title : undefined,
|
|
|
|
children: { title, content }
|
|
|
|
})
|
|
|
|
isFirstItemOfPage = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const sortedResults = results
|
|
|
|
.sort((a, b) => {
|
|
|
|
// Sort by number of matches in the title.
|
|
|
|
if (a._page_rk === b._page_rk) {
|
|
|
|
return a._section_rk - b._section_rk
|
|
|
|
}
|
|
|
|
if (pageTitleMatches[a._page_rk] !== pageTitleMatches[b._page_rk]) {
|
|
|
|
return pageTitleMatches[b._page_rk] - pageTitleMatches[a._page_rk]
|
|
|
|
}
|
|
|
|
return a._page_rk - b._page_rk
|
|
|
|
})
|
|
|
|
.map(res => ({
|
|
|
|
id: `${res._page_rk}_${res._section_rk}`,
|
|
|
|
route: res.route,
|
|
|
|
prefix: res.prefix,
|
|
|
|
children: res.children
|
|
|
|
}));
|
|
|
|
displayResults(sortedResults, query);
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
function displayResults(results, query) {
|
2023-08-06 15:23:37 +01:00
|
|
|
const { resultsElement } = getActiveSearchElement();
|
|
|
|
if (!resultsElement) return;
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
if (!results.length) {
|
2023-08-27 17:32:44 +01:00
|
|
|
resultsElement.innerHTML = `<span class="no-result">{{ $noResultsFound | safeHTML }}</span>`;
|
2023-08-06 01:06:32 +01:00
|
|
|
return;
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
2023-08-06 01:06:32 +01:00
|
|
|
|
2023-08-12 19:53:14 +01:00
|
|
|
// Highlight the query in the result text.
|
2023-08-06 01:06:32 +01:00
|
|
|
function highlightMatches(text, query) {
|
|
|
|
const escapedQuery = query.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
|
|
const regex = new RegExp(escapedQuery, 'gi');
|
|
|
|
return text.replace(regex, (match) => `<span class="match">${match}</span>`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a DOM element from the HTML string.
|
|
|
|
function createElement(str) {
|
2023-08-04 01:11:31 +01:00
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = str.trim();
|
|
|
|
return div.firstChild;
|
|
|
|
}
|
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
function handleMouseMove(e) {
|
|
|
|
const target = e.target.closest('a');
|
|
|
|
if (target) {
|
|
|
|
const active = resultsElement.querySelector('a.active');
|
|
|
|
if (active) {
|
|
|
|
active.classList.remove('active');
|
|
|
|
}
|
|
|
|
target.classList.add('active');
|
|
|
|
}
|
|
|
|
}
|
2023-08-04 01:11:31 +01:00
|
|
|
|
2023-08-06 01:06:32 +01:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
|
const result = results[i];
|
|
|
|
if (result.prefix) {
|
|
|
|
fragment.appendChild(createElement(`
|
|
|
|
<div class="prefix">${result.prefix}</div>`));
|
|
|
|
}
|
|
|
|
let li = createElement(`
|
|
|
|
<li>
|
|
|
|
<a data-index="${i}" href="${result.route}" class=${i === 0 ? "active" : ""}>
|
|
|
|
<div class="title">`+ highlightMatches(result.children.title, query) + `</div>` +
|
|
|
|
(result.children.content ?
|
|
|
|
`<div class="excerpt">` + highlightMatches(result.children.content, query) + `</div>` : '') + `
|
|
|
|
</a>
|
|
|
|
</li>`);
|
|
|
|
li.addEventListener('mousemove', handleMouseMove);
|
|
|
|
li.addEventListener('keydown', handleKeyDown);
|
|
|
|
li.querySelector('a').addEventListener('click', finishSearch);
|
2023-08-04 01:11:31 +01:00
|
|
|
fragment.appendChild(li);
|
|
|
|
}
|
2023-08-06 01:06:32 +01:00
|
|
|
resultsElement.appendChild(fragment);
|
2023-08-12 19:53:14 +01:00
|
|
|
resultsElement.dataset.count = results.length;
|
2023-08-04 01:11:31 +01:00
|
|
|
}
|
|
|
|
})();
|