-
Notifications
You must be signed in to change notification settings - Fork 412
/
serve.js
66 lines (50 loc) · 1.74 KB
/
serve.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
'use strict';
const path = require('path'),
debug = require('debug')('reveal-md'),
express = require('express'),
_ = require('lodash'),
liveReload = require('livereload'),
open = require('open'),
render = require('./render'),
parsePath = require('./options').parsePath;
const staticDir = express.static;
let initialOptions;
function updatePathOptions(req, res, next) {
const pathOpts = parsePath(req.url, initialOptions.baseDir);
res.locals.options = _.extend({}, initialOptions, pathOpts);
debug('Update path options to %O', pathOpts);
next();
}
function getScript(req, res) {
res.sendFile(path.resolve(process.cwd(), req.url.replace(/^\/scripts\//, '')));
}
module.exports = function startServer(options, cb) {
initialOptions = options;
const app = express();
['css', 'js', 'plugin', 'lib'].forEach(dir => {
app.use('/' + dir, staticDir(path.join(options.revealBasePath, dir)));
});
app.use('/css/highlight', staticDir(options.highlightThemePath));
if(options.watch) {
const liveReloadServer = liveReload.createServer({
exts: ['md']
});
liveReloadServer.watch(process.cwd());
}
app.get('/', updatePathOptions, render.renderMarkdownFileListing);
app.get(/(\w+\.md)$/, updatePathOptions, render.renderMarkdownAsSlides);
app.get('/scripts/*', getScript);
app.get('/*', staticDir(process.cwd()));
const server = app.listen(options.port);
const host = `http://${options.host}:${options.port}`;
const link = options.relativePath || options.url;
const url = `${host}${link ? '/' + link : ''}`;
if(!options.print) {
/* eslint-disable no-console */
console.log(`Reveal-server started at ${host}`);
if(!options.disableAutoOpen) {
open(url);
}
}
if(cb) cb(server);
};