Converting Markdown to Clean HTML Without a Static Site Generator
Quick answer
To convert Markdown to HTML without a static site generator, run one command: pandoc -s -o page.html page.md. Pandoc reads Markdown and writes HTML by default, and the -s flag makes the result a complete web page instead of a loose fragment. No config file, no build folder, no theme.
That's enough for a single document: a README, a changelog, a one-page handout. The rest of this guide covers the details that make the output clean.
Step-by-step
1. Convert a file, then make it a full page
Start with a small file:
# Release notes
Version 2 adds **export** and fixes three bugs.
- Faster search
- Better tablesRun pandoc with no flags and you get a fragment, just the converted body. According to the Pandoc manual, "by default, pandoc produces a document fragment". That's handy when you're pasting the result into an existing page. Open it directly and it's missing everything a browser expects.
Add -s (--standalone) and pandoc wraps the output in a proper header and footer:
pandoc notes.md -o notes.html # fragment
pandoc -s notes.md -o notes.html # complete documentPandoc assumes Markdown input and HTML output, so -f markdown -t html is optional.
2. Add a title, a stylesheet and a table of contents
A standalone page needs a title, or pandoc warns about a missing one. Set it from the command line with -M (--metadata), which overrides any title in the file's YAML block. Link a stylesheet with -c, which the manual says should be used together with -s. Add --toc for a table of contents built from your headings (it only works with -s, and --toc-depth limits how deep it goes).
pandoc -s notes.md -o notes.html \
-M title="Release notes" \
-c style.css \
--tocYour style.css can be ten lines. Pandoc doesn't impose a theme, and that's the point: the whole look is yours.
3. Ship one self-contained file
A page that links style.css breaks when you email only the HTML. The --embed-resources flag fixes that. It inlines linked stylesheets, scripts and images as data: URIs, so you get a single file that works anywhere. It only applies to HTML output.
pandoc -s notes.md -o notes.html -c style.css --embed-resources4. Pick a Markdown flavor
Markdown is not one language. Pandoc's default markdown input has its own extensions. Use -f commonmark for strict CommonMark, -f gfm for GitHub-style Markdown, or -f commonmark_x for CommonMark plus extensions. To see exactly what a format switches on, run pandoc --list-extensions=gfm.
pandoc -s -f gfm notes.md -o notes.html5. No pandoc? Use the original Markdown.pl
The converter that started it all is still around. John Gruber's Markdown.pl is a Perl script (version 1.0.1, released in December 2004) that turns plain text into HTML. It needs Perl 5.6.0 or later and emits XHTML-style tags like <br /> unless you pass --html4tags:
perl Markdown.pl --html4tags notes.md > fragment.htmlIt outputs fragments only, so you wrap the result in your own page. It also predates the CommonMark spec, so expect differences from modern Markdown.
Common problems and fixes
The same file gives different HTML in different tools
Gruber's syntax description leaves questions open, such as how far a sublist must be indented, and the CommonMark spec notes that implementations answered them differently. If output changes between tools, pin the flavor with -f commonmark or -f gfm. For the backstory, see how Markdown got a spec.
Raw HTML passes straight through
CommonMark says HTML blocks are not escaped in HTML output. That's fine for your own notes. For Markdown written by someone else, run the result through an HTML sanitizer before you publish it.
Code blocks look plain
A fenced block with a language, such as ruby, becomes a pre element containing a code element with the class language-ruby. Pandoc also colors code by default; --syntax-highlighting switches between styles such as pygments, kate and monochrome.
You have more than one file
A shell loop covers a folder of notes without any generator:
for f in *.md; do
pandoc -s "$f" -c style.css -o "${f%.md}.html"
doneThe same tool also writes other formats, as in turning Markdown notes into an EPUB or building Markdown slides with Marp and Reveal.js. If you need navigation, feeds and templates across hundreds of pages, that is the point where a site generator earns its place.
Doing this with Carets
Carets is a fast, native notes and code editor for iPhone, iPad and Mac, and it fits this workflow at the source-file end. Your Markdown stays plain text, so pandoc, Markdown.pl or any other converter can read it later without an export step.
Three features matter here:
- Markdown, plain text and code in one editor. Write the page, the
style.cssand the build script side by side. - Syntax highlighting. The shell commands, CSS and YAML front matter in this article are easier to check when they are colored.
- Files, projects and tags. Keep each page's Markdown, stylesheet and notes in one project instead of scattered folders.
Carets is built natively for iOS and Mac rather than as a web wrapper, so opening a file to fix a typo before you rebuild takes a moment. Get Carets on the App Store for iPhone, iPad and Mac.
Frequently Asked Questions
How do I convert Markdown to HTML from the command line?
Run pandoc -s -o page.html page.md. Pandoc assumes Markdown input and HTML output by default, and -s makes it a complete document instead of a fragment.
Why does my converted HTML have no head or body tags?
Pandoc outputs a fragment unless you pass -s or --standalone. Add it, or wrap the fragment in your own template.
Can I produce a single HTML file with the CSS included?
Yes. Pandoc's --embed-resources inlines stylesheets, scripts and images as data: URIs, so the file works on its own.
Is it safe to convert Markdown I did not write?
Not without sanitizing. CommonMark passes raw HTML blocks through unescaped, so run untrusted input through an HTML sanitizer before you publish it.