search.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ---
  2. layout: null
  3. ---
  4. (function () {
  5. function getQueryVariable(variable) {
  6. var query = window.location.search.substring(1),
  7. vars = query.split("&");
  8. for (var i = 0; i < vars.length; i++) {
  9. var pair = vars[i].split("=");
  10. if (pair[0] === variable) {
  11. return pair[1];
  12. }
  13. }
  14. }
  15. function getPreview(query, content, previewLength) {
  16. previewLength = previewLength || (content.length * 2);
  17. var parts = query.split(" "),
  18. match = content.toLowerCase().indexOf(query.toLowerCase()),
  19. matchLength = query.length,
  20. preview;
  21. // Find a relevant location in content
  22. for (var i = 0; i < parts.length; i++) {
  23. if (match >= 0) {
  24. break;
  25. }
  26. match = content.toLowerCase().indexOf(parts[i].toLowerCase());
  27. matchLength = parts[i].length;
  28. }
  29. // Create preview
  30. if (match >= 0) {
  31. var start = match - (previewLength / 2),
  32. end = start > 0 ? match + matchLength + (previewLength / 2) : previewLength;
  33. preview = content.substring(start, end).trim();
  34. if (start > 0) {
  35. preview = "..." + preview;
  36. }
  37. if (end < content.length) {
  38. preview = preview + "...";
  39. }
  40. // Highlight query parts
  41. preview = preview.replace(new RegExp("(" + parts.join("|") + ")", "gi"), "<strong>$1</strong>");
  42. } else {
  43. // Use start of content if no match found
  44. preview = content.substring(0, previewLength).trim() + (content.length > previewLength ? "..." : "");
  45. }
  46. return preview;
  47. }
  48. function displaySearchResults(results, query) {
  49. var searchResultsEl = document.getElementById("search-results"),
  50. searchProcessEl = document.getElementById("search-process");
  51. if (results.length) {
  52. var resultsHTML = "";
  53. results.forEach(function (result) {
  54. var item = window.data[result.ref],
  55. contentPreview = getPreview(query, item.content, 170),
  56. titlePreview = getPreview(query, item.title);
  57. resultsHTML += "<li><h4><a href='{{ site.baseurl }}" + item.url.trim() + "'>" + titlePreview + "</a></h4><p><small>" + contentPreview + "</small></p></li>";
  58. });
  59. searchResultsEl.innerHTML = resultsHTML;
  60. searchProcessEl.innerText = "Showing";
  61. } else {
  62. searchResultsEl.style.display = "none";
  63. searchProcessEl.innerText = "No";
  64. }
  65. }
  66. window.index = lunr(function () {
  67. this.field("id");
  68. this.field("title", {boost: 10});
  69. this.field("category");
  70. this.field("url");
  71. this.field("content");
  72. });
  73. var query = decodeURIComponent((getQueryVariable("q") || "").replace(/\+/g, "%20")),
  74. searchQueryContainerEl = document.getElementById("search-query-container"),
  75. searchQueryEl = document.getElementById("search-query"),
  76. searchInputEl = document.getElementById("search-input");
  77. searchInputEl.value = query;
  78. searchQueryEl.innerText = query;
  79. searchQueryContainerEl.style.display = "inline";
  80. for (var key in window.data) {
  81. window.index.add(window.data[key]);
  82. }
  83. displaySearchResults(window.index.search(query), query); // Hand the results off to be displayed
  84. })();