-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
search.js
327 lines (304 loc) · 10.2 KB
/
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
window.relearn = window.relearn || {};
window.relearn.runInitialSearch = function () {
if (window.relearn.isSearchInit && window.relearn.isLunrInit) {
var input = document.querySelector('#R-search-by-detail');
if (!input) {
return;
}
var value = input.value;
searchDetail(value);
}
};
var lunrIndex, pagesIndex;
function initLunrIndex(index) {
pagesIndex = index;
// Set up Lunr by declaring the fields we use
// Also provide their boost level for the ranking
lunrIndex = lunr(function () {
this.use(lunr.multiLanguage.apply(null, contentLangs));
this.ref('index');
this.field('title', {
boost: 15,
});
this.field('tags', {
boost: 10,
});
this.field('content', {
boost: 5,
});
this.pipeline.remove(lunr.stemmer);
this.searchPipeline.remove(lunr.stemmer);
// Feed Lunr with each file and let LUnr actually index them
pagesIndex.forEach(function (page, idx) {
page.index = idx;
this.add(page);
}, this);
});
window.relearn.isLunrInit = true;
window.relearn.runInitialSearch();
}
function triggerSearch() {
var input = document.querySelector('#R-search-by-detail');
if (!input) {
return;
}
var value = input.value;
searchDetail(value);
// add a new entry to the history after the user
// changed the term; this does not reload the page
// but will add to the history and update the address bar URL
var url = new URL(window.location);
var oldValue = url.searchParams.get('search-by');
if (value != oldValue) {
var state = window.history.state || {};
state = Object.assign({}, typeof state === 'object' ? state : {});
url.searchParams.set('search-by', value);
state.search = url.toString();
// with normal pages, this is handled by the 'pagehide' event, but this
// doesn't fire in case of pushState, so we have to do the same thing
// here, too
state.contentScrollTop = +elc.scrollTop;
window.history.pushState(state, '', url);
}
}
window.addEventListener('popstate', function (event) {
// restart search if browsed through history
if (event.state) {
var state = window.history.state || {};
state = Object.assign({}, typeof state === 'object' ? state : {});
if (state.search) {
var url = new URL(state.search);
if (url.searchParams.has('search-by')) {
var search = url.searchParams.get('search-by');
// we have to insert the old search term into the inputs
var inputs = document.querySelectorAll('input.search-by');
inputs.forEach(function (e) {
e.value = search;
var event = document.createEvent('Event');
event.initEvent('input', false, false);
e.dispatchEvent(event);
});
// recreate the last search results and eventually
// restore the previous scrolling position
searchDetail(search);
}
}
}
});
var input = document.querySelector('#R-search-by-detail');
if (input) {
input.addEventListener('keydown', function (event) {
// if we are pressing ESC in the searchdetail our focus will
// be stolen by the other event handlers, so we have to refocus
// here after a short while
if (event.key == 'Escape') {
setTimeout(function () {
input.focus();
}, 0);
}
});
}
function initLunrJs() {
// new way to load our search index
if (window.index_js_url) {
var js = document.createElement('script');
js.src = index_js_url;
js.setAttribute('async', '');
js.onload = function () {
initLunrIndex(relearn_searchindex);
};
js.onerror = function (e) {
console.error('Error getting Hugo index file');
};
document.head.appendChild(js);
}
}
/**
* Trigger a search in Lunr and transform the result
*
* @param {String} term
* @return {Array} results
*/
function search(term) {
// Find the item in our index corresponding to the Lunr one to have more info
// Remove Lunr special search characters: https://lunrjs.com/guides/searching.html
term = term.replace(/[*:^~+-]/g, ' ');
var searchTerm = lunr
.tokenizer(term)
.reduce(function (a, token) {
return a.concat(searchPatterns(token.str));
}, [])
.join(' ');
return !searchTerm || !lunrIndex
? []
: lunrIndex.search(searchTerm).map(function (result) {
return { index: result.ref, matches: Object.keys(result.matchData.metadata) };
});
}
function searchPatterns(word) {
// for short words high amounts of typos doesn't make sense
// for long words we allow less typos because this largly increases search time
var typos = [
{ len: -1, typos: 1 },
{ len: 60, typos: 2 },
{ len: 40, typos: 3 },
{ len: 20, typos: 4 },
{ len: 16, typos: 3 },
{ len: 12, typos: 2 },
{ len: 8, typos: 1 },
{ len: 4, typos: 0 },
];
return [
word + '^100',
word + '*^10',
'*' + word + '^10',
word +
'~' +
typos.reduce(function (a, c, i) {
return word.length < c.len ? c : a;
}).typos +
'^1',
];
}
function resolvePlaceholders(s, args) {
var args = args || [];
// use replace to iterate over the string
// select the match and check if the related argument is present
// if yes, replace the match with the argument
return s.replace(/{([0-9]+)}/g, function (match, index) {
// check if the argument is present
return typeof args[index] == 'undefined' ? match : args[index];
});
}
function searchDetail(value) {
var results = document.querySelector('#R-searchresults');
var hint = document.querySelector('.searchhint');
hint.innerText = '';
results.textContent = '';
var a = search(value);
if (a.length) {
hint.innerText = resolvePlaceholders(window.T_N_results_found, [value, a.length]);
a.forEach(function (item) {
var page = pagesIndex[item.index];
var numContextWords = 10;
var contextPattern =
'(?:\\S+ +){0,' +
numContextWords +
'}\\S*\\b(?:' +
item.matches
.map(function (match) {
return match.replace(/\W/g, '\\$&');
})
.join('|') +
')\\b\\S*(?: +\\S+){0,' +
numContextWords +
'}';
var context = page.content.match(new RegExp(contextPattern, 'i'));
var divsuggestion = document.createElement('a');
divsuggestion.className = 'autocomplete-suggestion';
divsuggestion.setAttribute('data-term', value);
divsuggestion.setAttribute('data-title', page.title);
divsuggestion.setAttribute('href', window.relearn.relBaseUri + page.uri);
divsuggestion.setAttribute('data-context', context);
var divtitle = document.createElement('div');
divtitle.className = 'title';
divtitle.innerText = '» ' + page.title;
divsuggestion.appendChild(divtitle);
var divbreadcrumb = document.createElement('div');
divbreadcrumb.className = 'breadcrumbs';
divbreadcrumb.innerText = page.breadcrumb || '';
divsuggestion.appendChild(divbreadcrumb);
if (context) {
var divcontext = document.createElement('div');
divcontext.className = 'context';
divcontext.innerText = context || '';
divsuggestion.appendChild(divcontext);
}
results.appendChild(divsuggestion);
});
window.relearn.markSearch();
} else if (value.length) {
hint.innerText = resolvePlaceholders(window.T_No_results_found, [value]);
}
input.focus();
setTimeout(adjustContentWidth, 0);
// if we are initiating search because of a browser history
// operation, we have to restore the scrolling postion the
// user previously has used; if this search isn't initiated
// by a browser history operation, it simply does nothing
var state = window.history.state || {};
state = Object.assign({}, typeof state === 'object' ? state : {});
if (state.hasOwnProperty('contentScrollTop')) {
window.setTimeout(function () {
elc.scrollTop = +state.contentScrollTop;
}, 10);
return;
}
}
initLunrJs();
function startSearch() {
var input = document.querySelector('#R-search-by-detail');
if (input) {
var state = window.history.state || {};
state = Object.assign({}, typeof state === 'object' ? state : {});
state.search = window.location.toString();
window.history.replaceState(state, '', window.location);
}
var searchList = new autoComplete({
/* selector for the search box element */
selectorToInsert: 'search:has(.searchbox)',
selector: '#R-search-by',
/* source is the callback to perform the search */
source: function (term, response) {
response(search(term));
},
/* renderItem displays individual search results */
renderItem: function (item, term) {
var page = pagesIndex[item.index];
var numContextWords = 2;
var contextPattern =
'(?:\\S+ +){0,' +
numContextWords +
'}\\S*\\b(?:' +
item.matches
.map(function (match) {
return match.replace(/\W/g, '\\$&');
})
.join('|') +
')\\b\\S*(?: +\\S+){0,' +
numContextWords +
'}';
var context = page.content.match(new RegExp(contextPattern, 'i'));
var divsuggestion = document.createElement('div');
divsuggestion.className = 'autocomplete-suggestion';
divsuggestion.setAttribute('data-term', term);
divsuggestion.setAttribute('data-title', page.title);
divsuggestion.setAttribute('data-uri', window.relearn.relBaseUri + page.uri);
divsuggestion.setAttribute('data-context', context);
var divtitle = document.createElement('div');
divtitle.className = 'title';
divtitle.innerText = '» ' + page.title;
divsuggestion.appendChild(divtitle);
if (context) {
var divcontext = document.createElement('div');
divcontext.className = 'context';
divcontext.innerText = context || '';
divsuggestion.appendChild(divcontext);
}
return divsuggestion.outerHTML;
},
/* onSelect callback fires when a search suggestion is chosen */
onSelect: function (e, term, item) {
location.href = item.getAttribute('data-uri');
e.preventDefault();
},
});
}
function ready(fn) {
if (document.readyState == 'complete') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(startSearch);