-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfrontEnd.js
171 lines (165 loc) · 4.62 KB
/
frontEnd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const dataFilePath = "/assets/data/search.json"; // search.json路径
const workerPath = "../assets/js/worker/search.worker.js"; // search.worker.js路径
function search(keyword) {
let start = new Date().getTime();
if (keyword == "" || keyword == ".") {
return false;
}
searchWord = HTMLDecode(keyword);
getSearchData().then((data) => {
if (typeof searchWorker == "undefined") {
searchWorker = new Worker(workerPath);
}
searchWorker.onmessage = (result) => {
let end = new Date().getTime();
let data = result.data;
console.log(`查询操作用时${end - start}MS`);
if (data.length == 0) {
console.log("未找到有关选项");
return false;
}
let resultHTML = "";
data.forEach((e, index) => {
resultHTML += structureSearchResult(e);
});
console.log(resultHTML); // 结果输出
};
searchWorker.postMessage([data, searchWord]);
});
}
function getSearchData() {
if (typeof searchData == "undefined") {
return new Promise((resolve, reject) => {
fetch(dataFilePath, {})
.then((response) => response.json())
.then((data) => {
searchData = data;
resolve(data);
});
}).catch((err) => {
throw err;
});
} else {
return Promise.resolve(searchData);
}
}
function HTMLEncode(str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/\'/g, "'");
s = s.replace(/\"/g, """);
s = s.replace(/\n/g, "<br/>");
return s;
}
function HTMLDecode(str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, "&");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/'/g, "'");
s = s.replace(/"/g, '"');
s = s.replace(/<br\/>/g, "\n");
return s;
}
function structureSearchResult(result) {
let cla = "";
let tag = "";
let match = "";
let matchItem,
matchNum,
matchContext,
matchTag,
matchClass,
matchTitle,
matchLinks;
let strLinks = "";
result.match.forEach((e) => {
if (typeof e == "object") {
matchItem = e[0];
} else {
matchItem = e;
}
switch (matchItem) {
case "name":
match += "标题 / ";
break;
case "context":
matchNum = e;
match += `内容(${e[1]}次) / `;
break;
case "title":
match += "章节 / ";
matchTitle = e;
break;
case "tag":
match += "标签 / ";
matchTag = e;
break;
case "class":
match += "分类 / ";
matchClass = e;
break;
case "links":
match += "外链 / ";
matchLinks = e;
break;
}
});
result.class.forEach((e, index) => {
if (typeof matchClass !== "undefined" && matchClass[1] == index) {
cla += `<a href="#/classification/${e}" class='active'>${e}</a>/`;
} else {
cla += `<a href="#/classification/${e}">${e}</a>/`;
}
});
cla = cla.substring(0, cla.length - 1);
result.tag.forEach((e, index) => {
if (typeof matchTag !== "undefined" && matchTag[1] == index) {
tag += `<a href="#/tag/${e}" class='active'>${e}</a>`;
} else {
tag += `<a href="#/tag/${e}">${e}</a>`;
}
});
match = match.substring(0, match.length - 3);
if (typeof matchNum == "undefined" || matchNum[2] < 10) {
matchContext = result.context.substring(0, 150);
} else {
matchContext = result.context.substring(
matchNum[2] - 10,
matchNum[2] + 140,
);
}
result.links.forEach((e, index) => {
if (typeof matchLinks !== "undefined" && matchLinks[1] == index) {
strLinks = `<a class='search-result-links one-line' href='${e}'><span class='i_small ri:link'></span> ${e}</a>`;
}
});
return `
<div class="loaded listprogram">
<article>
<span class="article-name">
<h4><a href="${result.url}">${result.name}</a></h4>
</span>
<p class="articles-info">
<span class='search-result-tags'>${match}</span>
<time>${result.time}</time> • <span class="i_small ri:archive-line"></span>
<span class="class">
${cla}
</span>
<div class='search-result-context'><span class='i_small ri:file-list-2-line'></span> ...${matchContext}</div>
${strLinks}
</p>
<p class="articles-tags">
${tag}
</p>
</article>
<hr>
</div>
`;
}