Test
Last active
August 29, 2015 14:01
-
-
Save smcelhinney/bea27c9ed2ab1a73cff7 to your computer and use it in GitHub Desktop.
Node Markdown parser (uses Restify)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Bootstrap 3 Template</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<!-- Bootstrap core CSS --> | |
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.0/css/bootstrap.css" rel="stylesheet" | |
media="screen"> | |
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> | |
<!--[if lt IE 9]> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.3.0/respond.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="col-md-8"> | |
{{& content}} | |
</div> | |
<div class="col-md-4"> | |
Page created at {{& now}} | |
</div> | |
</div> | |
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> | |
<!-- Include all compiled plugins (below), or include individual files as needed --> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.0/js/bootstrap.min.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var restify = require("restify"); | |
var server = restify.createServer({ | |
name: "A Markdown Parser", | |
version: require("./package.json").version | |
}); | |
// Some middleware. | |
server.use(restify.queryParser()); | |
// Documentation (done using Markdown) | |
var virtualPath = "/docs/:filename"; | |
server.get(virtualPath, function (req, res, next) { | |
var fs = require('fs') | |
, marked = require('marked') | |
, Mustache = require('mustache') | |
, physicalPath = './documentation/markdown/' + req.params.filename + ".md"; | |
// These are the default options, so change if you like. | |
marked.setOptions({ | |
renderer: new marked.Renderer(), | |
gfm: true, | |
tables: true, | |
breaks: false, | |
pedantic: false, | |
sanitize: true, | |
smartLists: true, | |
smartypants: false | |
}); | |
fs.readFile(physicalPath, 'utf8', function (err, data) { | |
if (err) { | |
return next(new restify.ResourceNotFoundError("This file is not found.")); | |
} | |
// Get your template file (mine is a simple Bootstrap template) | |
fs.readFile('./documentation/markdown.mst', 'utf8', function (_e, _d) { | |
var view = { | |
now: new Date, | |
content: marked(data) | |
}; | |
if (_e) { | |
return next(new restify.ResourceNotFoundError("The template file could not be found.")); | |
} | |
var body = Mustache.render(_d, view); | |
res.writeHead(200, { | |
'Content-Length': Buffer.byteLength(body), | |
'Content-Type': 'text/html' | |
}); | |
res.write(body); | |
res.end(); | |
}); | |
}); | |
}); | |
// Startup | |
server.listen(process.env.PORT || 80, function () { | |
console.log('Started up and listening on %s', server.url); | |
}); |
Also, I'd make it a little more performant by loading the Mustache template into the cache on node server startup using Mustache.parse(template);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obviously make sure all your paths are correct :)