All articles

GitHub Flavored Markdown cheat sheet

· 3 min read

Markdown is the format of READMEs, pull request descriptions, docs sites and chat messages. GitHub Flavored Markdown (GFM) is CommonMark plus a few extensions (tables, task lists, strikethrough and autolinks), and it's what most tools mean when they say "Markdown" today.

Headings

# Heading 1
## Heading 2
### Heading 3

Use one # heading per document and don't skip levels. GitHub generates an anchor for each heading: ## Getting Started becomes #getting-started.

Emphasis

Markdown Result
*italic* or _italic_ italic
**bold** bold
***bold italic*** bold italic
~~strikethrough~~ (GFM) strikethrough
`inline code` inline code

Underscores inside words don't create emphasis (snake_case_name stays as is), but asterisks do.

Lists

- Item
- Another item
  - Nested item (indent to the text of the parent)

1. First
2. Second
3. Third

Ordered lists use the first number as the start, so 1. on every line is fine and makes reordering easier.

Task lists (GFM)

- [x] Write the parser
- [ ] Add tests

On GitHub, task lists in issues and pull requests can be checked off by clicking.

[Link text](https://example.com)
[Link with title](https://example.com "Shown on hover")
[Relative link](docs/setup.md)
[Heading link](#installation)

![Alt text for the image](images/diagram.png)

[Reference-style link][docs]

[docs]: https://example.com/docs

GFM also turns plain URLs into links: https://example.com and www.example.com become clickable without brackets.

Code

Inline code uses backticks. For code containing a backtick, use two: `` a `b` c ``.

Code blocks use three backticks or tildes, followed by a language for syntax highlighting:

```js
const sum = (a, b) => a + b;
```

To show a fenced block inside a fenced block, use more backticks for the outer fence, as in the example above. Common language names: js, ts, jsx, json, bash, sh, python, sql, yaml, diff, html, css.

Tables (GFM)

| Option | Default | Description |
| :----- | :-----: | ----------: |
| `gfm`  | `true`  | Enable GFM  |
| `breaks` | `false` | Line breaks |
  • The header row and the separator row are required.
  • Colons in the separator set alignment: left :--, center :-:, right --:.
  • Columns don't need to line up in the source.
  • Write a pipe inside a cell as \|.
  • Cells can't contain block content like lists or code blocks; use <br> for line breaks inside a cell.

Blockquotes and alerts

> A quote can span
> multiple lines.
>
> > And be nested.

GitHub also renders alerts, a GitHub-specific extension:

> [!NOTE]
> Useful information.

> [!WARNING]
> Something that needs attention.

The types are NOTE, TIP, IMPORTANT, WARNING and CAUTION. Other Markdown renderers show them as plain blockquotes.

Line breaks and paragraphs

First line
same paragraph (rendered on the same line)

New paragraph after a blank line.

Line with a hard break\
next line

A trailing backslash or two trailing spaces force a line break. Two spaces are invisible in most editors and often removed by formatters, so the backslash is easier to maintain.

Horizontal rule

---

Leave a blank line above it. Directly under a line of text, --- turns that text into a heading instead.

Escaping

Precede any of these characters with a backslash to show it literally:

\ ` * _ { } [ ] ( ) # + - . ! |
1986\. A great year    → prevents a numbered list
\# Not a heading

HTML in Markdown

Markdown passes inline and block HTML through, which helps with things Markdown can't express:

<details>
<summary>Show the full log</summary>

Markdown content works here after a blank line.

</details>

GitHub sanitizes the HTML it renders, removing <script>, <style>, event handlers and most attributes. A library like marked doesn't: it converts Markdown and leaves raw HTML in the output. When rendering Markdown from users on your own site, sanitize the result with a library like DOMPurify, or disable raw HTML. Escaping HTML explains the risk.

Footnotes

GitHub supports footnotes in Markdown files, issues and pull requests:

Markdown was created in 2004.[^1]

[^1]: By John Gruber, with Aaron Swartz.

Support in other tools varies.

Rendering Markdown in JavaScript

import { marked } from "marked";
import DOMPurify from "dompurify";

const html = DOMPurify.sanitize(marked.parse(markdown, { gfm: true, breaks: false }));

gfm: true is marked's default; breaks: true turns single newlines into <br>, matching GitHub comments. Going the other direction, from existing HTML pages to Markdown, is covered in converting HTML to Markdown.

Frequently asked questions

Why does my single line break disappear?

In Markdown files, a single newline is a soft break and renders as a space. End the line with two spaces or a backslash, or leave a blank line for a new paragraph. GitHub issues and comments are an exception: they show single line breaks.

How do I add a table of contents?

Markdown has no TOC syntax. GitHub shows an outline button for README files automatically; elsewhere, write a list of links to heading anchors like [Install](#install).

Is HTML allowed in Markdown?

Yes, Markdown passes HTML through. GitHub then sanitizes it and removes scripts, styles and most attributes. A Markdown library like marked doesn't sanitize, so untrusted input needs a sanitizer.

How do I write a literal asterisk or underscore?

Escape it with a backslash: \*not italic\*.

Further reading