PageSourceSearch

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

js lonion1124.github.io collected 2026-10-03 09:32:05 UTC 5,878 bytes, 121 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    $.ajax({
24        url: path,
25        dataType: "xml",
26        success: function( xmlResponse ) {
27            // get the contents from search data
28            var datas = $( "entry", xmlResponse ).map(function() {
29                return {
30                    title: $( "title", this ).text(),
31                    content: $("content",this).text(),
32                    url: $( "url" , this).text()
33                };
34            }).get();
35
36            var $input = document.getElementById(search_id);
37			if (!$input) return;
38            var $resultContent = document.getElementById(content_id);
39            if ($("#local-search-input").length > 0) {
40                $input.addEventListener('input', function () {
41                    var str = '<ul class=\"search-result-list list-group list-group-flush\">';
42                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
43                    $resultContent.innerHTML = "";
44                    if (this.value.trim().length <= 0) {
45                        return;
46                    }
47                    // perform local searching
48                    datas.forEach(function (data) {
49                        var isMatch = true;
50                        var content_index = [];
51                        if (!data.title || data.title.trim() === '') {
52                            data.title = "Untitled";
53                        }
54                        var data_title = data.title.trim().toLowerCase();
55                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
56                        var data_url = data.url;
57                        var index_title = -1;
58                        var index_content = -1;
59                        var first_occur = -1;
60                        // only match artiles with not empty contents
61                        if (data_content !== '') {
62                            keywords.forEach(function (keyword, i) {
63                                index_title = data_title.indexOf(keyword);
64                                index_content = data_content.indexOf(keyword);
65
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                                    // content_index.push({index_content:index_content, keyword_len:keyword_len});
76                                }
77                            });
78                        } else {
79                            isMatch = false;
80                        }
81                        // show search results
82                        if (isMatch) {
83                            str += "<li class=\"list-group-item\"><a href='" + data_url + "' class='search-result-title fs-5 link-underline link-offset-2 link-un
83derline-opacity-0 link-underline-opacity-100-hover'><i class='bi bi-cursor'></i>&nbsp;" + data_title + "</a>";
84                            var content = data.content.trim().replace(/<[^>]+>/g, "");
85                            if (first_occur >= 0) {
86                                // cut out 100 characters
87                                var start = first_occur - 20;
88                                var end = first_occur + 80;
89
90                                if (start < 0) {
91                                    start = 0;
92                                }
93
94                                if (start == 0) {
95                                    end = 100;
96                                }
97
98                                if (end > content.length) {
99                                    end = content.length;
100                                }
101
102                                var match_content = content.substring(start, end);
103
104                                // highlight all keywords
105                                keywords.forEach(function (keyword) {
106                                    var regS = new RegExp(keyword, "gi");
107                                    match_content = match_content.replace(regS, "<strong class=\"search-keyword text-primary\">" + keyword + "</strong>");
108                                });
109
110                                str += "<p class=\"search-result text-muted\">" + match_content + "...</p>"
111                            }
112                            str += "</li>";
113                        }
114                    });
115                    str += "</ul>";
116                    $resultContent.innerHTML = str;
117                });
118            }
119        }
120    });
121}

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.