Main object exported by module. Can be used to instantiate a new MarkdownServer instance or invoked as a middleware function in Express.
- Description:
Main object exported by module. Can be used to instantiate a new MarkdownServer instance or invoked as a middleware function in Express.
- Source:
- Copyright:
- Nguyen Ly 2014-2024
- License:
- MIT License
Requires
- module:path
- module:lodash
- module:mkdirp
- module:fs
- module:markdown-serve/parser
- module:markdown-serve/resolver
Members
(static) MarkdownServer
- Description:
- Source:
Methods
(static) middleware(options)
- Description:
Simple Express middleware that hosts a MarkdownServer
- 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', 'pug');
app.use(mds.middleware({
rootDirectory: path.resolve(__dirname, 'content'),
view: 'markdown' // will use views/markdown.pug file for rendering out HTML content
}));
// views/markdown.pug
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>
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object | Middleware options Properties
|
Type Definitions
handlerFn(markdownFile, req, res, next)
- Description:
Customized middleware handler function option. Do not specify
options.view
if using this feature.
- 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() });
}
}));
Parameters:
Name | Type | Description |
---|---|---|
markdownFile |
MarkdownFile | Resolved MarkdownFile instance from |
req |
Object | Express Request object |
res |
Object | Express Response object |
next |
function | Express next() function |
preParseFn(markdownFile) → {Object}
- Description:
preParse function option used for customizing the view model object that is passed to the view
- Source:
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>
Parameters:
Name | Type | Description |
---|---|---|
markdownFile |
MarkdownFile | Resolved MarkdownFile instance from |
Returns:
Object literal to pass as view model to view
- Type
- Object