This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (71 loc) · 2.07 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
/*
* https://github.com/lgersman/orangevolt-livereload
* http://www.orangevolt.com
*
* Copyright 2013, Lars Gersmann <[email protected]>
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
var path = require( 'path'),
fs = require( 'fs'),
ejs = require( 'ejs'),
marked = require( 'marked'),
highlight = require( 'highlight.js')
;
function createGrundConnectMiddleware( options, middleware) {
return function( connect, options) {
var directory = options.directory || options.base[options.base.length - 1];
var middlewares = [];
middlewares.push( middleware);
// copied from grunt-connect task
if (!Array.isArray( options.base)) {
options.base = [ options.base];
}
options.base.forEach(function( base) {
// Serve static files.
middlewares.push( connect.static( base));
});
// make orangevolt-docp package private resources available
// in case they are not provided by the "edited" package
middlewares.push( connect.static( __dirname));
// Make directory browse-able.
middlewares.push( connect.directory( directory));
return middlewares;
};
}
marked.setOptions({
highlight: function( code, lang) {
if( lang) {
return highlight.highlight( lang, code).value;
} else {
return highlight.highlightAuto( code).value;
}
}
});
module.exports.grunt = {
livereload : {
middleware : function( options) {
options = options || {};
return createGrundConnectMiddleware( options, function( req, res, next) {
var p = path.resolve( '.' + req._parsedUrl.pathname);
if( fs.existsSync( p) && fs.lstatSync( p).isFile() && /\.(md|markown|wiki)$/.test( p)) {
if( req.headers['x-requested-with']) {
var content = fs.readFileSync( p, "utf-8");
res.end( marked( content));
} else {
res.end(
ejs.render(
fs.readFileSync( path.join( __dirname, 'template.ejs'), "utf-8"), {
url : req._parsedUrl.pathname,
file : p,
context : options.context || {}
})
);
}
} else {
next();
}
}
);
}
}
};