-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate_search_index.js
82 lines (70 loc) · 2.19 KB
/
update_search_index.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
const lunr = require('lunr');
const fs = require('fs-extra');
const path = require('path');
const matter = require('gray-matter');
function pad0(input) {
return ('0' + input).slice(-2);
}
// 2020/09/05/Project-Management.html
// 2020-9-4-UI.md
function postId(input) {
const segs = input.split('-');
let fileName = segs.slice(3).join('-');
fileName = path.basename(fileName, path.extname(fileName));
return path.join(segs[0], pad0(segs[1]), pad0(segs[2]), fileName + '.html');
}
async function addPosts(idx) {
const posts = await fs.readdir(path.join('docs', '_posts'));
const res = [];
for (const post of posts) {
const dat = await fs.readFile(path.join('docs', '_posts', post), { encoding: 'utf8' });
const gm = matter(dat);
res.push({
id: postId(post),
title: gm.data.title,
author: gm.data.author,
content: gm.content,
});
}
return res;
}
function refId(input) {
const segs = input.split(path.sep);
segs[segs.length - 1] = path.basename(input, path.extname(input)) + '.html';
return path.join(...segs.slice(1));
}
async function addReference(idx) {
const files = [].concat(
(await fs.readdir(path.join('docs', 'api', 'classes'))).map(name => path.join('docs', 'api', 'classes', name)),
(await fs.readdir(path.join('docs', 'api', 'interfaces'))).map(name => path.join('docs', 'api', 'interfaces', name)),
path.join('docs', 'api', 'modules.md'),
);
const res = [];
for (const file of files) {
const dat = await fs.readFile(file, { encoding: 'utf8' });
res.push({
id: refId(file),
title: path.basename(file, path.extname(file)),
author: 'Reference',
content: dat,
});
}
return res;
}
async function main() {
const entries = [].concat(
await addPosts(this),
await addReference(this),
);
console.log('# search entries', entries.length);
const idx = lunr(function() {
this.ref('id');
this.field('author');
this.field('title');
this.field('content');
entries.forEach(entry => this.add(entry));
});
const idxStr = JSON.stringify(idx);
await fs.writeFile(path.join('docs', 'js', 'search_index.js'), '(function() { window.search_index = ' + idxStr + ' })()');
}
main();