Module: markdown-serve

markdown-serve

Main object exported by module. Can be used to instantiate a new MarkdownServer instance or invoked as a middleware function in Express.

Source:

Requires

Members

<static> MarkdownServer

Source:

Methods

<static> middleware(options)

Simple Express middleware that hosts a MarkdownServer

Parameters:
Name Type Description
options Object

Middleware options

Properties
Name Type Argument Description
rootDirectory string

Full path to root physical directory containing Markdown files to serve

markedOptions Object <optional>

Global marked module options for Markdown processing

resolverOptions resolverOptions <optional>

Options to override default document name and Markdown file extension. If not specified, will default to index and md resppectively.

view string <optional>

Name of view to use for rendering content. If this property & handler are not specified, a JSON respresentation of MarkdownFile will be returned by the middleware.

preParse boolean | preParseFn <optional>

Only applies when view is specified. If set to true (not truthy), will make the parsed HTML content available as markdownFile.parsedContent on the view model object passed to the view. This is to support some view engines like hbs that do not support calling methods directly on the view model.

If specified as a preParseFn function, will return the function result as the view model object to the view.

handler handlerFn <optional>

handlerFn. Provides full customization of middleware response. Make sure view is not set when using this feature as that takes precedence.

Source:
Examples
// basic usage
// app.js
var express = require('express'),
    path = require('path'),
    mds = require('markdown-serve');

var app = express();

app.set('view', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(mds.middleware({
   rootDirectory: path.resolve(__dirname, 'content'),
   view: 'markdown'    // will use views/markdown.jade file for rendering out HTML content
}));

// views/markdown.jade
extend layout

block content
   header
     h1= markdownFile.meta.title

   .content
     != markdownFile.parseContent()

   footer
     hr
     | <strong>Created:</strong> #{markdownFile.created} <br/>
// preParse set to true when using hbs view engine
// as calling markdownFile.parseContent() is not supported, the HTML content is pre-parsed and available as markdownFile.parsedContent
app.set('view engine', 'hbs');

app.use(mds.middleware({
   rootDirectory: path.resolve(__dirname, 'content'),
   view: 'markdown',
   preParse: true
}));

// views/markdown.hbs
<div class="container">
   <h1>{{markdownFile.meta.title}}</h1>

   {{{markdownFile.parsedContent}}}

   <footer>
       <hr />
       <strong>Created:</strong> {{markdownFile.created}}
   </footer>
</div>

Type Definitions

handlerFn(markdownFile, req, res, next)

Customized middleware handler function option. Do not specify options.view if using this feature.

Parameters:
Name Type Description
markdownFile MarkdownFile

Resolved MarkdownFile instance from req.path passed to middleware

req Object

Express Request object

res Object

Express Response object

next function

Express next() function

Source:
Example
// custom handler
app.use(mds.middleware({
   rootDirectory: path.resolve(__dirname, 'content'),
   handler: function(markdownFile, req, res, next) {
       if (req.method !== 'GET') next();

       // limit access based on draft variable in front-matter
       if (markdownFile.meta.draft && !req.isAuthenticated && !req.user.isAdmin) {
           next();
           return;   // need return here
       }        

       res.render('markdown', { title: markdownFile.meta.title, content: markdownFile.parseContent() });
   }
}));

preParseFn(markdownFile) → {Object}

preParse function option used for customizing the view model object that is passed to the view

Parameters:
Name Type Description
markdownFile MarkdownFile

Resolved MarkdownFile instance from req.path passed to middleware

Source:
Returns:

Object literal to pass as view model to view

Type
Object
Example
// preParse is specified as a function
app.use(mds.middleware({
   rootDirectory: path.resolve(__dirname, 'content'),
   view: 'markdown',
   preParse: function(markdownFile) {
       return { title: markdownFile.meta.title, content: markdownFile.parseContent(), created: moment(markdownFile.created).format('L') };
   }
}));

// views/markdown.hbs
<div class="container">
   <h1>{{title}}</h1>

   {{{content}}}

   <footer>
       <hr />
       <strong>Created:</strong> {{created}}
   </footer>
</div>