Sha256: e396ac60bfcebc62688376c76126f8baf27c482d85405cf9ff56ba5cde86fa32

Contents?: true

Size: 1.49 KB

Versions: 11

Compression:

Stored size: 1.49 KB

Contents

// Yoinked in its entirety from https://davidwalsh.name/adding-search-to-your-site-with-javascript

var searchIndex = lunr(function() {
   // define searchable fields for each page
   this.ref("id");
   this.field("title", { boost: 10 });
   this.field("content");
   this.field("portal");
   this.field("categories");
   for (var key in window.pages) {
      this.add({
         "id": key,
         "title": pages[key].title,
         "content": pages[key].content
      });
   }
});



function getQueryVariable(variable) {
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] === variable) {
         return decodeURIComponent(pair[1].replace(/\+/g, "%20"));
      }
   }
}

var searchTerm = getQueryVariable("q");
// creation of searchIndex from earlier example
var results = searchIndex.search(searchTerm);
var resultPages = results.map(function (match) {
   return pages[match.ref];
});




// resultPages from previous example
resultsString = "";
resultPages.forEach(function (r) {
   resultsString += "<dt class='search_result'>";
   resultsString += "<a href='" + r.url + "'>" + r.title + "</a>";
   resultsString += "<span class='search_result-details'>" + r.portal + " ⋅ " + r.categories + "</span></dt>"
   resultsString += "<dd class='search_result-preview'>" + r.content.substring(0, 200) + "..." + "</dd>";
});
document.querySelector("#search-results").innerHTML = resultsString;

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
jekyll-theme-paperwiki-0.1.16 assets/js/search.js
jekyll-theme-paperwiki-0.1.15 assets/js/search.js
jekyll-theme-paperwiki-0.1.14 assets/js/search.js
jekyll-theme-paperwiki-0.1.13 assets/js/search.js
jekyll-theme-paperwiki-0.1.11 assets/js/search.js
jekyll-theme-paperwiki-0.1.10 assets/js/search.js
jekyll-theme-paperwiki-0.1.9 assets/js/search.js
jekyll-theme-paperwiki-0.1.8 assets/js/search.js
jekyll-theme-paperwiki-0.1.7 assets/js/search.js
jekyll-theme-paperwiki-0.1.6 assets/js/search.js
jekyll-theme-paperwiki-0.1.5 assets/js/search.js