forked from GitbookIO/plugin-lunr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (94 loc) · 3.46 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var nodejieba = require("nodejieba");
var PouchDB = require('pouchdb');
var replicationStream = require('pouchdb-replication-stream');
// PouchDB.debug.enable('*');
PouchDB.plugin(replicationStream.plugin);
PouchDB.adapter('writableStream', replicationStream.adapters.writableStream);
PouchDB.plugin(require('pouchdb-upsert'));
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
// var Promise = require("bluebird");
// Promise.longStackTraces();
var rimraf = require('rimraf');
var db = new PouchDB('searchindex');
module.exports = {
book: {
assets: './assets',
js: [
'pouchdb.min.js', 'pouchdb.load.min.js', 'search-jieba.js'
]
},
hooks: {
'init': function () {
// return db.destroy();
},
// Index each page
'page': function(page) {
if (this.output.name != 'website' || page.search === false) {
return page;
}
var text, that = this;
var url = this.output.toURL(page.path);
this.log.debug.ln('index page', page.path);
// Transform as TEXT
text = page.content.replace(/(<([^>]+)>)/ig, '');
// Add to index
var doc = {
url: this.output.toURL(page.path),
title: page.title,
summary: page.description,
body: text
};
// 分词
var words = _.uniq(nodejieba.cutForSearch(page.title + text, true));
// 移除特殊字符和标点符号
_.pull(words, ' ', '\n', '(', ')', '.', '-', '(', ')', ' ', ':', ':', ',', '。', '—', '?', '[', ']');
function insertData(word) {
word = word.toUpperCase();
return db.upsert('word__' + word, function (newdoc) {
if (!newdoc.urls) {
newdoc.urls = [];
}
newdoc.urls = _.union(newdoc.urls, [url]);
return newdoc;
}).then(function() {
});
}
return Promise.all(words.map(insertData))
.then(function () {
// Insert new doc
return db.upsert('doc__' + doc.url, function (newdoc) {
newdoc.doc = doc;
return newdoc;
});
})
.then(function () {
return page;
});
},
// Write index to disk
'finish': function() {
var that = this;
if (this.output.name != 'website') return;
var file_path = path.resolve(this.output.root(), 'search_jieba_index.dat');
var ws = fs.createWriteStream(file_path);
// dump db for browser to load
return db.dump(ws).catch(function (err) {
console.log(err);
}).then(function () {
return new Promise(function (fulfill, reject) {
// Delete levelDB generated by nodejs pouchDB
rimraf(path.resolve(that.output.root(), 'searchindex'), function (err) {
if (err) {
reject(err);
}
else {
fulfill();
}
});
})
});
}
}
};