forked from ryanlelek/Raneto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
115 lines (104 loc) · 3.64 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
var express = require('express'),
path = require('path'),
fs = require('fs'),
favicon = require('static-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
_s = require('underscore.string'),
moment = require('moment'),
marked = require('marked'),
validator = require('validator'),
extend = require('extend'),
raneto = require('raneto-core'),
config = require('./config'),
app = express();
// Setup views
app.set('views', path.join(__dirname, 'views'));
app.set('layout', 'layout');
app.set('view engine', 'html');
app.enable('view cache');
app.engine('html', require('hogan-express'));
// Setup 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')));
// Setup config
extend(raneto.config, config);
// Handle all requests
app.all('*', function(req, res, next) {
if(req.query.search){
var searchQuery = validator.toString(validator.escape(_s.stripTags(req.query.search))).trim(),
searchResults = raneto.doSearch(searchQuery),
pageListSearch = raneto.getPages('');
return res.render('search', {
config: config,
pages: pageListSearch,
search: searchQuery,
searchResults: searchResults,
body_class: 'page-search'
});
}
else if(req.params[0]){
var slug = req.params[0];
if(slug == '/') slug = '/index';
var pageList = raneto.getPages(slug),
filePath = path.normalize(raneto.config.content_dir + slug);
if(!fs.existsSync(filePath)) filePath += '.md';
if(slug == '/index' && !fs.existsSync(filePath)){
return res.render('home', {
config: config,
pages: pageList,
body_class: 'page-home'
});
} 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);
}
// Process Markdown files
if(path.extname(filePath) == '.md'){
// File info
var stat = fs.lstatSync(filePath);
// Meta
var meta = raneto.processMeta(content);
content = raneto.stripMeta(content);
if(!meta.title) meta.title = raneto.slugToTitle(filePath);
// Content
content = raneto.processVars(content);
var html = marked(content);
return res.render('page', {
config: config,
pages: pageList,
meta: meta,
content: html,
body_class: 'page-'+ raneto.cleanString(slug),
last_modified: moment(stat.mtime).format('Do MMM YYYY')
});
} else {
// Serve static file
res.sendfile(filePath);
}
});
}
} else {
next();
}
});
// Handle any errors
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;