Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
express-md
Advanced tools
Express middleware to serve markdown files as HTML
vars
npm install express-md --save
var express = require('express');
var expressMd = require('express-md');
var port = 3000;
var app = express();
// create an instance of express-md with custom options
var mdRouter = expressMd({
// serve markdown files from `docs` directory
dir: __dirname + '/docs',
// serve requests from root of the site
url: '/',
// variables to replace {{{ varName }}} in markdown files
vars: {
message: 'Hello World!'
}
});
// add as express middleware
app.use(mdRouter);
// start the server
app.listen(port, function () {
console.log(expressMd.version + ' listening on port ' + port);
});
You can configure express-md using the following options:
Option | Description | Default |
---|---|---|
dir | Directory where markdown files are located | |
url | URL from which markdown files should be served | |
extensions | Markdown files with these extensions will be served | ['.md', '.mdown'] |
passthrough | Files with these extensions will be served as-is | ['.css', '.png', '.jpg', '.jpeg', '.js'] |
headers | Add additional HTTP headers | |
cache | Override the caching subsystem. To disable caching, set this to false | in-memory cache |
watch | Automatically reload files changes in dir | false |
ignore | Request paths to ignore (supports strings and RegExp) | |
vars | Variables to replace {{{ variableName }}} in markdown files |
Place Markdown files with the extensions .md
or .mdown
in your docs directory. (You can override these file extensions; see below for details.) Organize the directory any way you like, with any number of subdirectories.
Each directory can have an index.md
(or index.mdown
) file that will be served if the user requests the directory name.
A template.html
file, if present in the same directory as a Markdown document, will be used to format that document. You can have multiple templates: express-md
will search parent directories up the directory tree to find the nearest template.html
and use that.
This allows you to have a default template, and override with custom templates in each subdirectory.
In template.html
, the text {{{ markdown }}}
will be replaced by the HTML that was rendered from the Markdown document.
You can specify placeholders in your HTML template using {{{ variableName }}}
variables can be passed in using the vars
option above, or using YAML font-matter within markdown files.
---
title: Barcode Scanner App
description: Orca Scan is a mobile barcode scanning app ...
---
Here is a quick example of how express-md
resolves templates.
<html>
<head>
<meta charset="utf-8">
<title>{{{ title }}}</title>
</head>
<body>
{{{ markdown }}}
{{{ message }}}
</body>
</html>
For this example, assume the following directory structure:
docs/
├── index.md
├── README.md
├── template.html
└── api/
├── index.md
├── template.html
└── v1.0/
└── index.md
The file docs/index.md
is served using the template file docs/template.html
.
The file docs/api/index.md
would be served using the template file docs/api/template.html
.
The file docs/api/v1.0/index.md
is in a directory that does not have a template file. In this case, express-md
will search up the directory tree until it finds a template. This file would be served using the template file docs/api/template.html
. If express-md
is unable to find a template, it will be served as a bare-bones HTML file which is styled to match Github readme.
Given the directory structure shown above, a request for http://localhost:3000/
would return docs/index.md
(converted to HTML, of course).
File extensions are handled automatically. In this example, the README file can be requested as http://localhost:3000/README
or http://localhost:3000/README.md
.
Likewise, the api/index.md
file can be requested as http://localhost:3000/api/
, http://localhost:3000/api/index.md
, or even http://localhost:3000/api/index
.
When an HTTP error occurs, express-md
will look for a document matching the error number, using the same logic that is used to find templates. Currently only 404
errors are supported this way.
For example, to have a custom 404
error page, create a 404.md
file. It will be converted to HTML and served using template.html
just like any other Markdown file would be.
Like templates, you can have custom 404.md
error documents in each subdirectory and express-md
will use the nearest one when serving an error.
Use the headers
option:
var middleware = expressMd({
headers: {'Cache-Control': 'public,max-age=3600'},
// other options…
});
The old version of the document is cached, either by express-md
or by your web browser. If you used a Cache-Control
header, the document may be cached by your web browser. Hit F5 (or Cmd-R, or Ctrl-R) a couple of times to refresh.
If you still see the old document, then it’s been cached by express-md
. Your options are:
express-md
false
as the cache
optionwatch
to force express-md
to automatically notice any changesexpress-md
aggressively caches the rendered, HTML form of your documents. The first time a document is requested, express-md
has to read it from disk (along with any template) and render it to HTML. On subsequent requests for the same document, it will be served from cache, which should be extremely fast. In addition, requests that result in a 404
error are cached, so once express-md
searches for a document and doesn’t find it, it won’t waste time looking for that document again.
By default, once a document is cached, express-md
will never re-read that document; the cached version will always be served until you reload the server. You have the option to disable caching by passing false
as the cache
option.
If you enable the experimental watch
option, the cache is emptied every time a change is detected in your docs
directory or any of its subdirectories. Because it may be resource-intensive, this option is turned off by default. Enabling it when you have a large set of documents or subdirectories may exhaust available file handles. If you only have a few documents or subdirectories, feel free to try it out. Contributions to improve this feature are welcome.
This a refactor of the node-docserver project.
Licensed under MIT License
FAQs
Express middleware to serve markdown files as HTML
We found that express-md demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.