-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
77 lines (63 loc) · 2.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
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');
var urls = [];
module.exports = {
book: {
assets: './assets',
js: [
'doc-search-lib.js',
'doc-search.js'
],
css: [
'doc-search.css'
]
},
hooks: {
"page": function (page) {
if (this.output.name != 'website') return page;
var lang = this.isLanguageBook() ? this.config.values.language : '';
if (lang) lang = lang + '/';
var outputUrl = this.output.toURL('_book/' + lang + page.path);
var normalizedUrl = outputUrl + (path.extname(outputUrl) !== '.html' ? 'index.html' : '');
if (!urls.some(item => item.url === normalizedUrl)) {
urls.push({
url: normalizedUrl
});
}
return page;
},
"finish": function () {
var $, $el, html;
var logo;
var pathFile = this.options.pluginsConfig.docSearch.apiKey && this.options.pluginsConfig.docSearch.index;
if(pathFile){
var searchBox = '<div id="book-search-input">\n' +
' <input type="text" id="book-doc-search-input" placeholder="Type to search">\n' +
'</div>';
if (this.options.pluginsConfig.docSearch.logo) {
if (this.options.pluginsConfig.docSearch.brandUrl) {
logo = '<a href="'+this.options.pluginsConfig.docSearch.brandUrl+'">' +
' <img class="logo" src="/'+this.options.pluginsConfig.docSearch.logo+'"/>'+
'</a>';
}
else {
logo = '<img class="logo" src="/'+this.options.pluginsConfig.docSearch.logo+'"/>';
}
}
urls.forEach(item => {
html = fs.readFileSync(item.url, {encoding: 'utf-8'});
$ = cheerio.load(html);
if ($('#book-search-input').length === 0) {
$el = $('.book-summary');
$el.prepend(searchBox);
if (logo) {
$el.prepend(logo);
}
}
fs.writeFileSync(item.url, $.root().html(), {encoding: 'utf-8'});
});
}
}
}
}