How to Convert Markdown to HTML Safely: CommonMark vs GFM

To convert Markdown to HTML, run it through a renderer that follows a specification: CommonMark for core Markdown, or GitHub Flavored Markdown (GFM) if you need tables, task lists, strikethrough and bare-URL links. If anyone other than you can write the Markdown, sanitize the resulting HTML with an allowlist sanitizer such as DOMPurify or nh3, or use the renderer’s safe mode. Markdown allows raw HTML, so rendering alone does not make untrusted input safe to display.

To check how a draft’s headings, emphasis and lists look before publishing, the FindPicked Markdown preview renders a basic subset in your browser. This guide covers the full conversion: which flavor to choose, which renderer does what, and how to sanitize. Every output below comes from renderers we ran on the same test file on November 17, 2026.

The short version

  1. Choose the flavor. Plain CommonMark for portable documents; GFM if your writers expect tables, task lists and ~~strikethrough~~.
  2. Choose a maintained renderer that states which flavor it follows, and pin its version.
  3. Decide whether raw HTML is allowed. If not, use a safe mode. If it is, sanitize every time.
  4. Sanitize after rendering, never before, and do not edit the HTML afterwards.
  5. Test with a hostile sample like the one below before going live.

CommonMark vs GitHub Flavored Markdown

The original Markdown description did not specify the syntax unambiguously, and as the CommonMark spec puts it, “implementations have diverged considerably”. CommonMark (version 0.31.2, January 2024) defines the syntax precisely, with 652 numbered examples you can test a renderer against.

GFM (version 0.29-gfm) is “a strict superset of CommonMark”. It adds five extensions:

Extension Markdown HTML in GFM
Tables Rows of cells separated by | with a --- line under the header <table>
Strikethrough ~~two~~ <del>two</del>
Task list items - [x] Write tests A disabled checkbox <input>
Extended autolinks www.example.com with no brackets A link with http:// added
Disallowed raw HTML Nine tags such as <script>, <style>, <iframe> The leading < is escaped as &lt;

Everything else is CommonMark. So the practical question is simple: if your writers use tables or checkboxes, you need a GFM renderer, or those parts will appear as literal text.

What four renderers did with the same file

We rendered this file, sample.md, in seven configurations of four libraries. It mixes GFM features with three classic injection attempts:

# Release notes

Fixed ~~two~~ three bugs. Details: www.example.com

| Feature | Status |
|---------|--------|
| Tables  | GFM only |

- [x] Write tests
- [ ] Update docs

<b>Raw HTML</b> and <img src="x" onerror="alert(1)">

[Click me](javascript:alert(1))

<script>alert("hi")</script>
Renderer and mode Table, strike, tasks www link <img onerror> javascript: link <script>
commonmark.js 0.31.2, default Plain text No Passed through Passed through Passed through
commonmark.js, safe: true Plain text No Removed href removed Removed
markdown-it 15.0.3, default preset Table and strike only No Escaped as text Not linked Escaped as text
markdown-it, commonmark preset Plain text No Passed through Not linked Passed through
marked 18.2.1, default (GFM) All three Yes Passed through Passed through Passed through
cmark-gfm (Python cmarkgfm), default All three Yes Removed href emptied Removed
cmark-gfm with CMARK_OPT_UNSAFE All three Yes Passed through Passed through Escaped by the tag filter

Three lessons come out of this table.

Defaults differ widely. marked rendered every GFM feature and also passed every attack through. Its README says plainly: “Marked does not sanitize the output HTML.” The commonmark.js README gives the same warning and points to its safe option.

“CommonMark compliant” is not “safe”. The CommonMark spec says raw HTML “will be rendered in HTML without escaping”. markdown-it’s default preset turns raw HTML off and is described as “safe by default”, but its commonmark preset turns raw HTML back on. Its link validator still refused the javascript: URL, because the library blocks javascript:, vbscript:, file: and most data: links by default.

The GFM tag filter is not a sanitizer. With raw HTML enabled, cmark-gfm, GitHub’s own C implementation, escaped the <script> tag but let the onerror handler and the javascript: link through. The GFM spec notes that GitHub.com performs “additional post-processing and sanitization” after rendering. Your site has to do the same.

How to sanitize untrusted Markdown

If you do not need raw HTML, the simplest option is a safe mode: commonmark.js with safe: true, markdown-it’s default preset, or cmark-gfm without the unsafe option. If you do need it, render first and then sanitize the HTML with an allowlist sanitizer. OWASP’s XSS prevention cheat sheet recommends DOMPurify for this.

JavaScript (Node). This is the script we ran with marked and DOMPurify 3.4.18. On the server, DOMPurify needs a DOM implementation such as jsdom; in a browser, call DOMPurify.sanitize() directly:

