-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
executable file
·161 lines (143 loc) · 4.8 KB
/
app.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
var express = require('express'),
path = require('path'),
favicon = require('static-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
fs = require('fs'),
marked = require('./marked'),
moment = require('moment'),
raneto = require('./raneto'),
config = require('./config'),
toc = require('./markdown-toc'),
pinyin=require("pinyin"),
app = express(); // 使用nodejs express
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('layout', 'layout');
app.set('view engine', 'html'); // 设置引擎为html
app.enable('view cache');
app.engine('html', require('hogan-express'));
app.use(favicon(__dirname +'/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use("/bower_components",express.static(path.join(__dirname, 'bower_components')));
/**
* 对所有路径进行解析.
*/
app.all('*', function(req, res, next){
if(req.query.search){
var searchResults =[];
raneto.search(req.query.search).forEach(function(result){
searchResults.push(raneto.getPage(__dirname +'/content/'+ result.ref, config));
});
var pageListSearch = raneto.getPages('', config);
return res.render('search', {
config: config,
pages: pageListSearch,
search: req.query.search,
searchResults: searchResults,
body_class: 'page-search'
});
}
else if(req.params[0]){
var slug = req.params[0];
if(slug == '/') slug = '/index'; //如果是首页,那么转向/index
var filePath = __dirname +'/content'+ slug +'.md';
var pageList = raneto.getPages(slug, config);
if(slug == '/index' && !fs.existsSync(filePath)){
return res.render('home', {
config: config,
pages: pageList,
body_class: 'page-home',
layout:'index_layout'
});
} else if(slug == '/5m' && !fs.existsSync(filePath)){
return res.render('5m', {
config: config,
pages: pageList,
body_class: 'page-home',
layout:'5m_layout'
});
} else if(slug == '/outmen' && !fs.existsSync(filePath)){
return res.render('outmen', {
config : {},
pages : {},
body_class: '',
layout: ''
});
} else {
fs.readFile(filePath, 'utf8', function(err, content) {
if(err){
err.status = '404';
err.message = 'Whoops. Looks like this page doesn\'t exist.';
return next(err);
}
// File info
var stat = fs.lstatSync(filePath);
// Meta
var meta = raneto.processMeta(content);
content = raneto.stripMeta(content);
// Content
content = raneto.processVars(content, config);
var html = marked(content);
var counter={};
var _toc=toc(content,{maxdepth:3,slugify:function(raw){
if(counter[raw]!=null){
counter[raw]+=1;
}
else{
counter[raw]=0;
}
return pinyin(raw,{style:pinyin.STYLE_NORMAL}).join("-").replace(/[^\w]+/g, '-')+counter[raw]
}});
var _tocHtml=marked(_toc.content);
var tmpl="page";
if(meta["tmpl"]){
tmpl=meta["tmpl"];
}
return res.render(tmpl, {
config: config,
toc:_tocHtml,
pages: pageList,
meta: meta,
content: html,
body_class: 'page-'+ raneto.cleanString(slug.replace(/\//g, ' ')),
last_modified: moment(stat.mtime).format('YYYY/MM/DD')
});
});
}
} else {
next();
}
});
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
config: config,
status: err.status,
message: err.message,
error: err,
body_class: 'page-error'
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
config: config,
status: err.status,
message: err.message,
error: {},
body_class: 'page-error'
});
});
module.exports = app;