Markdown Syntax Guide: From Basic to GitHub-Flavored Markdown
Markdown is the universal format for READMEs, docs, and notes. This complete reference covers headings, lists, code blocks, tables, and GitHub-Flavored Markdown (GFM) extensions.
Markdown, CommonMark and GFM
John Gruber published Markdown in 2004 as a Perl script plus an informal syntax description. The description left many cases open (how far a nested list must be indented, whether underscores inside words create emphasis), so every parser filled the gaps differently. The same file could render one way on GitHub and another way in a static site generator.
Today there are three names worth knowing:
| Name | What it is |
|---|---|
| Original Markdown | Gruber's 2004 syntax page and Markdown.pl. Ambiguous by modern standards; few tools follow it literally. |
| CommonMark | A precise specification with hundreds of test examples that resolves those ambiguities. Most modern parsers (markdown-it, marked, commonmark.js, cmark) follow it. |
| GitHub Flavored Markdown (GFM) | A formal superset of CommonMark published by GitHub. It adds exactly five extensions: tables, task list items, strikethrough, extended autolinks and a filter for some raw HTML tags. |
Some features people call "GFM" are not in that spec: footnotes, alerts ([!NOTE]), Mermaid diagrams and math blocks are rendered by github.com but are separate GitHub features. If your README depends on them, expect plain text on npm, PyPI or a self-hosted docs site that only implements the GFM spec.
Core syntax in one table
| Markdown | Renders as |
|---|---|
| # Title … ###### Title | <h1> to <h6>. A space after the # is required: #Title is a paragraph. |
| *italic* or _italic_ | <em>italic</em> |
| **bold** or __bold__ | <strong>bold</strong> |
| `code` | <code>code</code>; use ``a ` b`` (double backticks) when the code contains a backtick |
| [text](https://example.com "title") | A link with an optional title |
|  | An image; the alt text is what screen readers announce |
| > quote | <blockquote> |
| - item / * item / + item | Unordered list |
| 1. item | Ordered list; only the first number matters |
| --- or *** or ___ | <hr> (a --- directly under a line of text makes that line an <h2> instead) |
| line ⏎ (two trailing spaces) or line\⏎ | <br> hard line break |
A single newline inside a paragraph is not a line break in CommonMark: the two lines are joined with a space. This is why a poem or an address pasted into a README collapses into one line. End the line with a backslash (more visible than two spaces, which editors often strip), or separate the lines with a blank line to make new paragraphs. Some renderers, such as GitHub issue comments, treat every newline as a break, so the same text can look different in a comment and in a .md file.
Six rendering surprises (and the fix)
Each example below shows the source and the HTML a CommonMark/GFM parser produces. They were checked with the marked library, which the StackUtils Markdown tools use.
1. A sentence that starts with a number becomes a list
2026. A great year for us. → <ol start="2026"><li>A great year for us.</li></ol> 2026\. A great year for us. → <p>2026. A great year for us.</p>
Any line starting with digits followed by . or ) and a space can open an ordered list. Escape the dot with a backslash.
2. Ordered lists ignore your numbering
1. one 3. three 1. two 4. four 1. three → <ol> 1, 2, 3 → <ol start="3"> 3, 4
Only the first number is used, as the list's start attribute. Writing 1. on every line is a common convention because reordering items then never produces a noisy diff.
3. Nested lists that do not nest
1. First - nested? (2 spaces) → <ol><li>First</li></ol><ul><li>nested?</li></ul> 1. First - nested (3 spaces: aligned with the text after "1. ") → <ol><li>First<ul><li>nested</li></ul></li></ol>
In CommonMark a child block must be indented to the column where the parent item's content starts. For - item that is 2 spaces; for 1. item it is 3; for 10. item it is 4. The "always use 4 spaces" rule from older guides comes from the original Markdown.pl and is not what CommonMark parsers check.
4. Underscores inside words
Call my_long_function_name() → <p>Call my_long_function_name()</p> (no emphasis) _snake_case_ and __init__ → <em>snake_case</em> and <strong>init</strong>
CommonMark does not create emphasis from underscores in the middle of a word, but an identifier that starts and ends with underscores still turns into italics or bold. Put identifiers in backticks; that also makes them easier to copy.
5. Pipes inside table cells
| Operator | Meaning | |----------|---------| | `a|b` | or | ← broken: cell ends at the pipe, even inside backticks | `a\|b` | or | ← correct: escape the pipe
The GFM spec splits table rows on unescaped pipes before inline parsing, so a pipe inside a code span still needs a backslash. Cells beyond the header's column count are silently dropped, which makes this bug easy to miss.
6. Markdown inside HTML blocks
<details> <summary>More</summary> **not bold** </details> <details> <summary>More</summary> **bold** ← blank lines around it: parsed as Markdown </details>
An HTML block continues until the next blank line, and its content is passed through as raw HTML. Leave a blank line after the opening tags to switch back to Markdown. This is the standard trick for collapsible sections in READMEs.
Code blocks
Fenced blocks start with three or more backticks (or tildes) and an optional info string that most renderers use for syntax highlighting. To show a fence inside a fence, use a longer outer fence:
````markdown
```js
console.log("inner fence shown literally");
```
````Indented code (four spaces) also works but cannot carry a language tag and is easy to trigger by accident inside lists. Prefer fences. Language names that GitHub recognises include js, ts, bash, json, yaml, sql and diff; a diff block colours lines that start with + and -, which is handy for documenting changes.
GFM extensions and GitHub-only features
| Column | Centered | Right | |:-------|:--------:|------:| | a | b | c | - [x] Write the parser - [ ] Write the docs ~~deprecated~~ option Visit www.example.com ← extended autolink, no <> needed
Tables need a header row and a delimiter row; colons set alignment. Columns do not have to line up in the source, which is convenient but makes wide tables hard to edit by hand. Generating them from data is easier: export a sheet to CSV and convert it, or paste an HTML table into the HTML to Markdown converter.
The following render on github.com but are outside the GFM spec:
Text with a footnote.[^1] [^1]: The footnote body, rendered at the bottom of the page. > [!WARNING] > Alerts support NOTE, TIP, IMPORTANT, WARNING and CAUTION.
Heading links and tables of contents
GitHub gives every heading an id: lowercased, spaces replaced by hyphens, most punctuation removed, and a -1, -2 suffix for duplicates. So ## What's new in v2.0? becomes #whats-new-in-v20. A table of contents is just a list of links to those ids:
- [Installation](#installation) - [What's new in v2.0?](#whats-new-in-v20)
Other renderers generate ids differently (or not at all), so test anchor links on the platform where the document will be read.
Rendering Markdown safely
CommonMark allows raw HTML, and most parsers pass it through unchanged. If you render Markdown written by users (comments, profiles, issue text), the output can contain <script> tags, onerror attributes or javascript: links. Parsers such as marked do not sanitize. Run the generated HTML through a sanitizer like DOMPurify before inserting it into the page, or disable raw HTML in the parser if your use case does not need it.
A README structure that works
- Name and one-sentence description. The first paragraph is what people see in search results and link previews.
- Install and quick start. The commands needed to go from clone to a running example, in fenced
bashblocks. - Usage. One realistic example with its output, not a list of every option.
- Configuration. A table of options, defaults and accepted values.
- Contributing and licence. Link to
CONTRIBUTING.mdand name the licence.
The README generator fills in this skeleton from a form, and the licence generator produces the matching LICENSE file. Preview the result before pushing: the Markdown preview renders as you type, which makes the nesting and table problems above visible immediately.
Preview and convert Markdown
Check how a document renders, export it as HTML, or turn existing HTML back into Markdown. Everything runs in your browser.