fix(deps): update dependency flexsearch to v0.7.0 (#146)

This commit is contained in:
renovate[bot] 2021-06-14 22:15:32 +02:00 committed by GitHub
parent 4a97a8e873
commit 74e73b804d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 23 deletions

View file

@ -18,16 +18,19 @@
loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}');
loadScript('{{ index .Site.Data.assets "js/flexsearch.min.js" | relURL }}', function() {
const indexCfg = {{ with .Scratch.Get "geekdocSearchConfig" }}{{ . | jsonify}}{{ else }}{}{{ end }};
const dataUrl = "{{ $searchData.RelPermalink }}"
const indexCfgDefaults = {
tokenize: 'forward'
}
const indexCfg = {{ with .Scratch.Get "geekdocSearchConfig" }}{{ . | jsonify }}{{ else }}indexCfgDefaults{{ end }};
const dataUrl = '{{ $searchData.RelPermalink }}'
indexCfg.doc = {
id: 'id',
field: ['title', 'content'],
indexCfg.document = {
key: 'id',
index: ['title', 'content'],
store: ['title', 'href', 'parent'],
};
const index = FlexSearch.create(indexCfg);
const index = new FlexSearch.Document(indexCfg);
window.geekdocSearchIndex = index;
getJson(dataUrl, function(data) {
@ -39,20 +42,26 @@
}
function search() {
const searchCfg = {
field: ['title', 'content'],
enrich: true,
limit: 10
};
while (results.firstChild) {
results.removeChild(results.firstChild);
}
if (!input.value) {
return results.classList.remove("has-hits");
return results.classList.remove('has-hits');
}
let searchHits = window.geekdocSearchIndex.search(input.value, 10);
let searchHits = flattenHits(window.geekdocSearchIndex.search(input.value, searchCfg));
if (searchHits.length < 1) {
return results.classList.remove("has-hits");
return results.classList.remove('has-hits');
}
results.classList.add("has-hits");
results.classList.add('has-hits');
if (showParent === true) {
searchHits = groupBy(searchHits, hit => hit.parent);
@ -76,7 +85,7 @@
title = item.appendChild(document.createElement('span')),
subList = item.appendChild(document.createElement('ul'));
title.textContent = "Results";
title.textContent = 'Results';
createLinks(searchHits, subList);
items.push(item);
@ -88,8 +97,8 @@
}
/**
* Creates links to given pages and either returns them in an array or attaches them to a target element
* @param {Object} pages Page to which the link should point to
* Creates links to given fields and either returns them in an array or attaches them to a target element
* @param {Object} fields Page to which the link should point to
* @param {HTMLElement} target Element to which the links should be attatched
* @returns {Array} If target is not specified, returns an array of built links
*/
@ -101,11 +110,11 @@
entry = item.appendChild(document.createElement("span")),
a = entry.appendChild(document.createElement("a"));
entry.classList.add("flex")
entry.classList.add('flex')
a.href = page.href;
a.textContent = page.title;
a.classList.add("gdoc-search__entry")
a.classList.add('gdoc-search__entry')
if (target) {
target.appendChild(item);
@ -135,6 +144,22 @@
});
}
function flattenHits(results) {
const items = [];
const map = new Map();
for (const field of results) {
for (const page of field.result) {
if(!map.has(page.doc.href)){
map.set(page.doc.href, true);
items.push(page.doc);
}
}
}
return items
}
function loadScript(src, callback) {
let script = document.createElement('script');
script.defer = true;