forked from GitbookIO/plugin-lunr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.37 KB
/
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
var Entities = require('html-entities').AllHtmlEntities;
var Html = new Entities();
var documentsStore = [];
var searchIndexEnabled = true;
module.exports = {
book: {
assets: './assets',
js: [
'plugin.js'
]
},
hooks: {
// Index each page
'page': function (page) {
if (this.output.name !== 'website' || !searchIndexEnabled || page.search === false) {
return page;
}
this.log.debug.ln('index page', page.path);
var text;
text = page.content;
// Decode HTML
text = Html.decode(text);
// Strip HTML tags
text = text.replace(/(<([^>]+)>)/ig, '');
var keywords = [];
if (page.search) {
keywords = page.search.keywords || [];
}
// Add to index
var doc = {
url: this.output.toURL(page.path),
title: page.title,
summary: page.description,
keywords: keywords.join(' '),
body: text
};
documentsStore.push(doc);
return page;
},
// Write index to disk
'finish': function () {
if (this.output.name !== 'website') return;
this.log.debug.ln('write search index');
var output = ''
documentsStore.forEach(store => {
output += '{ "index" : {} }\n'
output += JSON.stringify(store) + '\n'
})
return this.output.writeFile('search_index.json', output)
}
}
};