Markdown Guide: CommonMark, GitHub Flavored Markdown (GFM), and Syntax
A complete developer guide to Markdown: CommonMark standards, GitHub Flavored Markdown (GFM) tables, task lists, code blocks, and AST rendering.
1. The Evolution of Markdown: From Gruber's Spec to CommonMark
Created in 2004 by John Gruber and Aaron Swartz, Markdown was designed to allow writers to compose rich text using an easy-to-read, easy-to-write plain text format that converts cleanly into valid HTML. As Markdown became the default format for technical documentation, README files, and static site generators, ambiguous edge cases in the original syntax led to the development of CommonMark—a rigorous, unambiguous standard.
Today, CommonMark and GitHub Flavored Markdown (GFM) are supported by virtually every code editor, chat platform (Discord, Slack), documentation generator (Astro, Next.js, VitePress), and issue tracker.
2. Essential Syntax: Headings, Emphasis, Blockquotes, and Lists
- Headings: Prefix lines with # for H1 through ###### for H6. Always leave a blank line before and after headings to ensure standard parsers recognize them.
- Emphasis: Wrap text in *italic* or **bold**, or combine them with ***bold italic***.
- Blockquotes: Prefix lines with > for callouts and quotes. You can nest blockquotes with >>.
- Lists: Use 1. 2. 3. for ordered lists and - or * for unordered lists. Indent sub-lists with 2 or 4 spaces.
3. Code Blocks: Inline Backticks vs Fenced Blocks
Use single backticks for inline code `const x = 10;`. For multi-line code blocks, use triple backticks (```) followed by the language identifier (````javascript, ````python, ````bash) to enable syntax highlighting in modern renderers.
Fenced code blocks eliminate the old, confusing 4-space indentation rule from legacy Markdown and allow specifying line highlighting, diff blocks, and copy-to-clipboard buttons.
4. GitHub Flavored Markdown (GFM) Extensions
GFM adds essential features for software engineering:
- Tables: Created using pipes | and hyphens - with optional colon alignment (:--- left, :---: center, ---: right).
- Task Lists: - [x] Completed task and - [ ] Pending task.
- Strikethrough: Wrap text in ~~strikethrough~~.
- Autolinks: URLs like https://webutil.tech are automatically turned into clickable links without explicit anchor tags.
5. Safe HTML Rendering and AST Processing with Unified / Remark
When rendering user-generated Markdown in web applications, always sanitize the output HTML using libraries like DOMPurify or sanitize-html to prevent malicious XSS attacks from injected <script> or onload handlers:
```javascript import { unified } from 'unified'; import remarkParse from 'remark-parse'; import remarkHtml from 'remark-html';
const file = await unified() .use(remarkParse) .use(remarkHtml, { sanitize: true }) .process('# Hello World'); ```
6. Real-Time In-Browser Markdown Preview with WebUtil
WebUtil's Markdown Preview tool provides instant split-screen editing and live HTML rendering with full GFM support running 100% locally in your browser. It includes word counting, copy-as-HTML, and export options.
Recommended Articles & Guides
How to Format JSON Properly: A Complete Guide
Learn how to format JSON data correctly with best practices, common patterns, and troubleshooting tips for developers.
Common JSON Errors and How to Fix Them
A comprehensive guide to the most frequent JSON errors developers encounter and step-by-step solutions to fix them.
URL Encoding and Decoding: What Every Developer Should Know
Understand how URL encoding works, why it matters for web development, and how to properly encode and decode URLs.