Markdown renderer aiming at providing :
[[toc]] from pandoc)# This is the title {#custom-id}).md to .html for relative links for static site generation."en")markdown-to-html is designed to render trusted sources only (your own documentation, course materials,…). Do not use it to render markdown files written by untrusted users :
.phtml views are injected as is in the layout and YAML metadata is exposed to the layout. Rendering untrusted content leads to XSS.check --check-external-links performs HTTP requests to every URL found in the source files.The server mode is intended for local preview :
127.0.0.1 by default (use --host 0.0.0.0 to expose it, as done in the Docker image)..git, node_modules) and dotfiles. Symbolic links pointing outside the source directory are not served.Node.js >= 22 is required.
npm install -g @mborne/markdown-to-html
# check version
markdown-to-html --version
# show help
markdown-to-html --help
# markdown-to-html [serve|convert|check] --help
markdown-to-html convert samples/01-default-layout /tmp/output
# with the default layout
markdown-to-html serve samples/01-default-layout
# with a custom layout
markdown-to-html serve samples/02-remarkjs --layout remarkjs
# exposed on every network interface (default is 127.0.0.1)
markdown-to-html serve samples/01-default-layout --host 0.0.0.0
To check links between markdown files :
markdown-to-html check samples/01-default-layout
# to include HTTP test for remote URLs
markdown-to-html check samples/01-default-layout --check-external-links
The package is written in TypeScript and ships its own type declarations.
Note that it is an ESM only package since v0.11.0 (require() is no longer supported).
import { convert, serve, Renderer, SourceDir, Layout } from '@mborne/markdown-to-html';
// generate a static site
convert('/path/to/source', '/path/to/output', '/path/to/layout', { language: 'fr' });
// or serve it on http://127.0.0.1:3000
serve('/path/to/source', '/path/to/layout', { language: 'fr', host: '127.0.0.1' });
// or render a single file
const sourceDir = new SourceDir('/path/to/source');
const renderer = new Renderer(sourceDir, new Layout('/path/to/layout'), {
renameLinksToHtml: true,
});
const sourceFile = sourceDir.locateFile('index.md');
if (sourceFile !== null) {
console.log(renderer.render(sourceFile));
}
The built-in layouts are shipped with the package and can be located with LAYOUT_NAMES and LAYOUTS_DIR :
import path from 'node:path';
import { LAYOUT_NAMES, LAYOUTS_DIR } from '@mborne/markdown-to-html';
console.log(LAYOUT_NAMES); // ['default', 'github', 'remarkjs']
const layoutPath = path.join(LAYOUTS_DIR, 'default');
It is inspired from :
See jamstack.org - Site Generators :