Skip to content

Instantly share code, notes, and snippets.

@v-amorim
Last active August 12, 2024 23:04
Show Gist options
  • Save v-amorim/bf1bcb8bee4e0455fc5b8019be876794 to your computer and use it in GitHub Desktop.
Save v-amorim/bf1bcb8bee4e0455fc5b8019be876794 to your computer and use it in GitHub Desktop.
Markdown Cheatsheet

Markdown Cheatsheet

This is my definitive Markdown cheatsheet. It includes all the Markdown syntax you need to know to create beautiful and professional-looking documents.

Styling text 1

Style Syntax char|textchar Keyboard shortcut Example Output
Heading #|text
##|text
###|text
####|text
#####|text
######|text
None
# A first-level heading
## A second-level heading
### A third-level heading
#### A fourth-level heading
##### A fifth-level heading
###### A sixth-level heading

A first-level heading

A second-level heading

A third-level heading

A fourth-level heading

A fifth-level heading
A sixth-level heading
Horizontal Rule --- or *** None
---
Text between Horizontal Rules
---

Text between Horizontal Rules
Bold **|text** or __|text__ Ctrl+B
**This is bold text**
This is bold text
Italic *|text* or _|text_ Ctrl+I
_This text is italicized_
This text is italicized
Strikethrough ~~|text~~ None
~~This was mistaken text~~
This was mistaken text
Quote >|quote Ctrl+Shift+.
> Text that is a quote
Text that is a quote
Inline code quote `|code` Ctrl+E
This has an `inline quote code` text
This has an inline quote code text
Inline math quote $ equation $ None
$\sqrt{3x-1}+(1+x)^2$
3x-1+(1+x)2
Block code quote ```
|multi-lined-code
```
Ctrl+E
This has a
```
Block
quote
code
```
This has a
Block
quote
code
Block math quote $$
|equation
$$
or ```math
|equation
```
None
$$
f(x) = x^2
$$
f(x)=x2
Bold and nested italic **|bold_|italic_|bold** None
**This text is _extremely_ important**
This text is extremely important
All bold and italic **_|bold-italic_** None
**_All this text is important_**
All this text is important
Subscript <sub>|text</sub> None
This is a <sub>subscript</sub> text
This is a subscript text
Superscript <sup>|text</sup> None
This is a <sup>superscript</sup> text
This is a superscript text
Sub+Superscript <sup><sub>|text</sub></sup> None
<sup><sub>This is a very small text</sub></sup>
This is a very small text
Hidden Comment <!--|hidden comment--> None
<!--This is a hidden comment-->
Unordered List - |item
* |item
+ |item
Ctrl+Shift+8
- Unordered List Item
* Unordered List Item
+ Unordered List Item
  • Unordered List Item
  • Unordered List Item
  • Unordered List Item
Ordered List 1. |item Ctrl+Shift+7
1. Ordered List Item
1. Ordered List Item
1. Ordered List Item
  1. Ordered List Item
  2. Ordered List Item
  3. Ordered List Item
Task List - [ ] |item None
- [ ] Task List Item
- [x] Task List Item
Task List Item
Task List Item
Nested List Combine any List syntax None
1. First list item
   - First nested list item
     - Second nested list item
  1. First list item
    • First nested list item
      • Second nested list item
