-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildIndex.js
More file actions
81 lines (72 loc) · 2.67 KB
/
Copy pathbuildIndex.js
File metadata and controls
81 lines (72 loc) · 2.67 KB
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
const CWD = process.cwd();
const fs = require('fs');
const lunr = require('lunr');
require(CWD + "/assets/js/lunr.unicodeNormalizer.js")(lunr);
async function initSearchIndex() {
try {
fs.readFile(CWD + "/public/index.json", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
const pagesIndex = JSON.parse(data);
const searchIndex = lunr(function () {
if (!(pagesIndex && Array.isArray(pagesIndex) && pagesIndex.length > 0)) {
return;
}
this.field("title");
this.field("content");
this.field("advisors");
this.field("students");
this.field("tags");
this.ref("href");
pagesIndex.forEach((page) => {
const _page = {};
Object.keys(page).forEach(key => {
const obj = page[key]
if (!(obj === null || obj === undefined)) {
if (Array.isArray(obj)) {
if (obj.length > 0) {
_page[key] = obj.map(function (t) {
return lunr.unicodeNormalizer(t);
}).join(" ");
}
}
else {
_page[key] = lunr.unicodeNormalizer(obj.toString());
}
}
})
this.add(_page)
});
});
fs.writeFile(CWD + '/static/searchIndex.json', JSON.stringify(searchIndex), (err) => {
if (err) {
console.error(err);
return;
}
});
fs.writeFile(CWD + '/static/pagesIndex.json', JSON.stringify(pagesIndex), (err) => {
if (err) {
console.error(err);
return;
}
});
fs.writeFile(CWD + '/public/searchIndex.json', JSON.stringify(searchIndex), (err) => {
if (err) {
console.error(err);
return;
}
});
fs.writeFile(CWD + '/public/pagesIndex.json', JSON.stringify(pagesIndex), (err) => {
if (err) {
console.error(err);
return;
}
});
});
} catch (e) {
console.log(e);
}
}
initSearchIndex();