|
| 1 | +var lunrIndex, pagesIndex; |
| 2 | + |
| 3 | +function endsWith(str, suffix) { |
| 4 | + return str.indexOf(suffix, str.length - suffix.length) !== -1; |
| 5 | +} |
| 6 | + |
| 7 | +// Initialize lunrjs using our generated index file |
| 8 | +function initLunr() { |
| 9 | + if (!endsWith(baseurl,"/")){ |
| 10 | + baseurl = baseurl+'/' |
| 11 | + }; |
| 12 | + |
| 13 | + // First retrieve the index file |
| 14 | + $.ajax({ |
| 15 | + url: baseurl +"search-index.json", |
| 16 | + }).done(function(index) { |
| 17 | + pagesIndex = index; |
| 18 | + |
| 19 | + // Set up lunrjs by declaring the fields we use |
| 20 | + // Also provide their boost level for the ranking |
| 21 | + lunrIndex = lunr(function() { |
| 22 | + this.ref("uri"); |
| 23 | + this.field('title', { |
| 24 | + boost: 15 |
| 25 | + }); |
| 26 | + this.field('tags', { |
| 27 | + boost: 10 |
| 28 | + }); |
| 29 | + this.field("content", { |
| 30 | + boost: 5 |
| 31 | + }); |
| 32 | + |
| 33 | + this.pipeline.remove(lunr.stemmer); |
| 34 | + this.searchPipeline.remove(lunr.stemmer); |
| 35 | + |
| 36 | + // Feed lunr with each file and let lunr actually index them |
| 37 | + pagesIndex.forEach(function(page) { |
| 38 | + this.add(page); |
| 39 | + |
| 40 | + // Set version selector URL |
| 41 | + if(page.uri == window.location.href) { |
| 42 | + |
| 43 | + let currentVersion = baseurl.includes('latest') ? 'latest' : $('#sgVersion option:selected').text().replace(' (development)',''); |
| 44 | + $('#sgVersion option:not(:selected)').each(function(index, alt) { |
| 45 | + |
| 46 | + let altVersion = alt.text.replace(' (development)',''); |
| 47 | + |
| 48 | + if(baseurl.includes('localhost')) { // If testing locally |
| 49 | + var altVersionIndex = baseurl+'search-index-'+altVersion+'.json'; |
| 50 | + } else { // If on Live site |
| 51 | + var altVersionIndex = baseurl.replace(currentVersion, altVersion) + 'search-index.json'; |
| 52 | + } |
| 53 | + |
| 54 | + $.ajax({ |
| 55 | + url: altVersionIndex, |
| 56 | + dataType: 'text' |
| 57 | + }).done(function(altVersionIndex){ |
| 58 | + altVersionIndex = JSON.parse(altVersionIndex.replace(/[\n\r\t]/g,"")) |
| 59 | + let vPage = altVersionIndex.find(p => (p.title == page.title)) |
| 60 | + |
| 61 | + if(vPage !== undefined) { |
| 62 | + $(alt).val(vPage.uri) |
| 63 | + } else { |
| 64 | + $(alt).val( baseurl.replace(currentVersion, altVersion) + '?not-found=1'); |
| 65 | + } |
| 66 | + }) |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + }, this); |
| 71 | + }) |
| 72 | + }).fail(function(jqxhr, textStatus, error) { |
| 73 | + var err = textStatus + ", " + error; |
| 74 | + console.error("Error getting Hugo index file:", err); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Trigger a search in lunr and transform the result |
| 80 | + * |
| 81 | + * @param {String} query |
| 82 | + * @return {Array} results |
| 83 | + */ |
| 84 | +function search(queryTerm) { |
| 85 | + // Find the item in our index corresponding to the lunr one to have more info |
| 86 | + return lunrIndex.search(queryTerm+"^100"+" "+queryTerm+"*^10"+" "+"*"+queryTerm+"^10"+" "+queryTerm+"~2^1").map(function(result) { |
| 87 | + return pagesIndex.filter(function(page) { |
| 88 | + return page.uri === result.ref; |
| 89 | + })[0]; |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +// Let's get started |
| 94 | +initLunr(); |
| 95 | +$( document ).ready(function() { |
| 96 | + var searchList = new autoComplete({ |
| 97 | + /* selector for the search box element */ |
| 98 | + selector: $("#search-by").get(0), |
| 99 | + /* source is the callback to perform the search */ |
| 100 | + source: function(term, response) { |
| 101 | + response(search(term)); |
| 102 | + }, |
| 103 | + /* renderItem displays individual search results */ |
| 104 | + renderItem: function(item, term) { |
| 105 | + var numContextWords = 2; |
| 106 | + var text = item.content.match( |
| 107 | + "(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}" + |
| 108 | + term+"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}"); |
| 109 | + item.context = text; |
| 110 | + return '<div class="autocomplete-suggestion" ' + |
| 111 | + 'data-term="' + term + '" ' + |
| 112 | + 'data-title="' + item.title + '" ' + |
| 113 | + 'data-uri="'+ item.uri + '" ' + |
| 114 | + 'data-context="' + item.context + '">' + |
| 115 | + '» ' + item.title + |
| 116 | + '<div class="context">' + |
| 117 | + (item.context || '') +'</div>' + |
| 118 | + '</div>'; |
| 119 | + }, |
| 120 | + /* onSelect callback fires when a search suggestion is chosen */ |
| 121 | + onSelect: function(e, term, item) { |
| 122 | + location.href = item.getAttribute('data-uri'); |
| 123 | + } |
| 124 | + }); |
| 125 | +}); |
0 commit comments