From b90fbbedd28bfb4f8cfa1fe80e1200c73f8a81aa Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 23 Dec 2020 16:43:41 +0100 Subject: [PATCH] fix search issues --- CHANGELOG.md | 9 +++------ assets/js/search.js | 38 +++++++++++++++++++++----------------- src/sass/_base.scss | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb4f4a9..8dd79bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,3 @@ -- ENHANCEMENT - - Rework search as a dropdown list #38 (@LukasHirt) - - Use option `geekdocSearchShowParent` to group search results by parent #38 (@LukasHirt). - This is changing the current behavior of prefixing search results with the parent by - grouped nested lists. - - Add `ok` color option for `hint` shortcode +- BUGFIX + - fix empty search list after pae refresh + - fix search issue if `geekdocSearchShowParent` is disabled diff --git a/assets/js/search.js b/assets/js/search.js index 10a5cdc..9f0de8b 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -8,7 +8,7 @@ const results = document.querySelector('#gdoc-search-results'); let showParent = false - {{ if .Site.Params.GeekdocSearchShowParent }} + {{ if .Site.Params.GeekdocSearchShowParent }} showParent = true {{ end }} @@ -19,12 +19,12 @@ input.removeEventListener('focus', init); // init once input.required = true; + loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}'); loadScript('{{ index .Site.Data.assets "js/flexsearch.min.js" | relURL }}'); loadScript('{{ $searchData.RelPermalink }}', function() { input.required = false; search(); }); - loadScript('{{ index .Site.Data.assets "js/groupBy.min.js" | relURL }}'); } function search() { @@ -33,14 +33,10 @@ } if (!input.value) { - console.log("empty") - results.classList.remove("has-hits"); - return; + return results.classList.remove("has-hits"); } let searchHits = window.geekdocSearchIndex.search(input.value, 10); - - console.log(searchHits.length); if (searchHits.length < 1) { return results.classList.remove("has-hits"); } @@ -53,16 +49,24 @@ const items = []; - for (const section in searchHits) { - const item = document.createElement('li'); + if (showParent) { + for (const section in searchHits) { + const item = document.createElement('li'), + title = item.appendChild(document.createElement('span')), + subList = item.appendChild(document.createElement('ul')); - if (showParent) { - const title = item.appendChild(document.createElement('span')); title.textContent = section; - } + createLinks(searchHits[section], subList); - const subList = item.appendChild(document.createElement('ul')); - createLinks(searchHits[section], subList); + items.push(item); + } + } else { + const item = document.createElement('li'), + title = item.appendChild(document.createElement('span')), + subList = item.appendChild(document.createElement('ul')); + + title.textContent = "Results"; + createLinks(searchHits, subList); items.push(item); } @@ -104,12 +108,12 @@ } function loadScript(src, callback) { - const script = document.createElement('script'); + let script = document.createElement('script'); script.defer = true; - script.async = false; + script.async = true; script.src = src; script.onload = callback; - document.head.appendChild(script); + document.body.appendChild(script); } })(); diff --git a/src/sass/_base.scss b/src/sass/_base.scss index 82417e1..e1fe7ab 100644 --- a/src/sass/_base.scss +++ b/src/sass/_base.scss @@ -463,7 +463,7 @@ img { > li > span { font-weight: bold; - color: $gray-600; + color: $gray-700; } > li + li {