lunr-en.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var initQuery = function() {
  2. // See if we have a search box
  3. var searchInput = document.querySelector('input#lunr_search');
  4. if (searchInput === null) {
  5. return;
  6. }
  7. // Function to parse our lunr cache
  8. var idx = lunr(function () {
  9. this.field('title')
  10. this.field('excerpt')
  11. this.field('categories')
  12. this.field('tags')
  13. this.ref('id')
  14. this.pipeline.remove(lunr.trimmer)
  15. for (var item in store) {
  16. this.add({
  17. title: store[item].title,
  18. excerpt: store[item].excerpt,
  19. categories: store[item].categories,
  20. tags: store[item].tags,
  21. id: item
  22. })
  23. }
  24. });
  25. // Run search upon keyup
  26. searchInput.addEventListener('keyup', function () {
  27. var resultdiv = document.querySelector('#results');
  28. var query = document.querySelector("input#lunr_search").value.toLowerCase();
  29. var result =
  30. idx.query(function (q) {
  31. query.split(lunr.tokenizer.separator).forEach(function (term) {
  32. q.term(term, { boost: 100 })
  33. if(query.lastIndexOf(" ") != query.length-1){
  34. q.term(term, { usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING, boost: 10 })
  35. }
  36. if (term != ""){
  37. q.term(term, { usePipeline: false, editDistance: 1, boost: 1 })
  38. }
  39. })
  40. });
  41. // Empty the results div
  42. while (resultdiv.firstChild) {
  43. resultdiv.removeChild(resultdiv.firstChild);
  44. }
  45. resultdiv.insertAdjacentHTML('afterbegin', '<p class="results__found">'+result.length+' Result(s) found</p>');
  46. for (var item in result) {
  47. var ref = result[item].ref;
  48. if(store[ref].teaser){
  49. var searchitem =
  50. '<div class="list__item">'+
  51. '<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">'+
  52. '<h2 class="archive__item-title" itemprop="headline">'+
  53. '<a href="'+store[ref].url+'" rel="permalink">'+store[ref].title+'</a>'+
  54. '</h2>'+
  55. '<div class="archive__item-teaser">'+
  56. '<img src="'+store[ref].teaser+'" alt="">'+
  57. '</div>'+
  58. '<p class="archive__item-excerpt" itemprop="description">'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...</p>'+
  59. '</article>'+
  60. '</div>';
  61. }
  62. else{
  63. var searchitem =
  64. '<div class="list__item">'+
  65. '<article class="archive__item" itemscope itemtype="https://schema.org/CreativeWork">'+
  66. '<h2 class="archive__item-title" itemprop="headline">'+
  67. '<a href="'+store[ref].url+'" rel="permalink">'+store[ref].title+'</a>'+
  68. '</h2>'+
  69. '<p class="archive__item-excerpt" itemprop="description">'+store[ref].excerpt.split(" ").splice(0,20).join(" ")+'...</p>'+
  70. '</article>'+
  71. '</div>';
  72. }
  73. resultdiv.insertAdjacentHTML('beforeend', searchitem);
  74. }
  75. });
  76. };
  77. initFunction(initQuery);