|
| 1 | +--- |
| 2 | +layout: base |
| 3 | +title: Search |
| 4 | +nav_exclude: true |
| 5 | +search_exclude: true |
| 6 | +--- |
| 7 | + |
| 8 | + <h1 id="search">Search Results</h1> |
| 9 | + |
| 10 | + <div id="search-results" class="search-results"> |
| 11 | + Enter a search query in the search box on the left. |
| 12 | + </div> |
| 13 | + |
| 14 | + <!-- We only need to load the search dependencies in this page. --> |
| 15 | + <script src="https://unpkg.com/lunr/lunr.js"></script> |
| 16 | + <script type="text/javascript"> |
| 17 | + "use strict"; |
| 18 | + |
| 19 | + // First we figure out if there is a search query and show a "searching..." animation |
| 20 | + var getQueryVariable = function(variable) { |
| 21 | + var query = window.location.search.substring(1); |
| 22 | + var vars = query.split('&'); |
| 23 | + for (var i = 0; i < vars.length; i++) { |
| 24 | + var pair = vars[i].split('='); |
| 25 | + if (pair[0] === variable) { |
| 26 | + return decodeURIComponent(pair[1].replace(/\+/g, '%20')); |
| 27 | + } |
| 28 | + } |
| 29 | + }; |
| 30 | + var searchResults = document.getElementById('search-results'); |
| 31 | + var searchQuery = getQueryVariable('q'); |
| 32 | + var dotAnimation = null; |
| 33 | + if (searchQuery) { |
| 34 | + document.getElementById('search-query').setAttribute('value', searchQuery); |
| 35 | + var dotsCount = 0; |
| 36 | + dotAnimation = setInterval(function() { |
| 37 | + dotsCount++; |
| 38 | + var dots = new Array(dotsCount % 5).join('.'); |
| 39 | + searchResults.innerHTML = '<li>Searching' + dots + '</li>'; |
| 40 | + }, 500); |
| 41 | + } |
| 42 | + |
| 43 | + // Then we perform the search on page load |
| 44 | + window.addEventListener('load', function() { |
| 45 | + var displaySearchResults = function(results, store) { |
| 46 | + clearInterval(dotAnimation); |
| 47 | + if (results.length) { |
| 48 | + var appendString = ''; |
| 49 | + for (var i = 0; i < results.length; i++) { |
| 50 | + var item = store[results[i].ref]; |
| 51 | + appendString += '<li><a href="' + item.url + '"><h3>' + item.title + '</h3></a>'; |
| 52 | + appendString += '<p>' + item.content.substring(0, 150) + '...</p></li>'; |
| 53 | + } |
| 54 | + searchResults.innerHTML = appendString; |
| 55 | + } else { |
| 56 | + searchResults.innerHTML = '<li>Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.</li>'; |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + if (searchQuery) { |
| 61 | + var idx = lunr(function() { |
| 62 | + this.field('id'); |
| 63 | + this.field('title', { boost: 10 }); |
| 64 | + this.field('author'); |
| 65 | + this.field('content'); |
| 66 | + }); |
| 67 | + $.getJSON('/search_data.json').then(function(search_data) { |
| 68 | + var idx = lunr(function() { |
| 69 | + this.field('id'); |
| 70 | + this.field('title', { boost: 10 }); |
| 71 | + this.field('author'); |
| 72 | + this.field('content'); |
| 73 | + |
| 74 | + for (var key in search_data) { |
| 75 | + this.add({ |
| 76 | + 'id': key, |
| 77 | + 'title': search_data[key].title, |
| 78 | + 'author': search_data[key].author, |
| 79 | + 'content': search_data[key].content |
| 80 | + }); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + var results = idx.search(searchQuery); |
| 85 | + displaySearchResults(results, search_data); |
| 86 | + }); |
| 87 | + } |
| 88 | + }); |
| 89 | + </script> |
0 commit comments