-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchBackend.js
41 lines (38 loc) · 1.06 KB
/
searchBackend.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
function initSearch(link, categories, indexBuilder) {
function loadBackend() {
return fetch(new Request(link))
.then((response) => response.json())
.then((json) => {
for(let title in json) {
if(json[title].category && !categories.includes(json[title].category)) {
delete json[title]
}
}
return json
});
}
function getLunrBuilder() {
return new Promise((resolve, reject) => {
window.addEventListener('DOMContentLoaded', () => {
elasticlunr.clearStopWords();
let builder = elasticlunr(indexBuilder)
resolve(builder);
})
});
}
return Promise.all([loadBackend(), getLunrBuilder()]).then(([data, builder]) => {
Object.values(data).map((doc) => {
Object.keys(doc).forEach((key) => {
if(Array.isArray(doc[key])) {
doc[key] = doc[key].join(" ")
}
})
return doc;
}
).forEach(builder.addDoc.bind(builder));
return {
searchData: data,
index: builder
}
})
}