From fb0f3ff8c8e5a2e3b906dd9a8815a2d22ca9c38c Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Mon, 7 Nov 2016 15:36:20 +0100 Subject: Fix #1674: add search to doc site --- docs/js/search.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 docs/js/search.js (limited to 'docs/js/search.js') diff --git a/docs/js/search.js b/docs/js/search.js new file mode 100644 index 000000000..6127f55c7 --- /dev/null +++ b/docs/js/search.js @@ -0,0 +1,57 @@ +(function() { + function displaySearchResults(results, store) { + var searchResults = document.getElementById('search-results'); + + if (results.length) { // Are there any results? + var appendString = ''; + + for (var i = 0; i < results.length; i++) { // Iterate over the results + var item = store[results[i].ref]; + appendString += '
  • ' + item.title + '

    '; + appendString += '

    ' + item.content.substring(0, 100) + '...

  • '; + } + + searchResults.innerHTML = appendString; + } else { + searchResults.innerHTML = '
  • No results found
  • '; + } + } + + 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('query'); + + if (searchTerm) { + document.getElementById('search-box').setAttribute("value", searchTerm); + var idx = lunr(function () { + this.field('id'); + this.field('title', { boost: 10 }); // hits in title get a boost + this.field('author'); + this.field('content'); + }); + + for (var key in window.store) { // Add the data to lunr + var author = (typeof window.store[key].author === 'undefined') ? "" : window.store[key].author; + idx.add({ + 'id': key, + 'title': window.store[key].title, + 'author': author, + 'content': window.store[key].content + }); + + var results = idx.search(searchTerm); // Get lunr to perform a search + displaySearchResults(results, window.store); // We'll write this in the next section + } + } +})(); -- cgit v1.2.3