-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-docs.js
164 lines (140 loc) · 3.8 KB
/
build-docs.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var fm = require('front-matter');
var jade = require('jade');
var marked = require('marked');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var glob = require('glob');
var highlight = require('highlight.js');
var docTpl = function () {
return jade.compileFile('./app/_doc.jade', {pretty: true});
};
var sidebarTpl = function () {
return jade.compileFile('./app/_sidebar.jade', {pretty: true});
};
var link = function (dir) {
return '/' + dir.replace(/^docs\//, '').replace(/\.md$/, '').replace(/index$/, '').replace(/\/$/, '');
};
marked.setOptions({
highlight: function (code, lang) {
if(lang) {
return highlight.highlight(lang, code, true).value;
} else {
return code;
}
}
});
var getInfo = function (file) {
var content = fm(fs.readFileSync(file, 'utf8'));
var docLink = link(file);
var docTitle = content.attributes.title;
var docBody = content.body;
var docIndex = path.basename(file) === 'index.md';
return {
link: docLink,
title: docTitle,
body: docBody,
index: docIndex,
file: file
}
};
var getCrumbs = function (info) {
var breadcrumbs = info.link.split('/').slice(1);
if(breadcrumbs.length == 1 && breadcrumbs[0] === '')
breadcrumbs = [];
var out = [];
var acc = '';
out.push({
active: false,
title: 'docs',
link: '/'
});
breadcrumbs.forEach(function (item) {
acc += '/' + item;
out.push({
active: false,
title: item,
link: acc
})
});
out[out.length - 1].active = true;
return out;
};
var recurse = function (dir, top) {
var docs = fs.readdirSync(dir);
var out = top ? [] : {articles: []};
docs.forEach(function (doc) {
var docPath = path.join(dir, doc);
var stats = fs.statSync(docPath);
if(stats.isDirectory()) {
if(top) {
out.push(recurse(docPath, false));
} else {
out.articles.push(recurse(docPath, false));
}
} else if(stats.isFile()) {
if(top) {
out.push(getInfo(docPath));
} else {
var info = getInfo(docPath);
if(info.index) {
out.link = info.link;
out.title = info.title;
out.body = info.body;
out.index = info.index;
} else {
out.articles.push(info);
}
}
}
});
return out;
};
module.exports = function (cb) {
var articles = recurse('./docs/', true);
// move to first
for(var i = 0; i < articles.length; i++) {
var article = articles[i];
if(article.link === '/') {
articles.splice(i, 1);
articles.splice(0, 0, article);
}
}
var sidebart = sidebarTpl();
var doct = docTpl();
var searchData = [];
var files = glob.sync('docs/**/*.md');
files.forEach(function (file) {
var info = getInfo(file);
var publicFile = file.replace(/^docs/, 'public').replace(/\.md$/, '.html');
mkdirp.sync(path.dirname(publicFile));
var doc = marked(info.body);
var crumbs = getCrumbs(info);
var activeLinks = crumbs.length === 1 ? [crumbs[0].link] :
crumbs.slice(1).map(function (crumb) { return crumb.link });
var compiled = doct({
title: info.title,
doc: doc,
sidebar: sidebart({
articles: articles,
activeLinks: activeLinks
}),
breadcrumbs: crumbs,
link: 'https://github.com/LapisBlue/Docs/blob/master/' + info.file
});
searchData.push({
link: info.link,
title: info.title,
body: doc + info.body,
crumbsFlat: crumbs.map(function (crumb) { return crumb.title }).join('/'),
crumbs: crumbs
});
fs.writeFileSync(publicFile, compiled);
});
mkdirp.sync('./public/js/');
fs.writeFileSync('./public/js/search-data.js', '\nsetSearchData(' + JSON.stringify(searchData, null, 2) + ');');
if(cb) cb();
};
if(require.main === module) {
module.exports();
}