Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}));
110 changes: 59 additions & 51 deletions lib/markdown.js
Original file line number Diff line number Diff line change
@@ -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;