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;

View file

@ -224,18 +224,30 @@ gulp.task("asset-sync", function () {
return gulp
.src([
"node_modules/clipboard/dist/clipboard.min.js",
"node_modules/flexsearch/dist/flexsearch.min.js",
"node_modules/flexsearch/dist/flexsearch.compact.js",
"node_modules/mermaid/dist/mermaid.min.js",
])
.pipe(replace(/\/\/# sourceMappingURL=.+$/, ""))
.pipe(
rename(function (path) {
path.basename = path.basename.replace(/compact/, "min");
})
)
.pipe(gulp.dest(JS_BUILD));
});
gulp.task("asset-rev", function () {
return gulp
.src([CSS_BUILD + "/*.min.css", JS_BUILD + "/*.min.js"], {
base: BUILD + "/assets",
})
.src(
[
CSS_BUILD + "/*.min.css",
JS_BUILD + "/*.min.js",
JS_BUILD + "/*.compact.js",
],
{
base: BUILD + "/assets",
}
)
.pipe(rev())
.pipe(gulp.dest("static"))
.pipe(

6
package-lock.json generated
View file

@ -2633,9 +2633,9 @@
"dev": true
},
"flexsearch": {
"version": "0.6.32",
"resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.6.32.tgz",
"integrity": "sha512-EF1BWkhwoeLtbIlDbY/vDSLBen/E5l/f1Vg7iX5CDymQCamcx1vhlc3tIZxIDplPjgi0jhG37c67idFbjg+v+Q=="
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.0.tgz",
"integrity": "sha512-Ejr7vz2f66F9TQwmj2Tv6wZcFWoDvJZK9hbkrWjo1lmMYh2RYoCimBi6p39wkQyuuOS7PI5vnNepTtI5+WTtKw=="
},
"flush-write-stream": {
"version": "1.1.1",

View file

@ -15,7 +15,7 @@
"license": "MIT",
"dependencies": {
"clipboard": "2.0.8",
"flexsearch": "0.6.32",
"flexsearch": "0.7.0",
"mermaid": "8.10.2"
},
"devDependencies": {