import fs from 'node:fs';
import { marked } from 'marked';
import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';

const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);

const markdown = fs.readFileSync('sample.md', 'utf8');
const unsafeHtml = marked.parse(markdown);
const safeHtml = DOMPurify.sanitize(unsafeHtml);
console.log(safeHtml);

The table, strikethrough, checkboxes and link to www.example.com survived. These lines changed:

<p><b>Raw HTML</b> and <img src="x"></p>
<p><a>Click me</a></p>

The onerror attribute and the javascript: URL were removed, and the <script> element was dropped entirely.

Python. nh3 is a binding to the Rust sanitizer ammonia. We paired it with cmarkgfm, the Python binding to cmark-gfm:

import cmarkgfm
import nh3
from cmarkgfm.cmark import Options

markdown = open("sample.md", encoding="utf-8").read()
unsafe_html = cmarkgfm.github_flavored_markdown_to_html(markdown, options=Options.CMARK_OPT_UNSAFE)
print(nh3.clean(unsafe_html))

With nh3 0.3.8, the onerror handler and the javascript: URL were removed, the <script> tag that the GFM filter had already escaped stayed as inert text, and links gained rel="noopener noreferrer". One difference: nh3’s default allowlist does not include <input>, so the task-list checkboxes disappeared and only their text remained. To keep them, extend the allowlist:

attrs = {tag: set(names) for tag, names in nh3.ALLOWED_ATTRIBUTES.items()}
attrs["input"] = {"type", "checked", "disabled"}
safe_html = nh3.clean(unsafe_html, tags=nh3.ALLOWED_TAGS | {"input"}, attributes=attrs)

That is the general pattern with allowlist sanitizers: start strict, then add only the tags and attributes your content needs.

Rules that keep sanitizing effective:

  • Sanitize the rendered HTML, not the Markdown. Links and images only exist after rendering.
  • Do not change the HTML afterwards. OWASP warns that modifying sanitized content, or passing it to a library that mutates it, can void the protection.
  • Keep the sanitizer updated. OWASP says you must regularly patch DOMPurify and similar libraries.
  • Sanitize every time you render, including when stored Markdown is rendered again later with a newer renderer.

Where the FindPicked Markdown preview fits

The Markdown preview is a deliberately small renderer. We ran its code on the same sample.md. It handles headings up to level 3, bold, italic, inline code and simple bulleted or numbered lists. It escapes the entire input before adding its own fixed set of tags, so the <img>, the javascript: link and the <script> all appeared as harmless text. Tables, links and images are not rendered.

It also differs from CommonMark in two visible ways. A single line break inside a paragraph becomes <br> in the preview, while the CommonMark spec treats it as a soft break that browsers display as a space. And raw HTML is always shown as text, where a CommonMark renderer would pass it through. The Copy HTML button copies the preview’s HTML, so the tool suits quick structure checks and simple snippets, not full document conversion. For a step-by-step pre-publishing routine, see preview a Markdown draft before publishing. To check a renderer you are considering against the reference implementation, paste your test file into the CommonMark dingus.

How we tested

On November 17, 2026, on macOS with Node 24.16 and Python 3.14.7, we ran commonmark.js 0.31.2, markdown-it 15.0.3, marked 18.2.1, cmarkgfm 2025.10.22, DOMPurify 3.4.18 with jsdom 31.0.1, and nh3 0.3.8 on the sample.md shown above. For the FindPicked preview, we ran the rendering function from the tool’s page unchanged. The tables describe the HTML each renderer produced; we did not test every renderer option or plugin, and defaults can change between versions.

Frequently Asked Questions

What is the difference between CommonMark and GitHub Flavored Markdown?

CommonMark is a precise specification of core Markdown. GitHub Flavored Markdown (GFM) is a strict superset that adds tables, task lists, strikethrough, extended autolinks and a filter for a few HTML tags. A renderer that supports only CommonMark leaves GFM tables and checkboxes as plain text.

Is it safe to render Markdown written by other people?

Only if you also sanitize the output or use a safe mode. CommonMark passes raw HTML through without escaping. In our tests, commonmark.js and marked with default settings let an image with an onerror handler and a javascript: link through unchanged.

Should I sanitize the Markdown or the HTML?

Sanitize the HTML that the renderer produces. Markdown syntax creates links and images that only exist after rendering, so a filter that runs on the Markdown text cannot judge them reliably. Do not modify the HTML again after sanitizing it.

Does the GFM tag filter make HTML safe?

No. It escapes only nine tags, such as script, style and iframe. In our cmark-gfm test with raw HTML enabled, an image with an onerror handler and a javascript: link still came through. GitHub itself runs additional sanitization after rendering.