Inline Link [|text](|url "|hover") None
[GitHub Pages](https://pages.github.com/ "hover info")
GitHub Pages
Inline Image Link ![alt-text](|url "|hover") None
![](https://picsum.photos/100/100)
Reference Link [ref_tag]: |url "|hover" None
[git_page]: https://pages.github.com/ "hover info"
Inline Link with Ref. [|text][ref_tag] None
[GitHub Pages][git_page]
GitHub Pages
Inline Image Link with Ref. ![alt-text][ref_tag] None
![][100x100]
Table | Header 1 | Header 2 |
| --- | --- |
| Cell A1 | Cell A2 |
| Cell B1 | Cell B2 |
None
| Header 1 | Header 2 |
| --- | --- |
| Cell A1 | Cell A2 |
| Cell B1 | Cell B2 |
Header 1Header 2
Cell A1Cell A2
Cell B1Cell B2
Footnote [^footnote]: |text-and-or-link</kbd None
[^footnote]This is a footnote
This has a footnote. 2
Collapsible Section <details>
<summary>|summary</summary>

|text
</details>
None
<details>
<summary>Click to expand</summary>
This is hidden
</details>
Click to expandThis is hidden
Text Box <table>
<td>
|text
</td></table>
None
<table>
<td>
This is text in the box
</td></table>
This is text in the box

Alerts 3

> [!NOTE]
> Highlights information that users should take into account, even when skimming.

> [!TIP]
> Optional information to help a user be more successful.

> [!IMPORTANT]
> Crucial information necessary for users to succeed.

> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.

> [!CAUTION]
> Negative potential consequences of an action.

Note

Highlights information that users should take into account, even when skimming.

Tip

Optional information to help a user be more successful.

Important

Crucial information necessary for users to succeed.

Warning

Critical content demanding immediate user attention due to potential risks.

Caution

Negative potential consequences of an action.

Color Preview 4

Only in issues, pull requests and discussions, you can call out colors within a sentence by using backticks. A supported color model within backticks will display a visualization of the color.

Color Syntax Example Output
HEX #RRGGBB
#0969DA
RGB rgb(R,G,B)
rgb(9, 105, 218)
HSL hsl(H,S,L)
hsl(212, 92%, 45%)

Relative Links

Relative links and image paths help readers navigate your repository. A relative link points to another file relative to the current file's location. For example, a link in your root README to a file in docs/CONTRIBUTING.md would look like:

[Contribution guidelines for this project](docs/CONTRIBUTING.md)

GitHub automatically adjusts relative links and paths based on the current branch, ensuring they always work. Links starting with / are relative to the repository root. You can use relative link operands like ./ and ../.

Examples of relative links:

  • In a .md file on the same branch /assets/images/electrocat.png
  • In a .md file on another branch /../main/assets/images/electrocat.png
  • In issues, pull requests and comments of the repository ../blob/main/assets/images/electrocat.png?raw=true
  • In a .md file in another repository /../../../../github/docs/blob/main/assets/images/electrocat.png
  • In issues, pull requests and comments of another repository ../../../github/docs/blob/main/assets/images/electrocat.png?raw=true

Link a Label 5

You can reference labels in markdown using the following syntax:

https://github.com/account/repo/labels/label-name

Light/Dark mode images

Swap out images based on theme settings. 6

Add: #gh-dark-mode-only or #gh-light-mode-only to the end of the image path to specify which theme the image should be displayed in.

![Logo](./dark.png#gh-dark-mode-only)
![Logo](./light.png#gh-light-mode-only)

You can now specify whether to display images for light or dark themes in Markdown, using the HTML <picture> element in combination with the prefers-color-scheme media feature. 7

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/25423296/163456776-7f95b81a-f1ed-45f7-b7ab-8fa810d529fa.png">
  <img alt="Shows an illustrated sun in light color mode and a moon with stars in dark color mode." src="https://user-images.githubusercontent.com/25423296/163456779-a8556205-d0a5-45e2-ac17-42d089e3c3f8.png">
</picture>

Footnotes 8

You can add footnotes to your content by using this bracket syntax:

[^one_line]Here is a simple footnote.

A footnote can also have multiple[^multi_line] lines.

Can also reference to a link. [^link]

[^one_line]: My reference.
[^multi_line]:
    To add line breaks within a footnote, prefix new lines with 2 spaces.
This is a second line. [^link]: Example with URL: https://github.com

9Here is a simple footnote.

A footnote can also have multiple10 lines.

Can also reference to a link. 11

Collapsible Sections

Collapsing large blocks of text can make your markdown much easier to digest

<details>
<summary>To make sure markdown is rendered correctly in the collapsed section...</summary>

1.  Put an **empty line** after the `<summary>` block.
2.  _Insert your markdown syntax_
3.  Put an **empty line** before the `</details>` tag

</details>
To make sure markdown is rendered correctly in the collapsed section...
  1. Put an empty line after the <summary> block.
  2. Insert your markdown syntax
  3. Put an empty line before the </details> tag

Badges

[python_badge]: https://img.shields.io/badge/Python-informational?logo=python&style=flat&logoColor=79dafa&labelColor=282a36&color=ff6e96
[autohotkey_badge]: https://img.shields.io/badge/Auto_Hotkey-informational?logo=autohotkey&style=flat&logoColor=79dafa&labelColor=282a36&color=ff6e96
[ruby_badge]: https://img.shields.io/badge/Ruby-informational?logo=ruby&style=flat&logoColor=79dafa&labelColor=282a36&color=5e4053

Nice looking file tree 12

Using the graphql syntax in block quotes will nicely highlight file trees like below:

./root/*
  ├─ assets/*   # Fonts, icons, images, etc.
  ├─ code/*     # Where the code lives
  │  ├─ main.py # The main file
  │  └─ Other files…
  └─ utils/*    # Utility functions

Text box

Add a box with contents to markdown

<table><td>

This is text in the `box`</td></table>

This is text in the box

<table><td align="center" width="1000">

This is text in the centered `box`</td></table>

This is text in the centered box

Advanced Table Tips 13

Creating a Table

Use pipes | and hyphens - to create tables. Hyphens define headers, and pipes separate columns. Ensure there's a blank line before the table for correct rendering.

| Header 1 | Header 2 |
| -------- | -------- |
| Cell A1  | Cell A2  |
| Cell B1  | Cell B2  |
Header 1 Header 2
Cell A1 Cell A2
Cell B1 Cell B2

Formatting Table Content

| Tool   | Purpose                          |
| ------ | -------------------------------- |
| `ls`   | Display _directory_ contents     |
| `grep` | Search for **specific** patterns |
Tool Purpose
ls Display directory contents
grep Search for specific patterns

Aligning Text

Align text left, center, or right using colons : in the header row.

| Left Align | Center Align | Right Align |
| :--------- | :----------: | ----------: |
| Item A     |    Item B    |      Item C |
| Item D     |    Item E    |      Item F |
Left Align Center Align Right Align
Item A Item B Item C
Item D Item E Item F

Images on tables

|                  First Image                   |                  Second Image                   |
| :--------------------------------------------: | :---------------------------------------------: |
| ![First Image](https://picsum.photos/1260/750) | ![Second Image](https://picsum.photos/1260/750) |
First Image Second Image
First Image Second Image

Table within a Table 14

|                         Image                          |                                                                                                                                                                                                                    Table Inside Table                                                                                                                                                                                                                     |
| :----------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| ![Heart On Your Sleeve](https://picsum.photos/150/150) | <table> <thead> <tr> <th>Header</th> <th>Example</th> <th>Example</th> </tr> </thead> <tbody> <tr> <td align="center"><img src="https://picsum.photos/150/150" width="150px"></td> <td><img src="https://picsum.photos/100/150" width="100px" align="center"></td> <td><img src="https://picsum.photos/75/75" width="75px"></td> </tr> <tr> <td align="center">Info</td> <td align="center">(?)</td> <td align="center">Info</td> </tr> </tbody> </table> |
Image Table Inside Table
Heart On Your Sleeve
Header Example Example
Info (?) Info

Advanced Formatting Tips 15

left alignment

This is the code you need to align images to the left:

<img align="left" width="100" height="100" src="https://picsum.photos/100/100">

right alignment

This is the code you need to align images to the right:

<img align="right" width="100" height="100" src="https://picsum.photos/100/100">

center alignment example

<p align="center">
  <img width="460" height="300" src="https://picsum.photos/460/300" />
</p>

Horizontal images no gap

via comment

<p>
  <img src="https://picsum.photos/100/100" />
  <img src="https://picsum.photos/100/100" />
</p>


Horizontal images with gap

With hspace property you can set horizontal (left and right) padding for an image

<p>
  <img src="https://picsum.photos/100/100" hspace="10" />
  <img src="https://picsum.photos/100/100" hspace="10" />
</p>


Vertical images with gap

We also have a property "vspace", which does what it sounds like, add vertical spacing. But it doesn't seem to work on GitHub, unlike VSCode's buit in markdown viewer. So probably just add a <p> tag in between.

<p>
  <img src="https://picsum.photos/500/100" />
</p>

<p>
  <img src="https://picsum.photos/500/100" />
</p>

<p>
  <img src="https://picsum.photos/500/100" />
</p>

Diagrams 16

GitHub now supports the following diagram types:

content type supported extensions
mermaid .mermaid, .mmd
geoJSON .geojson, .json
topoJSON .topojson, .json
STL .stl
Mermaid
Examples
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
Loading

sequenceDiagram
    Alice->>+John: Hello John, how are you?
    Alice->>+John: John, can you hear me?
    John-->>-Alice: Hi Alice, I can hear you!
    John-->>-Alice: I feel great!
Loading
Basics
flowchart LR
    id
Loading

flowchart LR
    id1[This is the text in the box]
Loading

flowchart LR
    id["This ❤ Unicode"]
Loading

%%{init: {"flowchart": {"htmlLabels": false}} }%%
flowchart LR
    markdown["`This **is** _Markdown_`"]
    newLines["`Line 1
               Line 2
               Line 3`"]
    markdown --> newLines

Loading

flowchart LR
    id1[This is the text in the box]
Loading
Direction

Possible FlowChart orientations are:

TB - Top to bottom
TD - Top-down/ same as top to bottom
BT - Bottom to top
RL - Right to left
LR - Left to right

flowchart TB
    Start --> Stop
Loading

flowchart TD
    Start --> Stop
Loading

flowchart BT
    Start --> Stop
Loading

flowchart RL
    Start --> Stop
Loading

flowchart LR
    Start --> Stop
Loading
Node shapes

A node with round edges

flowchart LR
    id1(This is the text in the box)
Loading

A stadium-shaped node

flowchart LR
    id1([This is the text in the box])
Loading

A node in a subroutine shape

flowchart LR
id1[[This is the text in the box]]
Loading

A node in a cylindrical shape

flowchart LR
id1[(Database)]
Loading

A node in the form of a circle

flowchart LR
id1((This is the text in the circle))
Loading

A node in an asymmetric shape

flowchart LR
id1>This is the text in the box]
Loading

A node (rhombus)

flowchart LR
id1{This is the text in the box}
Loading

A hexagon node

flowchart LR
id1{{This is the text in the box}}
Loading

Parallelogram

flowchart TD
id1[/This is the text in the box/]
Loading

Parallelogram alt

flowchart TD
id1[\This is the text in the box\]
Loading

Trapezoid

flowchart TD
A[/Christmas\]
Loading

Trapezoid alt

flowchart TD
B[\Go shopping/]
Loading

Double circle

flowchart TD
id1(((This is the text in the circle)))
Loading
Links between nodes

A link with arrow head

flowchart LR
    A-->B
Loading

An open link

flowchart LR
    A --- B
Loading

Text on links

flowchart LR
    A-- This is the text! ---B
Loading

or

flowchart LR
    A---|This is the text|B
Loading

A link with arrow head and text

flowchart LR
    A-->|text|B
Loading

or

flowchart LR
    A-- text -->B
Loading

Dotted link

flowchart LR
   A-.->B;
Loading

Dotted link with text

flowchart LR
   A-. text .-> B
Loading

Thick link

flowchart LR
   A ==> B
Loading

Thick link with text

flowchart LR
   A == text ==> B
Loading

An invisible link This can be a useful tool in some instances where you want to alter the default positioning of a node.

flowchart LR
    A ~~~ B
Loading

Chaining of links

flowchart LR
   A -- text --> B -- text2 --> C
Loading

It is also possible to declare multiple nodes links in the same line as per below:

flowchart LR
   a --> b & c--> d
Loading

You can then describe dependencies in a very expressive way. Like the one-liner below:

flowchart TB
    A & B--> C & D
Loading

If you describe the same diagram using the basic syntax, it will take four lines. A word of warning, one could go overboard with this making the flowchart harder to read in markdown form. The Swedish word lagom comes to mind. It means, not too much and not too little. This goes for expressive syntaxes as well.

flowchart TB
    A --> C
    A --> D
    B --> C
    B --> D
Loading
Arrow Types

Triangle edge

flowchart LR
   A --> B
Loading

Circle edge

flowchart LR
    A --o B
Loading

Cross edge

flowchart LR
    A --x B
Loading

Multi Directional edges

flowchart LR
    A o--o B
    B <--> C
    C x--x D

Loading
Link Lenght

Add more dashes to increase the length of the link

flowchart TD
    A[Start] --> B{Is it?}
    B -->|Yes| C[OK]
    C --> D[Rethink]
    D --> B
    B ---->|No| E[End]
Loading

Here are how you should add dashes to increase the length of the link:

Length 1 2 3
Normal --- ---- -----
Normal with arrow --> ---> ---->
Thick === ==== =====
Thick with arrow ==> ===> ====>
Dotted -.- -..- -...-
Dotted with arrow -.-> -..-> -...->
Subgraphs
subgraph title
graph definition
end

An example below:

flowchart TB
    c1-->a2
    subgraph one
    a1-->a2
    end
    subgraph two
    b1-->b2
    end
    subgraph three
    c1-->c2
    end
Loading

You can also set an explicit id for the subgraph.

flowchart TB
    c1-->a2
    subgraph ide1 [one]
    a1-->a2
    end
Loading

With the graphtype flowchart it is also possible to set edges to and from subgraphs as in the flowchart below.

flowchart TB
    c1-->a2
    subgraph one
    a1-->a2
    end
    subgraph two
    b1-->b2
    end
    subgraph three
    c1-->c2
    end
    one --> two
    three --> two
    two --> c2
Loading

With the graphtype flowcharts you can use the direction statement to set the direction which the subgraph will render like in this example.

  • If any of a subgraph's nodes are linked to the outside, subgraph direction will be ignored. Instead the subgraph will inherit the direction of the parent graph
flowchart LR
  subgraph TOP
    direction TB
    subgraph B1
        direction RL
        i1 -->f1
    end
    subgraph B2
        direction BT
        i2 -->f2
    end
  end
  A --> TOP --> B
  B1 --> B2
Loading
Formatting

Markdown Strings

The "Markdown Strings" feature enhances flowcharts and mind maps by offering a more versatile string type, which supports text formatting options such as bold and italics, and automatically wraps text within labels.

Formatting:

For bold text, use double asterisks (*) before and after the text.
For italics, use single asterisks (
) before and after the text.
With traditional strings, you needed to add
tags for text to wrap in nodes. However, markdown strings automatically wrap text when it becomes too long and allows you to start a new line by simply using a newline character instead of a
tag.
This feature is applicable to node labels, edge labels, and subgraph labels.

%%{init: {"flowchart": {"htmlLabels": false}} }%%
flowchart LR
subgraph "One"
  a("`The **cat**
  in the hat`") -- "edge label" --> b{{"`The **dog** in the hog`"}}
end
subgraph "`**Two**`"
  c("`The **cat**
  in the hat`") -- "`Bold **edge label**`" --> d("The dog in the hog")
end
Loading

Interaction

It is possible to bind a click event to a node, the click can lead to either a javascript callback or to a link which will be opened in a new browser tab.

flowchart LR
    A-->B
    B-->C
    C-->D
    click A callback "Tooltip for a callback"
    click B "https://www.github.com" "This is a tooltip for a link"
    click D href "https://www.github.com" "This is a tooltip for a link"
Loading
GeoJSON
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 1,
      "properties": {
        "ID": 0
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -90,
              35
            ],
            [
              -90,
              30
            ],
            [
              -85,
              30
            ],
            [
              -85,
              35
            ],
            [
              -90,
              35
            ]
          ]
        ]
      }
    }
  ]
}
Loading
TopoJSON
{
  "type": "Topology",
  "transform": {
    "scale": [
      0.0005000500050005,
      0.00010001000100010001
    ],
    "translate": [
      100,
      0
    ]
  },
  "objects": {
    "example": {
      "type": "GeometryCollection",
      "geometries": [
        {
          "type": "Point",
          "properties": {
            "prop0": "value0"
          },
          "coordinates": [
            4000,
            5000
          ]
        },
        {
          "type": "LineString",
          "properties": {
            "prop0": "value0",
            "prop1": 0
          },
          "arcs": [
            0
          ]
        },
        {
          "type": "Polygon",
          "properties": {
            "prop0": "value0",
            "prop1": {
              "this": "that"
            }
          },
          "arcs": [
            [
              1
            ]
          ]
        }
      ]
    }
  },
  "arcs": [
    [
      [
        4000,
        0
      ],
      [
        1999,
        9999
      ],
      [
        2000,
        -9999
      ],
      [
        2000,
        9999
      ]
    ],
    [
      [
        0,
        0
      ],
      [
        0,
        9999
      ],
      [
        2000,
        0
      ],
      [
        0,
        -9999
      ],
      [
        -2000,
        0
      ]
    ]
  ]
}
Loading
STL
solid cube_corner
  facet normal 0.0 -1.0 0.0
    outer loop
      vertex 0.0 0.0 0.0
      vertex 1.0 0.0 0.0
      vertex 0.0 0.0 1.0
    endloop
  endfacet
  facet normal 0.0 0.0 -1.0
    outer loop
      vertex 0.0 0.0 0.0
      vertex 0.0 1.0 0.0
      vertex 1.0 0.0 0.0
    endloop
  endfacet
  facet normal -1.0 0.0 0.0
    outer loop
      vertex 0.0 0.0 0.0
      vertex 0.0 0.0 1.0
      vertex 0.0 1.0 0.0
    endloop
  endfacet
  facet normal 0.577 0.577 0.577
    outer loop
      vertex 1.0 0.0 0.0
      vertex 0.0 1.0 0.0
      vertex 0.0 0.0 1.0
    endloop
  endfacet
endsolid
Loading

Footnotes

  1. https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/

  2. This is a footnote

  3. https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alertsbasic-writing-and-formatting-syntax

  4. https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#supported-color-models

  5. https://github.blog/changelog/2022-02-03-reference-labels-in-markdown/

  6. https://github.blog/changelog/2021-11-24-specify-theme-context-for-images-in-markdown/

  7. https://github.blog/changelog/2022-05-19-specify-theme-context-for-images-in-markdown-beta/

  8. https://github.blog/changelog/2021-09-30-footnotes-now-supported-in-markdown-fields/

  9. My reference.

  10. To add line breaks within a footnote, prefix new lines with 2 spaces.
    This is a second line.

  11. https://twitter.com/alexdotjs/status/1421015442286596100

  12. https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/organizing-information-with-tables

  13. https://github.com/drknzz/GitHub-Achievements

  14. https://github.com/DavidWells/advanced-markdown

  15. https://github.blog/changelog/2022-03-17-mermaid-topojson-geojson-and-ascii-stl-diagrams-are-now-supported-in-markdown-and-as-files/

@v-amorim
Copy link
Author

Color Preview

Only in issues, pull requests and discussions, you can call out colors within a sentence by using backticks. A supported color model within backticks will display a visualization of the color.

Color Syntax Example Output
HEX #RRGGBB
#0969DA
#0969DA
RGB rgb(R,G,B)
rgb(9, 105, 218)
rgb(9, 105, 218)
HSL hsl(H,S,L)
hsl(212, 92%, 45%)
hsl(212, 92%, 45%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment