markdown-to-html

Node.js CI Coverage Status

Markdown renderer aiming at providing :

Features

Security

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 :

The server mode is intended for local preview :

Usage

Install

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

Generate static website

markdown-to-html convert samples/01-default-layout /tmp/output

Serve markdown file

# 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

Check markdown files

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

Programmatic API

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');

Credits

It is inspired from :

Samples

Example Source Layout
Classic example samples/01-default-layout/index.md layout/default/page.html
RemarkJS slideshow samples/02-remarkjs/index.md layout/remarkjs/page.html
GitHub pages generated mixing both mborne/cours-patron-conception see build.js

License

MIT

Alternatives

See jamstack.org - Site Generators :

Docs