-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (52 loc) · 1.59 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
64
var Entities = require('html-entities').AllHtmlEntities;
var Html = new Entities();
// Map of Lunr ref to document
var documentsStore = {};
module.exports = {
book: {
assets: './assets',
js: [
'jquery.mark.min.js',
'search.js'
],
css: [
'search.css'
]
},
hooks: {
// Index each page
'page': function(page) {
if (this.output.name != 'website' || page.search === false) {
return page;
}
var text;
this.log.debug.ln('index page', page.path);
text = page.content;
// Decode HTML
text = Html.decode(text);
// Strip HTML tags
text = text.replace(/(<([^>]+)>)/ig, '');
text = text.replace(/[\n ]+/g, ' ');
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[doc.url] = doc;
return page;
},
// Write index to disk
'finish': function() {
if (this.output.name != 'website') return;
this.log.debug.ln('write search index');
return this.output.writeFile('search_plus_index.json', JSON.stringify(documentsStore));
}
}
};