-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildIndex.js
71 lines (65 loc) · 1.91 KB
/
buildIndex.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
var glob = require("glob");
var lunr = require("lunr");
var path = require("path");
var fs = require("fs");
var h2t = require("html-to-text");
// Put these in a module
async function getPages() {
return new Promise((resolve, reject) => {
glob(
"**/*.fjson",
{
cwd: path.join(__dirname, "json"),
},
(err, files) => {
if (err) reject(err);
else resolve(files);
},
);
});
}
async function getData(filename) {
const raw = fs.readFileSync(path.join(__dirname, "json", filename));
let str = raw.toString();
// str = str.replace(imagesRE3, "/static/_images/");
// str = str.replace(imagesRE2, "/static/_images/");
// if (runMath) {
// const obj = JSON.parse(str);
// if (obj.body) {
// obj.body = await mathify(obj.body);
// }
// str = JSON.stringify(obj);
// }
return str;
}
async function buildIndex() {
const pages = await getPages();
let fileData = {};
await Promise.all(
pages.map(async page => {
const data = await getData(page);
fileData[page] = data;
}),
);
var idx = lunr(function() {
const obj = this;
obj.field("id");
obj.field("title");
obj.field("body");
pages.forEach(page => {
console.log("Processing " + page);
const data = JSON.parse(fileData[page]);
const text = h2t.fromString(data.body);
if (data.title && data.body) {
const addition = {
title: data.title,
body: text,
id: `/${data.current_page_name}/`,
};
obj.add(addition);
}
});
});
fs.writeFileSync("static/lunr.json", JSON.stringify(idx.toJSON()));
}
buildIndex().catch(err => console.error(err));