-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (141 loc) · 4.99 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*!
* express-github-docs
* Copyright(c) 2015 Miska Kaipiainen
* MIT Licensed
*/
var path = require('path'),
fs = require('fs'),
url = require('url'),
Toc = require('./lib/toc.js'),
mdParser = require('./lib/md-parser.js'),
ghPuller = require('node-gh-repo-puller'),
ServeStatic = require('serve-static');
var expressgh = function(root, options){
if (!root) {
throw new TypeError('root path required')
}
if (typeof root !== 'string') {
throw new TypeError('root path must be a string')
}
// defaults
var o = options || {};
o.ghUser = o.ghUser || "";
o.ghRepo = o.ghRepo || "";
o.ghBranch = o.ghBranch || "master";
o.ghDir = o.ghDir || "";
o.render = o.render || true;
o.defaultLayout = o.defaultLayout || "docs";
o.defaultTemplate = o.defaultTemplate || "docs";
o.syncUrl = o.syncUrl || "_sync";
o.syncOnStart = o.syncOnStart || false;
o.root = path.resolve(root);
o.title = o.title || undefined;
// sanitize sync url; ensure leading "/" and remove trailing "/"
if((o.syncUrl.length > 0) && (o.syncUrl[0] != "/")) o.syncUrl = "/" + o.syncUrl;
if((o.syncUrl.length > 0) && (o.syncUrl[o.syncUrl.length-1] == "/")) o.syncUrl = o.syncUrl.substr(-1);
// init static files server
var serveStatic = ServeStatic(o.root);
if(o.syncOnStart){
syncGithub(o, function(e){
if(!e){
console.log("Sync with GitHub repository ["+ o.ghUser + "/" + o.ghRepo + "] done!");
} else {
console.log("Unable to sync with GitHub repository ["+ o.ghUser + "/" + o.ghRepo + "]!");
console.log(e);
}
});
}
return function expressgh(req, res, next) {
// resolve filepath
var filePath = path.join(o.root, url.parse(req.url).pathname);
// remove trailing slash
if(filePath[filePath.length-1] === path.sep) filePath = filePath.slice(0,-1);
// is sync url?
if(req.url == o.syncUrl){
syncGithub(o, function(e){
if(!e){
res.send("Sync with GitHub repository ["+ o.ghUser + "/" + o.ghRepo + "] done!");
} else {
res.send("Unable to sync with GitHub repository ["+ o.ghUser + "/" + o.ghRepo + "]!");
}
});
return;
}
// lookup for the .md file, else serve static file
if(fs.existsSync(filePath + ".md")){
filePath = filePath + ".md";
} else if(fs.existsSync(filePath + path.sep + "README.md")){
// just to preserve relative paths in .md files
var foo = url.parse(req.originalUrl).pathname;
if(foo[foo.length-1] !== "/" ){
foo += "/";
var redirectTo = foo;
if(url.parse(req.originalUrl).search){
redirectTo = foo + "?" + url.parse(req.originalUrl).search;
}
res.redirect(redirectTo);
return;
}
filePath = filePath + path.sep + "README.md";
} else {
serveStatic(req, res, next);
return;
}
// get the file contents
var rawFile = fs.readFileSync(filePath, 'utf8');
// parse the file
req.egd = mdParser(rawFile, {
rebasePath: req.baseUrl,
ghUser: o.ghUser,
ghRepo: o.ghRepo,
ghBranch: o.ghBranch,
ghDir: o.ghDir
});
// get table of contents
var toc = new Toc(o.root);
toc.get(function(tocErr, tocRes){
// expose front matter attributes to template
var attr = req.egd.attributes || {};
attr.content = req.egd.content;
// add table of contents
attr.egdtoc = "";
if(!tocErr){
attr.egdtoc = tocRes.toHTML({
title: o.title,
rebasePath: req.baseUrl,
selected: req.url
});
}
// render
if(o.render && req.egd && (typeof req.egd.content !== 'undefined')){
// if no layout or template, use defaults
if(!attr.layout) attr.layout = o.defaultLayout;
if(!attr.template) attr.template = o.defaultTemplate;
res.render(attr.template, attr);
} else {
next();
}
});
};
};
function syncGithub(o, callback){
if((o.ghUser == "") || (o.ghRepo == "")){
callback("Can not sync with GitHub! Invalid GitHub user/repo name!");
return;
}
ghPuller({
user: o.ghUser,
repo: o.ghRepo,
dir: o.ghDir,
branch: o.ghBranch,
target: o.root
}, function(pullerErr, pullerSuccess){
if(pullerErr){
callback(pullerErr);
return;
}
var toc = new Toc(o.root);
toc.update(callback);
});
}
module.exports = expressgh;