diff --git a/themes/hugo-xmin/layouts/_default/search.html b/themes/hugo-xmin/layouts/_default/search.html index d7e7c22..f7981e8 100644 --- a/themes/hugo-xmin/layouts/_default/search.html +++ b/themes/hugo-xmin/layouts/_default/search.html @@ -9,21 +9,17 @@
Please enter a word or phrase above
"; } function executeSearch(searchQuery) { - - show(document.querySelector('.search-loading')); - - fetch('/index.json').then(function (response) { - if (response.status !== 200) { - console.log('Looks like there was a problem. Status Code: ' + response.status); - return; - } - // Examine the text in the response - response.json().then(function (pages) { - var fuse = new Fuse(pages, fuseOptions); - var result = fuse.search(searchQuery); - if (result.length > 0) { - populateResults(result); - } else { - document.getElementById('search-results').innerHTML = 'No matches found
'; - } - hide(document.querySelector('.search-loading')); - }) - .catch(function (err) { - console.log('Fetch Error :-S', err); - }); - }); -} - -function populateResults(results) { - - var searchQuery = document.getElementById("search-query").value; - var searchResults = document.getElementById("search-results"); - - // pull template from hugo template definition - var templateDefinition = document.getElementById("search-result-template").innerHTML; - - results.forEach(function (value, key) { - - var contents = value.item.contents; - var snippet = ""; - var snippetHighlights = []; - - snippetHighlights.push(searchQuery); - snippet = contents.substring(0, summaryInclude * 2) + '…'; - - //replace values - var tags = "" - if (value.item.tags) { - value.item.tags.forEach(function (element) { - tags = tags + "" + "#" + element + " " - }); - } - - var output = render(templateDefinition, { - key: key, - title: value.item.title, - link: value.item.permalink, - tags: tags, - categories: value.item.categories, - snippet: snippet - }); - searchResults.innerHTML += output; - - snippetHighlights.forEach(function (snipvalue, snipkey) { - var instance = new Mark(document.getElementById('summary-' + key)); - instance.mark(snipvalue); - }); - - }); -} - -function render(templateString, data) { - var conditionalMatches, conditionalPattern, copy; - conditionalPattern = /\$\{\s*isset ([a-zA-Z]*) \s*\}(.*)\$\{\s*end\s*}/g; - //since loop below depends on re.lastInxdex, we use a copy to capture any manipulations whilst inside the loop - copy = templateString; - while ((conditionalMatches = conditionalPattern.exec(templateString)) !== null) { - if (data[conditionalMatches[1]]) { - //valid key, remove conditionals, leave contents. - copy = copy.replace(conditionalMatches[0], conditionalMatches[2]); - } else { - //not valid, remove entire section - copy = copy.replace(conditionalMatches[0], ''); - } + // Look for "index.json" in the same directory where this script is called. + fetch("/index.json"). + then(function (response) { + return response.json() + }). + then(function (data) { + let fuse = new Fuse(data, fuseOptions); + let result = fuse.search(searchQuery); + if (result.length > 0) { + populateResults(result); + } else { + document.getElementById('search-results').innerHTML = "No matches found
"; } - templateString = copy; - //now any conditionals removed we can do simple substitution - var key, find, re; - for (key in data) { - find = '\\$\\{\\s*' + key + '\\s*\\}'; - re = new RegExp(find, 'g'); - templateString = templateString.replace(re, data[key]); - } - return templateString; + }); } -// Helper Functions -function show(elem) { - elem.style.display = 'block'; -} -function hide(elem) { - elem.style.display = 'none'; -} -function param(name) { - return decodeURIComponent((location.search.split(name + '=')[1] || '').split('&')[0]).replace(/\+/g, ' '); +function populateResults(result){ + result.forEach( function (value, key) { + let contents= value.item.contents; + let snippet = ""; + let snippetHighlights=[]; + snippetHighlights.push(searchQuery); + if(snippet.length<1){ + var getSentenceByWordRegex = new RegExp( + `[^.?!]*(?<=[.?\\s!])${searchQuery}(?=[\\s.?!])[^.?!]*[.?!]`, + 'i' + ); + var maxTextLength = summaryInclude*2 + // Index of the matched search term + var indexOfMatch = contents.toLowerCase().indexOf( + searchQuery.toLowerCase() + ); + // Index of the first word of the sentence with the search term in it + var indexOfSentence = contents.indexOf( + getSentenceByWordRegex.exec(contents) + ); + + var start + var cutStart = false + // Is the match in the result? + if(indexOfSentence+maxTextLength < indexOfMatch){ + // Make sure that the match is in the result + start = indexOfMatch + // This bool is used to replace the first part with '...' + cutStart = true + } else { + // Match is in view, even if we show the whole sentence + start = indexOfSentence + } + + // Change end length to the text length if it is longer than + // the text length to prevent problems + var end = start + maxTextLength + if (end > contents.length){ + end = contents.length + } + + if(cutStart){ + // Replace first three characters with '...' + end -= 3; + snippet += "…" + contents.substring(start, end).trim(); + } + else{ + snippet += contents.substring(start, end).trim(); + } + } + snippet += "…"; + + // Lifted from https://stackoverflow.com/posts/3700369/revisions + var elem = document.createElement('textarea'); + elem.innerHTML = snippet; + var decoded = elem.value; + + // Pull template from hugo template definition + let frag = document.getElementById('search-result-template').content.cloneNode(true); + // Replace values + frag.querySelector(".search_summary").setAttribute("id", "summary-" + key); + frag.querySelector(".search_link").setAttribute("href", value.item.permalink); + frag.querySelector(".search_title").textContent = value.item.title; + frag.querySelector(".search_snippet").textContent = decoded; + let tags = value.item.tags; + if (tags) { + frag.querySelector(".search_tags").textContent = tags; + } else { + frag.querySelector(".search_iftags").remove(); + } + snippetHighlights.forEach( function (snipvalue, snipkey) { + let markjs = new Mark(frag); + markjs.mark(snipvalue); + }); + document.getElementById("search-results").appendChild(frag); + }); } +// @license-end