search.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. summaryInclude=60;
  2. var fuseOptions = {
  3. shouldSort: true,
  4. includeMatches: true,
  5. threshold: 0.0,
  6. tokenize:true,
  7. location: 0,
  8. distance: 100,
  9. maxPatternLength: 32,
  10. minMatchCharLength: 1,
  11. keys: [
  12. {name:"title",weight:0.8},
  13. {name:"hero",weight:0.7},
  14. {name:"summary",weight:0.6},
  15. {name:"date",weight:0.5},
  16. {name:"contents",weight:0.5},
  17. {name:"tags",weight:0.3},
  18. {name:"categories",weight:0.3}
  19. ]
  20. };
  21. var searchQuery = param("keyword");
  22. if(searchQuery){
  23. $("#search-query").val(searchQuery);
  24. executeSearch(searchQuery);
  25. }else {
  26. $('#search-results').append("<p>Please enter a word or phrase above</p>");
  27. }
  28. function executeSearch(searchQuery){
  29. $.getJSON( window.location.href.split("/search/")[0] + "/index.json", function( data ) {
  30. var pages = data;
  31. var fuse = new Fuse(pages, fuseOptions);
  32. var result = fuse.search(searchQuery);
  33. // console.log({"matches":result});
  34. document.getElementById("search-box").value = searchQuery
  35. if(result.length > 0){
  36. populateResults(result);
  37. }else{
  38. $('#search-results').append("<p>No matches found</p>");
  39. }
  40. });
  41. }
  42. function populateResults(result){
  43. $.each(result,function(key,value){
  44. var contents= value.item.contents;
  45. var snippet = "";
  46. var snippetHighlights=[];
  47. var tags =[];
  48. if( fuseOptions.tokenize ){
  49. snippetHighlights.push(searchQuery);
  50. }else{
  51. $.each(value.matches,function(matchKey,mvalue){
  52. if(mvalue.key == "tags" || mvalue.key == "categories" ){
  53. snippetHighlights.push(mvalue.value);
  54. }else if(mvalue.key == "contents"){
  55. start = mvalue.indices[0][0]-summaryInclude>0?mvalue.indices[0][0]-summaryInclude:0;
  56. end = mvalue.indices[0][1]+summaryInclude<contents.length?mvalue.indices[0][1]+summaryInclude:contents.length;
  57. snippet += contents.substring(start,end);
  58. snippetHighlights.push(mvalue.value.substring(mvalue.indices[0][0],mvalue.indices[0][1]-mvalue.indices[0][0]+1));
  59. }
  60. });
  61. }
  62. if(snippet.length<1){
  63. snippet += contents.substring(0,summaryInclude*2);
  64. }
  65. //pull template from hugo templarte definition
  66. var templateDefinition = $('#search-result-template').html();
  67. //replace values
  68. var output = render(templateDefinition,{key:key,title:value.item.title,hero:value.item.hero,date:value.item.date,summary:value.item.summary,link:value.item.permalink,tags:value.item.tags,categories:value.item.categories,snippet:snippet});
  69. $('#search-results').append(output);
  70. $.each(snippetHighlights,function(snipkey,snipvalue){
  71. $("#summary-"+key).mark(snipvalue);
  72. });
  73. });
  74. }
  75. function param(name) {
  76. return decodeURIComponent((location.search.split(name + '=')[1] || '').split('&')[0]).replace(/\+/g, ' ');
  77. }
  78. function render(templateString, data) {
  79. var conditionalMatches,conditionalPattern,copy;
  80. conditionalPattern = /\$\{\s*isset ([a-zA-Z]*) \s*\}(.*)\$\{\s*end\s*}/g;
  81. //since loop below depends on re.lastInxdex, we use a copy to capture any manipulations whilst inside the loop
  82. copy = templateString;
  83. while ((conditionalMatches = conditionalPattern.exec(templateString)) !== null) {
  84. if(data[conditionalMatches[1]]){
  85. //valid key, remove conditionals, leave contents.
  86. copy = copy.replace(conditionalMatches[0],conditionalMatches[2]);
  87. }else{
  88. //not valid, remove entire section
  89. copy = copy.replace(conditionalMatches[0],'');
  90. }
  91. }
  92. templateString = copy;
  93. //now any conditionals removed we can do simple substitution
  94. var key, find, re;
  95. for (key in data) {
  96. find = '\\$\\{\\s*' + key + '\\s*\\}';
  97. re = new RegExp(find, 'g');
  98. templateString = templateString.replace(re, data[key]);
  99. }
  100. return templateString;
  101. }