From 74e73b804d891acad84e63bdaf4efedb6ea8fd5a Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 14 Jun 2021 22:15:32 +0200 Subject: [PATCH] fix(deps): update dependency flexsearch to v0.7.0 (#146) --- assets/js/search.js | 55 ++++++++++++++++++++++++++++++++------------- gulpfile.js | 20 +++++++++++++---- package-lock.json | 6 ++--- package.json | 2 +- 4 files changed, 60 insertions(+), 23 deletions(-) diff --git a/assets/js/search.js b/assets/js/search.js index 037bf9b..b3749b6 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -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; diff --git a/gulpfile.js b/gulpfile.js index 7d209d5..05c194d 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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( diff --git a/package-lock.json b/package-lock.json index fd6f447..97b0bb9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 59d5081..e226640 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "license": "MIT", "dependencies": { "clipboard": "2.0.8", - "flexsearch": "0.6.32", + "flexsearch": "0.7.0", "mermaid": "8.10.2" }, "devDependencies": {