|
| 1 | +// https://learn.cloudcannon.com/jekyll/jekyll-search-using-lunr-js/ |
| 2 | +(function() { |
| 3 | + function displaySearchResults(results, store) { |
| 4 | + //console.log('Displaying search results...') |
| 5 | + var searchResults = document.getElementById('search-box-results'); |
| 6 | + searchResults.innerHTML = ''; // Clear old results |
| 7 | + |
| 8 | + if (results.length) { // Are there any results? |
| 9 | + var appendString = ''; |
| 10 | + |
| 11 | + for (var i = 0; i < results.length; i++) { // Iterate over the results |
| 12 | + var item = store[results[i].ref]; |
| 13 | + appendString += '<li><a href="' + item.url + '">' + item.title + '</a> - <i>' + item.content.substring(0, 150) + '...(continue reading)</i></li>'; |
| 14 | + } |
| 15 | + |
| 16 | + searchResults.innerHTML = appendString; |
| 17 | + } else { |
| 18 | + searchResults.innerHTML = '<li>No results found</li>'; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + function debounce(func, delay) { |
| 23 | + let timeout; |
| 24 | + return function (...args) { |
| 25 | + clearTimeout(timeout); |
| 26 | + timeout = setTimeout(() => func.apply(this, args), delay); |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + // Listen for input events in the search box |
| 31 | + var searchBox = document.getElementById('search-box'); |
| 32 | + searchBox.addEventListener('input', (event) => { |
| 33 | + //console.log("Handling search for: ", event.target.value); |
| 34 | + handleSearch(event.target.value); |
| 35 | + }); |
| 36 | + |
| 37 | + // Prevent form submission on Enter key press |
| 38 | + var searchForm = document.getElementById('search-form'); |
| 39 | + searchForm.addEventListener('submit', (event) => { |
| 40 | + event.preventDefault(); // Prevent the page from refreshing |
| 41 | + //console.log("Form submitted, but default action prevented."); |
| 42 | + }); |
| 43 | + |
| 44 | + // Initalize lunr with the fields it will be searching on. I've given title |
| 45 | + // a boost of 10 to indicate matches on this field are more important. |
| 46 | + var idx = lunr(function () { |
| 47 | + this.field('id'); |
| 48 | + this.field('title', { boost: 10 }); |
| 49 | + this.field('author'); |
| 50 | + this.field('tags', { boost: 5 }); |
| 51 | + this.field('content'); |
| 52 | + }); |
| 53 | + |
| 54 | + for (var key in window.store) { // Add the data to lunr |
| 55 | + idx.add({ |
| 56 | + 'id': key, |
| 57 | + 'title': window.store[key].title, |
| 58 | + 'author': window.store[key].author, |
| 59 | + 'tags': window.store[key].tags, |
| 60 | + 'content': window.store[key].content |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + var handleSearch = debounce((query) => { |
| 66 | + var searchTerm = query; |
| 67 | + //console.log("Searching for:", searchTerm); |
| 68 | + |
| 69 | + var results = idx.search(searchTerm); // Get lunr to perform a search |
| 70 | + displaySearchResults(results, window.store); // We'll write this in the next section |
| 71 | + }, 300); // 300ms delay |
| 72 | + |
| 73 | +})(); |
0 commit comments