search.js 3.3 KB

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