|
| 1 | +function ready(fn) { |
| 2 | + if (document.readyState != 'loading') { |
| 3 | + fn(); |
| 4 | + } else { |
| 5 | + document.addEventListener('DOMContentLoaded', fn); |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +ready(doSearch); |
| 10 | + |
| 11 | +const summaryInclude = 60; |
| 12 | +const fuseOptions = { |
| 13 | + shouldSort: true, |
| 14 | + includeMatches: true, |
| 15 | + matchAllTokens: true, |
| 16 | + threshold: 0.0, // for parsing diacritics |
| 17 | + tokenize: true, |
| 18 | + location: 0, |
| 19 | + distance: 100, |
| 20 | + maxPatternLength: 32, |
| 21 | + minMatchCharLength: 1, |
| 22 | + keys: [{ |
| 23 | + name: "title", |
| 24 | + weight: 0.8 |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "contents", |
| 28 | + weight: 0.5 |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "tags", |
| 32 | + weight: 0.3 |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "categories", |
| 36 | + weight: 0.3 |
| 37 | + } |
| 38 | + ] |
| 39 | +}; |
| 40 | + |
| 41 | +function param(name) { |
| 42 | + return decodeURIComponent((location.search.split(name + '=')[1] || '').split('&')[0]).replace(/\+/g, ' '); |
| 43 | +} |
| 44 | + |
| 45 | +let searchQuery = param("s"); |
| 46 | + |
| 47 | +function doSearch() { |
| 48 | + if (searchQuery) { |
| 49 | + document.getElementById("search-query").value = searchQuery; |
| 50 | + executeSearch(searchQuery); |
| 51 | + } else { |
| 52 | + const para = document.createElement("P"); |
| 53 | + para.innerText = "Please enter a word or phrase above"; |
| 54 | + document.getElementById("search-results").appendChild(para); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function getJSON(url, fn) { |
| 59 | + const request = new XMLHttpRequest(); |
| 60 | + request.open('GET', url, true); |
| 61 | + request.onload = function () { |
| 62 | + if (request.status >= 200 && request.status < 400) { |
| 63 | + const data = JSON.parse(request.responseText); |
| 64 | + fn(data); |
| 65 | + } else { |
| 66 | + console.log("Target reached on " + url + " with error " + request.status); |
| 67 | + } |
| 68 | + }; |
| 69 | + request.onerror = function () { |
| 70 | + console.log("Connection error " + request.status); |
| 71 | + }; |
| 72 | + request.send(); |
| 73 | +} |
| 74 | + |
| 75 | +function executeSearch(searchQuery) { |
| 76 | + getJSON("/" + document.LANG + "/index.json", function (data) { |
| 77 | + const pages = data; |
| 78 | + const fuse = new Fuse(pages, fuseOptions); |
| 79 | + const result = fuse.search(searchQuery); |
| 80 | + console.log({ |
| 81 | + "matches": result |
| 82 | + }); |
| 83 | + document.getElementById("search-results").innerHTML = ""; |
| 84 | + if (result.length > 0) { |
| 85 | + populateResults(result); |
| 86 | + } else { |
| 87 | + const para = document.createElement("P"); |
| 88 | + para.innerText = "No matches found"; |
| 89 | + document.getElementById("search-results").appendChild(para); |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +function populateResults(result) { |
| 95 | + result.forEach(function (value, key) { |
| 96 | + const content = value.item.contents; |
| 97 | + let snippet = ""; |
| 98 | + const snippetHighlights = []; |
| 99 | + if (fuseOptions.tokenize) { |
| 100 | + snippetHighlights.push(searchQuery); |
| 101 | + value.matches.forEach(function (mvalue) { |
| 102 | + if (mvalue.key === "tags" || mvalue.key === "categories") { |
| 103 | + snippetHighlights.push(mvalue.value); |
| 104 | + } else if (mvalue.key === "contents") { |
| 105 | + const ind = content.toLowerCase().indexOf(searchQuery.toLowerCase()); |
| 106 | + const start = ind - summaryInclude > 0 ? ind - summaryInclude : 0; |
| 107 | + const end = ind + searchQuery.length + summaryInclude < content.length ? ind + searchQuery.length + summaryInclude : content.length; |
| 108 | + snippet += content.substring(start, end); |
| 109 | + if (ind > -1) { |
| 110 | + snippetHighlights.push(content.substring(ind, ind + searchQuery.length)) |
| 111 | + } else { |
| 112 | + snippetHighlights.push(mvalue.value.substring(mvalue.indices[0][0], mvalue.indices[0][1] - mvalue.indices[0][0] + 1)); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + if (snippet.length < 1) { |
| 119 | + snippet += content.substring(0, summaryInclude * 2); |
| 120 | + } |
| 121 | + //pull template from hugo templarte definition |
| 122 | + const templateDefinition = document.getElementById("search-result-template").innerHTML; |
| 123 | + //replace values |
| 124 | + const output = render(templateDefinition, { |
| 125 | + key: key, |
| 126 | + title: value.item.title, |
| 127 | + link: value.item.permalink, |
| 128 | + tags: value.item.tags, |
| 129 | + categories: value.item.categories, |
| 130 | + snippet: snippet |
| 131 | + }); |
| 132 | + document.getElementById("search-results").appendChild(htmlToElement(output)); |
| 133 | + |
| 134 | + snippetHighlights.forEach(function (snipvalue) { |
| 135 | + new Mark(document.getElementById("summary-" + key)).mark(snipvalue); |
| 136 | + }); |
| 137 | + |
| 138 | + }); |
| 139 | +} |
| 140 | + |
| 141 | +function render(templateString, data) { |
| 142 | + let conditionalMatches, copy; |
| 143 | + const conditionalPattern = /\$\{\s*isset ([a-zA-Z]*) \s*\}(.*)\$\{\s*end\s*}/g; |
| 144 | + //since loop below depends on re.lastInxdex, we use a copy to capture any manipulations whilst inside the loop |
| 145 | + copy = templateString; |
| 146 | + while ((conditionalMatches = conditionalPattern.exec(templateString)) !== null) { |
| 147 | + if (data[conditionalMatches[1]]) { |
| 148 | + //valid key, remove conditionals, leave content. |
| 149 | + copy = copy.replace(conditionalMatches[0], conditionalMatches[2]); |
| 150 | + } else { |
| 151 | + //not valid, remove entire section |
| 152 | + copy = copy.replace(conditionalMatches[0], ''); |
| 153 | + } |
| 154 | + } |
| 155 | + templateString = copy; |
| 156 | + //now any conditionals removed we can do simple substitution |
| 157 | + let key, find, re; |
| 158 | + for (key in data) { |
| 159 | + find = '\\$\\{\\s*' + key + '\\s*\\}'; |
| 160 | + re = new RegExp(find, 'g'); |
| 161 | + templateString = templateString.replace(re, data[key]); |
| 162 | + } |
| 163 | + return templateString; |
| 164 | +} |
| 165 | + |
| 166 | +/** |
| 167 | + * By Mark Amery: https://stackoverflow.com/a/35385518 |
| 168 | + * @param {String} HTML representing a single element |
| 169 | + * @return {Element} |
| 170 | + */ |
| 171 | +function htmlToElement(html) { |
| 172 | + const template = document.createElement('template'); |
| 173 | + html = html.trim(); // Never return a text node of whitespace as the result |
| 174 | + template.innerHTML = html; |
| 175 | + return template.content.firstChild; |
| 176 | +} |
0 commit comments