-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-search.js
330 lines (303 loc) · 11 KB
/
local-search.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* global CONFIG */
document.addEventListener('DOMContentLoaded', () => {
if (!CONFIG.path) {
console.warn('`hexo-generator-searchdb` plugin is not installed!');
return;
}
// Popup Window
let isfetched = false;
let datas;
const input = document.querySelector('.search-input');
const getIndexByWord = (words, text, caseSensitive = false) => {
const index = [];
const included = new Set();
words.forEach(word => {
if (CONFIG.localsearch.unescape) {
const div = document.createElement('div');
div.innerText = word;
word = div.innerHTML;
}
const wordLen = word.length;
if (wordLen === 0) return;
let startPosition = 0;
let position = -1;
if (!caseSensitive) {
text = text.toLowerCase();
word = word.toLowerCase();
}
while ((position = text.indexOf(word, startPosition)) > -1) {
index.push({ position, word });
included.add(word);
startPosition = position + wordLen;
}
});
// Sort index by position of keyword
index.sort((left, right) => {
if (left.position !== right.position) {
return left.position - right.position;
}
return right.word.length - left.word.length;
});
return [index, included];
};
// Merge hits into slices
const mergeIntoSlice = (start, end, index) => {
let item = index[0];
let { position, word } = item;
const hits = [];
const count = new Set();
while (position + word.length <= end && index.length !== 0) {
count.add(word);
hits.push({
position,
length: word.length
});
const wordEnd = position + word.length;
// Move to next position of hit
index.shift();
while (index.length !== 0) {
item = index[0];
position = item.position;
word = item.word;
if (wordEnd > position) {
index.shift();
} else {
break;
}
}
}
return {
hits,
start,
end,
count: count.size
};
};
// Highlight title and content
const highlightKeyword = (val, slice) => {
let result = '';
let index = slice.start;
for (const { position, length } of slice.hits) {
result += val.substring(index, position);
index = position + length;
result += `<mark class="search-keyword">${val.substr(position, length)}</mark>`;
}
result += val.substring(index, slice.end);
return result;
};
const getResultItems = keywords => {
const resultItems = [];
datas.forEach(({ title, content, url }) => {
// The number of different keywords included in the article.
const [indexOfTitle, keysOfTitle] = getIndexByWord(keywords, title);
const [indexOfContent, keysOfContent] = getIndexByWord(keywords, content);
const includedCount = new Set([...keysOfTitle, ...keysOfContent]).size;
// Show search results
const hitCount = indexOfTitle.length + indexOfContent.length;
if (hitCount === 0) return;
const slicesOfTitle = [];
if (indexOfTitle.length !== 0) {
slicesOfTitle.push(mergeIntoSlice(0, title.length, indexOfTitle));
}
let slicesOfContent = [];
while (indexOfContent.length !== 0) {
const item = indexOfContent[0];
const { position } = item;
// Cut out 100 characters. The maxlength of .search-input is 80.
const start = Math.max(0, position - 20);
const end = Math.min(content.length, position + 80);
slicesOfContent.push(mergeIntoSlice(start, end, indexOfContent));
}
// Sort slices in content by included keywords' count and hits' count
slicesOfContent.sort((left, right) => {
if (left.count !== right.count) {
return right.count - left.count;
} else if (left.hits.length !== right.hits.length) {
return right.hits.length - left.hits.length;
}
return left.start - right.start;
});
// Select top N slices in content
const upperBound = parseInt(CONFIG.localsearch.top_n_per_article, 10);
if (upperBound >= 0) {
slicesOfContent = slicesOfContent.slice(0, upperBound);
}
let resultItem = '';
url = new URL(url, location.origin);
url.searchParams.append('highlight', keywords.join(' '));
if (slicesOfTitle.length !== 0) {
resultItem += `<li><a href="${url.href}" class="search-result-title">${highlightKeyword(title, slicesOfTitle[0])}</a>`;
} else {
resultItem += `<li><a href="${url.href}" class="search-result-title">${title}</a>`;
}
slicesOfContent.forEach(slice => {
resultItem += `<a href="${url.href}"><p class="search-result">${highlightKeyword(content, slice)}...</p></a>`;
});
resultItem += '</li>';
resultItems.push({
item: resultItem,
id : resultItems.length,
hitCount,
includedCount
});
});
return resultItems;
};
const inputEventFunction = () => {
if (!isfetched) return;
const searchText = input.value.trim().toLowerCase();
const keywords = searchText.split(/[-\s]+/);
const container = document.querySelector('.search-result-container');
let resultItems = [];
if (searchText.length > 0) {
// Perform local searching
resultItems = getResultItems(keywords);
}
if (keywords.length === 1 && keywords[0] === '') {
container.classList.add('no-result');
container.innerHTML = '<div class="search-result-icon"><i class="fa fa-search fa-5x"></i></div>';
} else if (resultItems.length === 0) {
container.classList.add('no-result');
container.innerHTML = '<div class="search-result-icon"><i class="far fa-frown fa-5x"></i></div>';
} else {
resultItems.sort((left, right) => {
if (left.includedCount !== right.includedCount) {
return right.includedCount - left.includedCount;
} else if (left.hitCount !== right.hitCount) {
return right.hitCount - left.hitCount;
}
return right.id - left.id;
});
const stats = CONFIG.i18n.hits.replace(/\$\{hits}/, resultItems.length);
container.classList.remove('no-result');
container.innerHTML = `<div class="search-stats">${stats}</div>
<hr>
<ul class="search-result-list">${resultItems.map(result => result.item).join('')}</ul>`;
window.pjax && window.pjax.refresh(container);
}
};
const fetchData = () => {
// Search DB path
const searchPath = CONFIG.root + CONFIG.path;
const isXml = !CONFIG.path.endsWith('json');
fetch(searchPath)
.then(response => response.text())
.then(res => {
// Get the contents from search data
isfetched = true;
datas = isXml ? [...new DOMParser().parseFromString(res, 'text/xml').querySelectorAll('entry')].map(element => {
return {
title : element.querySelector('title').textContent,
content: element.querySelector('content').textContent,
url : element.querySelector('url').textContent
};
}) : JSON.parse(res);
// Only match articles with non-empty titles
datas = datas.filter(data => data.title).map(data => {
data.title = data.title.trim();
data.content = data.content ? data.content.trim().replace(/<[^>]+>/g, '') : '';
data.url = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
return data;
});
// Remove loading animation
inputEventFunction();
});
};
/**
* This function returns the parsed url parameters of the
* current request. Multiple values per key are supported,
* it will always return arrays of strings for the value parts.
*/
const getQueryParameters = () => {
const s = location.search;
const parts = s.substr(s.indexOf('?') + 1).split('&');
const result = {};
for (const part of parts) {
const [key, value] = part.split('=', 2);
if (key in result) {
result[key].push(value);
} else {
result[key] = [value];
}
}
return result;
};
// Highlight by wrapping node in mark elements with the given class name
const highlightText = (node, slice, className) => {
const val = node.nodeValue;
let index = slice.start;
const children = [];
for (const { position, length } of slice.hits) {
const text = document.createTextNode(val.substring(index, position));
index = position + length;
const mark = document.createElement('mark');
mark.className = className;
mark.appendChild(document.createTextNode(val.substr(position, length)));
children.push(text, mark);
}
node.nodeValue = val.substring(index, slice.end);
children.forEach(element => {
node.parentNode.insertBefore(element, node);
});
};
// Highlight the search words provided in the url in the text
const highlightSearchWords = () => {
const params = getQueryParameters();
const keywords = params.highlight ? params.highlight[0].split(/\+/).map(decodeURIComponent) : [];
const body = document.querySelector('.post-body');
if (!keywords.length || !body) return;
const walk = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, false);
const allNodes = [];
while (walk.nextNode()) {
if (!walk.currentNode.parentNode.matches('button, select, textarea')) allNodes.push(walk.currentNode);
}
allNodes.forEach(node => {
const [indexOfNode] = getIndexByWord(keywords, node.nodeValue);
if (!indexOfNode.length) return;
const slice = mergeIntoSlice(0, node.nodeValue.length, indexOfNode);
highlightText(node, slice, 'search-keyword');
});
};
highlightSearchWords();
if (CONFIG.localsearch.preload) {
fetchData();
}
if (CONFIG.localsearch.trigger === 'auto') {
input.addEventListener('input', inputEventFunction);
} else {
document.querySelector('.search-icon').addEventListener('click', inputEventFunction);
input.addEventListener('keypress', event => {
if (event.key === 'Enter') {
inputEventFunction();
}
});
}
// Handle and trigger popup window
document.querySelectorAll('.popup-trigger').forEach(element => {
element.addEventListener('click', () => {
document.body.classList.add('search-active');
// Wait for search-popup animation to complete
setTimeout(() => input.focus(), 500);
if (!isfetched) fetchData();
});
});
// Monitor main search box
const onPopupClose = () => {
document.body.classList.remove('search-active');
};
document.querySelector('.search-pop-overlay').addEventListener('click', event => {
if (event.target === document.querySelector('.search-pop-overlay')) {
onPopupClose();
}
});
document.querySelector('.popup-btn-close').addEventListener('click', onPopupClose);
document.addEventListener('pjax:success', () => {
highlightSearchWords();
onPopupClose();
});
window.addEventListener('keyup', event => {
if (event.key === 'Escape') {
onPopupClose();
}
});
});