PageSourceSearch

https://s31k31.github.io/js/search.js

js s31k31.github.io collected 2026-10-03 09:53:16 UTC 5,416 bytes, 140 lines download raw bytes

1// A local search script with the help of [hexo-generator-search](https://github.com/PaicHyperionDev/hexo-generator-search)
2// Copyright (C) 2015 
3// Joseph Pan <http://github.com/wzpan>
4// Shuhao Mao <http://github.com/maoshuhao>
5// This library is free software; you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as
7// published by the Free Software Foundation; either version 2.1 of the
8// License, or (at your option) any later version.
9// 
10// This library is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14// 
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18// 02110-1301 USA
19// 
20
21var searchFunc = function (path, search_id, content_id) {
22  'use strict';
23  var BTN = "<button type='button' class='local-search-close' id='local-search-close'></button>";
24  $.ajax({
25    url: path,
26    dataType: "xml",
27    success: function (xmlResponse) {
28      // get the contents from search data
29      // var datas = $("entry", xmlResponse).map(function () {
30      //   return {
31      //     title: $("title", this).text(),
32      //     content: $("content", this).text(),
33      //     url: $("url", this).text()
34      //   };
35      // }).get();
36
37      // get the contents from search data
38      // 基于 GitHub 用户 DrebinPeng 的修改
39      var _div = document.createElement('div') // 此处修改
40      var datas = $("entry", xmlResponse).map(function () {
41        // 解决search.xml中的中文被转码为HTML实体的问题,即出现<p>&#x56E0;&#x6B64;&#x4E0A;</p>这样的问题
42        _div.innerHTML = $("content", this).text() // 此处修改
43        return {
44          title: $("title", this).text(),
45          content: _div.innerHTML, // 此处修改
46          url: $("url", this).text()
47        };
48      }).get();
49
50      var $input = document.getElementById(search_id);
51      var $resultContent = document.getElementById(content_id);
52
53      $input.addEventListener('input', function () {
54        var str = '<ul class="search-result-list">';
55        var keywords = this.value.trim().toLowerCase().split(/[\s]+/);
56        $resultContent.innerHTML = "";
57        if (this.value.trim().length <= 0) {
58          return;
59        }
60        // perform local searching
61        datas.forEach(function (data) {
62          var isMatch = true;
63          // var content_index = [];
64          if (!data.title || data.title.trim() === '') {
65            data.title = "Untitled";
66          }
67          var data_title = data.title.trim().toLowerCase();
68          var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
69          var data_url = data.url;
70          var index_title = -1;
71          var index_content = -1;
72          var first_occur = -1;
73          // only match artiles with not empty contents
74          if (data_content !== '') {
75            keywords.forEach(function (keyword, i) {
76              index_title = data_title.indexOf(keyword);
77              index_content = data_content.indexOf(keyword);
78
79              if (index_title < 0 && index_content < 0) {
80                isMatch = false;
81              } else {
82                if (index_content < 0) {
83                  index_content = 0;
84                }
85                if (i == 0) {
86                  first_occur = index_content;
87                }
88                // content_index.push({index_content:index_content, keyword_len:keyword_len});
89              }
90            });
91          } else {
92            isMatch = false;
93          }
94          // show search results
95          if (isMatch) {
96            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
97            var content = data.content.trim().replace(/<[^>]+>/g, "");
98            if (first_occur >= 0) {
99              // cut out 100 characters
100              var start = first_occur - 20;
101              var end = first_occur + 80;
102
103              if (start < 0) {
104                start = 0;
105              }
106
107              if (start == 0) {
108                end = 100;
109              }
110
111              if (end > content.length) {
112                end = content.length;
113              }
114
115              var match_content = content.substr(start, end);
116
117              // highlight all keywords
118              keywords.forEach(function (keyword) {
119                var regS = new RegExp(keyword, "gi");
120                match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
121              });
122
123              str += "<p class=\"search-result\">" + match_content + "...</p>"
124            }
125            str += "</li>";
126          }
127        });
128        str += "</ul>";
129        if (str.indexOf('<li>') === -1) {
130          return $resultContent.innerHTML = BTN + "<div class=\"search-result-empty\"><p><i class=\"fe fe-tired\"></i> 没有找到内容,更换下搜索词试试吧~<p></div>";
131        }
132        $resultContent.innerHTML = BTN + str;
133      });
134    }
135  });
136  $(document).on('click', '#local-search-close', function () {
137    $('#local-search-input').val('');
138    $('#local-search-result').html('');
139  });
140};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.