You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.4 KiB

2 years ago
  1. var lunrIndex, pagesIndex;
  2. function endsWith(str, suffix) {
  3. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  4. }
  5. // Initialize lunrjs using our generated index file
  6. function initLunr() {
  7. if (!endsWith(baseurl,"/")){
  8. baseurl = baseurl+'/'
  9. };
  10. // First retrieve the index file
  11. $.getJSON(baseurl +"index.json")
  12. .done(function(index) {
  13. pagesIndex = index;
  14. // Set up lunrjs by declaring the fields we use
  15. // Also provide their boost level for the ranking
  16. lunrIndex = lunr(function() {
  17. this.ref("uri");
  18. this.field('title', {
  19. boost: 15
  20. });
  21. this.field('tags', {
  22. boost: 10
  23. });
  24. this.field("content", {
  25. boost: 5
  26. });
  27. this.pipeline.remove(lunr.stemmer);
  28. this.searchPipeline.remove(lunr.stemmer);
  29. // Feed lunr with each file and let lunr actually index them
  30. pagesIndex.forEach(function(page) {
  31. this.add(page);
  32. }, this);
  33. })
  34. })
  35. .fail(function(jqxhr, textStatus, error) {
  36. var err = textStatus + ", " + error;
  37. console.error("Error getting Hugo index file:", err);
  38. });
  39. }
  40. /**
  41. * Trigger a search in lunr and transform the result
  42. *
  43. * @param {String} query
  44. * @return {Array} results
  45. */
  46. function search(queryTerm) {
  47. // Find the item in our index corresponding to the lunr one to have more info
  48. return lunrIndex.search(queryTerm+"^100"+" "+queryTerm+"*^10"+" "+"*"+queryTerm+"^10"+" "+queryTerm+"~2^1").map(function(result) {
  49. return pagesIndex.filter(function(page) {
  50. return page.uri === result.ref;
  51. })[0];
  52. });
  53. }
  54. // Let's get started
  55. initLunr();
  56. $( document ).ready(function() {
  57. var searchList = new autoComplete({
  58. /* selector for the search box element */
  59. selector: $("#search-by").get(0),
  60. /* source is the callback to perform the search */
  61. source: function(term, response) {
  62. response(search(term));
  63. },
  64. /* renderItem displays individual search results */
  65. renderItem: function(item, term) {
  66. var numContextWords = 2;
  67. var text = item.content.match(
  68. "(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}" +
  69. term+"(?:\\s?(?:[\\w]+)\\s?){0,"+numContextWords+"}");
  70. item.context = text;
  71. var divcontext = document.createElement("div");
  72. divcontext.className = "context";
  73. divcontext.innerText = (item.context || '');
  74. var divsuggestion = document.createElement("div");
  75. divsuggestion.className = "autocomplete-suggestion";
  76. divsuggestion.setAttribute("data-term", term);
  77. divsuggestion.setAttribute("data-title", item.title);
  78. divsuggestion.setAttribute("data-uri", item.uri);
  79. divsuggestion.setAttribute("data-context", item.context);
  80. divsuggestion.innerText = '» ' + item.title;
  81. divsuggestion.appendChild(divcontext);
  82. return divsuggestion.outerHTML;
  83. },
  84. /* onSelect callback fires when a search suggestion is chosen */
  85. onSelect: function(e, term, item) {
  86. location.href = item.getAttribute('data-uri');
  87. }
  88. });
  89. });