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

View file

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

6
package-lock.json generated
View file

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

View file

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