diff --git a/README.md b/README.md index 0323327..0f9fabf 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ Usage // name of markdown variable passed in the context when rendering // optional // default 'markdown' - variable: 'bar' + variable: 'bar', + // default markdown file to load if a directory is requested + // optional + // default 'readme.md' + defaultFile: 'index.md' })); \ No newline at end of file diff --git a/lib/markdown.js b/lib/markdown.js index 6e1d374..8b67c94 100644 --- a/lib/markdown.js +++ b/lib/markdown.js @@ -1,57 +1,65 @@ var fs = require('fs'), - path = require('path'), - url = require('url'), - markdown = require('marked'); + path = require('path'), + url = require('url'), + markdown = require('marked'); function expressMarkdown(options) { - if (!options) - throw new Error("Missing options argument"); - - var dir = options.directory, - view = options.view, - variable = options.variable || 'markdown'; - - if (!dir) - throw new Error('Missing "directory" value in options'); - - // clean up path, remove '..' - dir = path.resolve(dir); - - return function(req, res, next) { - var file = req.url.toString(), - fileLower = file.toLowerCase(); - - if (fileLower.slice(-3) !== '.md' && fileLower.slice(-9) !== '.markdown') - return next(); - - file = dir + '/' + url.parse(file).pathname; - file = path.resolve(file); - - // make sure the final path is in our defined directory - if (file.substr(0, dir.length) !== dir) - return res.send(400); - - fs.exists(file, function(exists) { - if (!exists) - return next(); - - fs.readFile(file, 'utf8', function(err, data) { - var context = {}; - if(err) - return next(err); - - data = markdown(data); - - if (view) { - context[variable] = data; - res.render(view, context); - } - else { - res.send(data); - } - }); - }); - } + if (!options) + throw new Error("Missing options argument"); + + var dir = options.directory, + view = options.view, + variable = options.variable || 'markdown', + defaultFile = options.defaultFile || 'readme.md'; + + if (!dir) + throw new Error('Missing "directory" value in options'); + + // clean up path, remove '..' + dir = path.resolve(dir); + + return function(req, res, next) { + var file = req.url.toString().toLowerCase(); + + file = dir + '/' + url.parse(file).pathname; + + // Check if file is a directory and if so append the default file + try { + if (fs.lstatSync(file).isDirectory()) { + file = file + (file.slice(-1) !== '/' ? '/' : '') + defaultFile; + } + } catch (e) {} + + if (file.slice(-3) !== '.md' && file.slice(-9) !== '.markdown') + return next(); + + file = path.resolve(file); + + // make sure the final path is in our defined directory + if (file.substr(0, dir.length) !== dir) + return res.send(400); + + fs.exists(file, function(exists) { + if (!exists) + return next(); + + fs.readFile(file, 'utf8', function(err, data) { + var context = {}; + if(err) + return next(err); + + data = markdown(data); + + if (view) { + context[variable] = data; + res.render(view, context); + } + else { + res.send(data); + } + }); + }); + } } module.exports = expressMarkdown;