local-search.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* global CONFIG */
  2. (function() {
  3. // Modified from [hexo-generator-search](https://github.com/wzpan/hexo-generator-search)
  4. function localSearchFunc(path, searchSelector, resultSelector) {
  5. 'use strict';
  6. // 0x00. environment initialization
  7. var $input = jQuery(searchSelector);
  8. var $result = jQuery(resultSelector);
  9. if ($input.length === 0) {
  10. // eslint-disable-next-line no-console
  11. throw Error('No element selected by the searchSelector');
  12. }
  13. if ($result.length === 0) {
  14. // eslint-disable-next-line no-console
  15. throw Error('No element selected by the resultSelector');
  16. }
  17. if ($result.attr('class').indexOf('list-group-item') === -1) {
  18. $result.html('<div class="m-auto text-center"><div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div><br/>Loading...</div>');
  19. }
  20. jQuery.ajax({
  21. // 0x01. load xml file
  22. url : path,
  23. dataType: 'xml',
  24. success : function(xmlResponse) {
  25. // 0x02. parse xml file
  26. var dataList = jQuery('entry', xmlResponse).map(function() {
  27. return {
  28. title : jQuery('title', this).text(),
  29. content: jQuery('content', this).text(),
  30. url : jQuery('url', this).text()
  31. };
  32. }).get();
  33. if ($result.html().indexOf('list-group-item') === -1) {
  34. $result.html('');
  35. }
  36. $input.on('input', function() {
  37. // 0x03. parse query to keywords list
  38. var content = $input.val();
  39. var resultHTML = '';
  40. var keywords = content.trim().toLowerCase().split(/[\s-]+/);
  41. $result.html('');
  42. if (content.trim().length <= 0) {
  43. return $input.removeClass('invalid').removeClass('valid');
  44. }
  45. // 0x04. perform local searching
  46. dataList.forEach(function(data) {
  47. var isMatch = true;
  48. if (!data.title || data.title.trim() === '') {
  49. data.title = 'Untitled';
  50. }
  51. var orig_data_title = data.title.trim();
  52. var data_title = orig_data_title.toLowerCase();
  53. var orig_data_content = data.content.trim().replace(/<[^>]+>/g, '');
  54. var data_content = orig_data_content.toLowerCase();
  55. var data_url = data.url;
  56. var index_title = -1;
  57. var index_content = -1;
  58. var first_occur = -1;
  59. // Skip matching when content is included in search and content is empty
  60. if (CONFIG.include_content_in_search && data_content === '') {
  61. isMatch = false;
  62. } else {
  63. keywords.forEach(function (keyword, i) {
  64. index_title = data_title.indexOf(keyword);
  65. index_content = data_content.indexOf(keyword);
  66. if (index_title < 0 && index_content < 0) {
  67. isMatch = false;
  68. } else {
  69. if (index_content < 0) {
  70. index_content = 0;
  71. }
  72. if (i === 0) {
  73. first_occur = index_content;
  74. }
  75. }
  76. });
  77. }
  78. // 0x05. show search results
  79. if (isMatch) {
  80. resultHTML += '<a href=\'' + data_url + '\' class=\'list-group-item list-group-item-action font-weight-bolder search-list-title\'>' + orig_data_title + '</a>';
  81. var content = orig_data_content;
  82. if (first_occur >= 0) {
  83. // cut out 100 characters
  84. var start = first_occur - 20;
  85. var end = first_occur + 80;
  86. if (start < 0) {
  87. start = 0;
  88. }
  89. if (start === 0) {
  90. end = 100;
  91. }
  92. if (end > content.length) {
  93. end = content.length;
  94. }
  95. var match_content = content.substring(start, end);
  96. // highlight all keywords
  97. keywords.forEach(function(keyword) {
  98. var regS = new RegExp(keyword, 'gi');
  99. match_content = match_content.replace(regS, '<span class="search-word">' + keyword + '</span>');
  100. });
  101. resultHTML += '<p class=\'search-list-content\'>' + match_content + '...</p>';
  102. }
  103. }
  104. });
  105. if (resultHTML.indexOf('list-group-item') === -1) {
  106. return $input.addClass('invalid').removeClass('valid');
  107. }
  108. $input.addClass('valid').removeClass('invalid');
  109. $result.html(resultHTML);
  110. });
  111. }
  112. });
  113. }
  114. function localSearchReset(searchSelector, resultSelector) {
  115. 'use strict';
  116. var $input = jQuery(searchSelector);
  117. var $result = jQuery(resultSelector);
  118. if ($input.length === 0) {
  119. // eslint-disable-next-line no-console
  120. throw Error('No element selected by the searchSelector');
  121. }
  122. if ($result.length === 0) {
  123. // eslint-disable-next-line no-console
  124. throw Error('No element selected by the resultSelector');
  125. }
  126. $input.val('').removeClass('invalid').removeClass('valid');
  127. $result.html('');
  128. }
  129. var modal = jQuery('#modalSearch');
  130. var searchSelector = '#local-search-input';
  131. var resultSelector = '#local-search-result';
  132. modal.on('show.bs.modal', function() {
  133. var path = CONFIG.search_path || '/local-search.xml';
  134. localSearchFunc(path, searchSelector, resultSelector);
  135. });
  136. modal.on('shown.bs.modal', function() {
  137. jQuery('#local-search-input').focus();
  138. });
  139. modal.on('hidden.bs.modal', function() {
  140. localSearchReset(searchSelector, resultSelector);
  141. });
  142. })();