Convert Markdown to HTML using the unified ecosystem (remark + rehype). GitHub Flavored Markdown is enabled by default.
Built with TypeScript, bundled with tsup, tested with Vitest. Ships ESM, CommonJS and TypeScript declarations.
Node.js 22 or newer (see package.json).
import { markdownToHtml } from 'generator-markdown-to-html';
const html = await markdownToHtml('# Hello, *world*!');
// <h1>Hello, <em>world</em>!</h1>CommonJS works too:
const { markdownToHtml } = require('generator-markdown-to-html');markdownToHtml is also the default export.
| Option | Type | Default | Description |
|---|---|---|---|
gfm |
boolean |
true |
Tables, strikethrough, task lists and autolink literals. |
linkAttributes |
Record<string, unknown> |
none | Attributes added to every <a>, using hast property names (see below). |
const html = await markdownToHtml('[docs](https://example.com)', {
linkAttributes: { target: '_blank', rel: 'noopener', className: 'external' },
});
// <p><a href="https://example.com" target="_blank" rel="noopener" class="external">docs</a></p>Use className (not class) for CSS classes, as property names follow
hast.
- The function is pure: no file or network access, no logging.
- Raw HTML inside Markdown is dropped (the default
remark-rehypebehaviour), but link and image URLs are not sanitized (for examplejavascript:URLs pass through). If you render untrusted Markdown, sanitize the resulting HTML (for example withsanitize-htmlor DOMPurify) before embedding it.
npm install
npm run typecheck # tsc --noEmit
npm run build # tsup -> dist/
npm test # vitestdist/index.js— ESMdist/index.cjs— CommonJSdist/index.d.ts— TypeScript declarations
examples/ contains standalone scripts showing how to write custom remark
plugins (prefixing headings, decorating links). They are not part of the
published package. Run them from the repository root, for example:
npx tsx examples/custom-link-plugin.tsGitHub Actions runs type check, build and tests on Node 22 and 24 for
every push and pull request to master/main.