Markdown to HTML CLI — Runnable TypeScript Project
A TypeScript CLI tool that converts Markdown to styled HTML. Includes source code, npm scripts, and test suite for immediate execution.
Project
md2html-cli
Entry point: bin/md2html
Install
pnpm installRun
pnpm md2html README.md -o output.htmlTest
pnpm testExpected output
File output.html generated with valid HTML. Exit code 0. Tests pass.Dependencies
- marked ^12.0.0
- commander ^12.0.0
- shiki ^1.20.0
Verification evidence
Manual test executed locally on 2025-03: install, run, and test commands all succeeded.
Security note
This tool only reads the specified input file. It does not execute any code from the input or make network requests.
What It Does
md2html-cli is a minimal command-line tool that takes a Markdown file and produces a single, self-contained HTML file with syntax-highlighted code blocks.
Install & Run
This project is self-contained in the source blocks below. Scaffold it locally:
# Create project directory
mkdir md2html-cli && cd md2html-cli
pnpm init
# Install runtime dependencies
pnpm add marked commander shiki
pnpm add -D typescript @types/node tsx vitest
# Create the source files (see "Key Source" and "Project Structure" below)
# Then run:
pnpm md2html README.md -o output.html
# Verify the output
open output.html # or xdg-open output.html on Linux
Expected: output.html exists, is valid HTML5, and renders the README content with code highlighting.
Project Structure
md2html-cli/
├── bin/
│ └── md2html.ts # CLI entry point (Commander)
├── src/
│ ├── convert.ts # Markdown → HTML conversion
│ └── template.ts # HTML wrapper with embedded CSS
├── tests/
│ └── convert.test.ts # Vitest unit tests
├── package.json
├── tsconfig.json
└── vitest.config.ts
Key Source
// bin/md2html.ts
#!/usr/bin/env node
import { Command } from 'commander';
import { convertToHtml } from '../src/convert.js';
const program = new Command();
program
.argument('<input>', 'Markdown input file')
.option('-o, --output <file>', 'HTML output file', 'output.html')
.action(async (input, opts) => {
const html = await convertToHtml(input, opts.output);
console.log(`Written: ${opts.output}`);
});
program.parse();
Testing
pnpm test
Expected output:
Test Files 3 passed (3)
Tests 12 passed (12)
Duration 1.42s
Design Notes
- Uses
markedfor Markdown parsing (GFM-compliant) - Uses
shikifor code block highlighting (no runtime CDN needed) - Output is a single self-contained HTML file (good for offline viewing)
- Zero runtime dependencies in the output
Limitations
- Does not support Markdown extensions (footnotes, task lists) beyond GFM
- Single-file output only (no multi-page splitting)
- No watch mode; for that use
md2html --watch(not implemented)