-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (98 loc) · 2.71 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
// {
// "API": [{
// title: 'api',
// content: 'sdfdf'
// url: './index.html',
// children: [{
// title: 'api',
// content: 'api',
// url: ''
// }]
// }]
// }
const fs = require('fs')
const path = require('path')
const cheerio = require('cheerio');
const searchJson = {}
const h1Sign = '----h1----'
function objToArr(obj){
var arr = []
for(var i in obj){
obj[i] && arr.push(obj[i])
}
return arr;
}
function filterHTMLTag(msg) {
if(typeof msg !== 'string') return ''
msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
msg = msg.replace(/[|]*\n/, '') //去除行尾空格
msg = msg.replace(/&npsp;/ig, ''); //去掉npsp
return msg;
}
function getContent(childNodes, $, url){
if(!childNodes)return []
let children = {};
let content = ''
let i = 0;
let curParent = null;
for(let i=0; i< childNodes.length; i++){
let child = $(childNodes[i]);
let tagName = child[0].tagName;
if(tagName === 'h1'){
curParent = h1Sign
}else if(tagName === 'h2' || tagName === 'h3'){
curParent = child.attr('id')
children[curParent] = {
title: filterHTMLTag(child.text()),
url: url + '#' + curParent,
content: filterHTMLTag(child.text())
}
}else if(curParent){
if(curParent === h1Sign){
content += (filterHTMLTag(child.text()) || '')
}else children[curParent].content += (filterHTMLTag(child.text()) || '');
}
}
return {
content: content,
children: objToArr(children)
};
}
module.exports = {
page: function(page, book){
if(page.type === 'md'){
page = Object.assign({}, page);
let $ = cheerio.load(page.content);
let childNodes = $('#markdown-body').contents();
searchJson[book.name] = searchJson[book.name] ? searchJson[book.name] : [];
const dist = this.config.dist;
let pagePath = page.distPath.substr(dist.length)
const content = getContent(childNodes, $, pagePath)
searchJson[book.name].push({
title: page.title,
content: content.content,
url: pagePath,
children: content.children
})
}
},
init: function(){
this.addAsset('./search_json.js', 'js')
},
finish: function(){
const dist = this.config.dist;
fs.writeFileSync(path.resolve(dist, './search_json.js'), `window.ydoc_plugin_search_json = ${JSON.stringify(searchJson, null, 2)}`)
},
assets: {
dir: './assets',
js: ['core.js', 'search.js'],
css: 'search.css'
},
"tpl:header": function() {
return `<div class="m-search">
<div class="icon"></div>
<input type="text" class="input js-input" placeholder="搜索" />
<div class="m-search-result js-search-result"></div>
</div>`;
}
}