diff options
author | Ivan Boothe <git@rootwork.org> | 2022-07-28 23:43:03 +0300 |
---|---|---|
committer | Ivan Boothe <git@rootwork.org> | 2022-07-28 23:43:03 +0300 |
commit | 5be8a40ff4156e5c1d5c7edb0fe4537c7992a35f (patch) | |
tree | 8a80abbda871840cfcdc181ba72876d7828bd95d | |
parent | 19a69960289ef5e27defd2021fd325dbafd3c5ec (diff) |
Only run highlightSearchTerms() when a query exists
Signed-off-by: Ivan Boothe <git@rootwork.org>
-rw-r--r-- | assets/js/functions.js | 9 | ||||
-rw-r--r-- | assets/js/search.js | 54 |
2 files changed, 34 insertions, 29 deletions
diff --git a/assets/js/functions.js b/assets/js/functions.js index 5c7d915..59b9e79 100644 --- a/assets/js/functions.js +++ b/assets/js/functions.js @@ -186,6 +186,15 @@ function forEach(node, callback) { node ? Array.prototype.forEach.call(node.childNodes, callback) : false; } +function findQuery(query = 'query') { + const urlParams = new URLSearchParams(window.location.search); + if(urlParams.has(query)){ + let c = urlParams.get(query); + return c; + } + return ""; +} + function wrapText(text, context, wrapper = 'mark') { let open = `<${wrapper}>`; let close = `</${wrapper}>`; diff --git a/assets/js/search.js b/assets/js/search.js index 13727a3..c3c926a 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -130,15 +130,6 @@ function initializeSearch(index) { } } - function findQuery(query = 'query') { - const urlParams = new URLSearchParams(window.location.search); - if(urlParams.has(query)){ - let c = urlParams.get(query); - return c; - } - return ""; - } - function passiveSearch() { if(searchPageElement) { const searchTerm = findQuery(); @@ -208,27 +199,32 @@ function initializeSearch(index) { } function highlightSearchTerms(search, context, wrapper = 'mark', cssClass = '') { - let container = elem(context); - let reg = new RegExp("(" + search + ")", "gi"); - - function searchInNode(parentNode, search) { - forEach(parentNode, function (node) { - if (node.nodeType === 1) { - searchInNode(node, search); - } else if ( - node.nodeType === 3 && - reg.test(node.nodeValue) - ) { - let string = node.nodeValue.replace(reg, `<${wrapper} class="${cssClass}">$1</${wrapper}>`); - let span = document.createElement("span"); - span.dataset.searched = "true"; - span.innerHTML = string; - parentNode.replaceChild(span, node); - } - }); - }; + const query = findQuery() + if(query){ + + let container = elem(context); + let reg = new RegExp("(" + search + ")", "gi"); + + function searchInNode(parentNode, search) { + forEach(parentNode, function (node) { + if (node.nodeType === 1) { + searchInNode(node, search); + } else if ( + node.nodeType === 3 && + reg.test(node.nodeValue) + ) { + let string = node.nodeValue.replace(reg, `<${wrapper} class="${cssClass}">$1</${wrapper}>`); + let span = document.createElement("span"); + span.dataset.searched = "true"; + span.innerHTML = string; + parentNode.replaceChild(span, node); + } + }); + }; - searchInNode(container, search); + searchInNode(container, search); + + } } window.addEventListener('load', function() { |