Converting Markdown to HTML: Fragment, Document, or Minified
Markdown is the default writing format for developer documentation, README files, changelogs, and technical blogs. At some point every piece of Markdown needs to become HTML. The question is not _whether_ to convert, but _which output mode_ fits your use case.
Three output modes
Most Markdown-to-HTML converters offer three distinct outputs, and picking the wrong one creates unnecessary work downstream.
1. Fragment (innerHTML)
A fragment is a bare block of HTML with no <html>, <head>, or <body> wrapper. You get exactly the tags that correspond to your Markdown — paragraphs, headings, lists, code blocks — and nothing else.
<h2>Installation</h2>
<p>Run the following command:</p>
<pre><code class="language-bash">npm install my-lib</code></pre>
When to use fragments:
- Injecting content into an existing page (CMS, blog template, email builder)
- Storing rendered HTML in a database column
- Embedding inside a React component via
dangerouslySetInnerHTML
Fragments are the most common output mode because most apps already have a surrounding layout.
2. Full document
A full document wraps the fragment in a complete HTML5 skeleton:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Document</title>
</head>
<body>
<h2>Installation</h2>
<p>Run the following command:</p>
...
</body>
</html>
When to use full documents:
- Generating standalone HTML files for offline reading
- Creating printable documentation (add a
<style>block or link a CSS file) - Producing self-contained email templates where the provider expects a full document
3. Minified
Minified output strips all unnecessary whitespace, newlines, and optional closing tags. The result is a single long string that weighs 15-30% less than its pretty-printed counterpart.
<h2>Installation</h2><p>Run the following command:</p><pre><code class="language-bash">npm install my-lib</code></pre>
When to use minified output:
- Embedding HTML in JSON payloads (API responses, CMS content fields)
- Email HTML where every kilobyte matters for deliverability
- Static site generation pipelines where post-processing handles formatting
Markdown extensions worth knowing
Standard CommonMark covers paragraphs, headings, lists, links, images, code blocks, and emphasis. Most converters also support GitHub Flavored Markdown (GFM), which adds:
- Tables — pipe-delimited columns
- Task lists —
- [x] Donecheckboxes - Strikethrough —
~~deleted~~ - Autolinks — bare URLs become clickable
If your content uses any of these, make sure your converter has GFM enabled or the raw syntax will appear in the output.
XSS: the hidden danger
Converting user-supplied Markdown to HTML opens the door to cross-site scripting. Markdown allows inline HTML, which means a user can write:
Check this out: <img src=x onerror="alert('xss')">
That renders as executable HTML. If you display it on a page without sanitization, you have a vulnerability.
Mitigation strategies:
- Sanitize the output. Run the HTML through a sanitizer like DOMPurify before inserting it into the DOM. Strip
<script>, event handlers (onerror,onclick), andjavascript:URLs.
- Disable raw HTML in the parser. Many Markdown libraries have an option to escape or strip HTML tags in the source. This is the safest approach for user-generated content.
- Use an allowlist. Only permit specific tags (
<p>,<a>,<code>,<pre>,<ul>,<ol>,<li>,<em>,<strong>,<h1>-<h6>) and specific attributes (href,src,alt,class).
// Example: DOMPurify sanitization
import DOMPurify from 'dompurify';
const dirty = convertMarkdownToHtml(userInput);
const clean = DOMPurify.sanitize(dirty, {
ALLOWED_TAGS: ['p', 'a', 'code', 'pre', 'ul', 'ol', 'li', 'em', 'strong', 'h1', 'h2', 'h3'],
ALLOWED_ATTR: ['href', 'class'],
});
Syntax highlighting in code blocks
Fenced code blocks with a language identifier ( `js ) produce <code class="language-js">. The HTML alone does not add colors — you need a client-side highlighter like Prism or highlight.js, or you can run a server-side highlighter during the build step to emit pre-colored <span> elements.
For static sites, server-side highlighting is better because it works without JavaScript on the client. For dynamic content, lazy-load a highlighter after the page renders.
Practical workflow
A common pipeline for technical documentation looks like this:
- Authors write in Markdown (version-controlled in Git)
- A build step converts to HTML fragments
- Fragments are injected into page templates
- Output is minified for production
- Syntax highlighting is applied at build time
This keeps the source readable, the output fast, and the pipeline reproducible.
Try our Markdown to HTML Converter to convert your Markdown instantly — right in your browser, no upload required.