Markdown is a lightweight, open-source, easy-to-read and easy-to-write method of formatting text that you can use as plain text in any IDE or editor.
When writing on GitHub, you can use Markdown syntax and HTML elements to extend Markdown’s functionality. You can use Markdown syntax everywhere in GitHub, such as in the README file, wiki, comments, pull requests, and when creating issues.
For every software developer, learning markdown is an essential step along the path of your career.
To enhance Markdown’s basic features, GitHub added some custom functionalities and created GitHub-Flavored Markdown. With this, you can easily interact with other users in pull requests and issues by mentioning user, issue, and PR references and adding emoji.
This tutorial teaches you the basics of GitHub-Flavored Markdown so you can start using it in your projects.
All the code is available in the GitHub repository.
GitHub-Flavored Markdown Syntax
GitHub Flavored Markdown syntax is divided into two parts.
We’ll look at each one in detail below.
Basic Formatting Syntax
Basic formatting syntax applies to everyone. It contains fundamental essentials such as headings, code, images, quotes, links, and so on – things you’ll need to know for writing.
- Headings
- Paragraphs
- Comment
- Styling text
- Quotes
- Code
- Links
- Images
- Lists
- Mentioning people and teams
- Referencing issues and pull requests
- Using emojis
- Footnotes
- Alerts
Note that the code samples mostly come from GitHub’s documentation.
Headings
You can use the #
symbol to create headings. One #
creates an H1 heading, two create an H2 heading, and so on, like this:
Paragraphs
To create paragraphs, you can use a blank line to separate one or more lines of text or paragraphs.
Comments are available in almost every programming language. They help developers write notes and add additional information to their code, helping other developers understand what’s going on and how the code is working.
To add notes and additional information in Markdown, use the following syntax: <!--- Wrap text --->
.
Here’s an example:
Styling text
You can apply basic styles to your text, such as bold, italic, strikethrough, subscript, or superscript, to improve readability and convey your point more clearly.
- For Bold, you can use the following syntax:
**your text**
- For italics, you can use the following syntax:
*your text* or _your text_.
- For strikethrough, you can use the following syntax:
~~your text~~
- For subscript, you can use the following syntax:
The subscript <sub> text </sub> is here.
- For superscript, you can use the following syntax:
The superscript <sup> text </sup> is here.
## Bold
**your text**
## italics
*your text*
_your text_
## strikethrough
~~your text~~
## subscript
The subscript <sub> text </sub> is here.
## superscript
The subscript <sup> text </sup> is here.
Quotes
A blockquote or quote is a sentence or paragraph formatted to let the reader know that you’re quoting someone. To create a blockquote in Markdown, you can use the >
symbol.
Code
Markdown files support two types of code samples: inline and code block.
- To add a code block in a Markdown file, use the following syntax:
``` your code ```
. - To add inline code to the Markdown file, use the following syntax:
`your code`
.
To support code highlighting in a code block, you can add an optional language identifier after your triple backticks (like JavaScript in the example below):
Links
A markdown file divides links into two categories: inline and relative.
Inline links
To create an inline link in a Markdown file, wrap the link text in brackets [ ]
followed immediately by the URL in parentheses ( )
.
This site was built using [GitHub Pages](https://pages.github.com/).
Relative links
Relative links are defined similarly to inline links but they change in the []
section: the []
section contains the path of the file in your repository.
You use relative links to link two files: for example, to link the CONTRIBUTING file into the README file.
[Contribution guidelines](docs/CONTRIBUTING.md)
Relative links starting with /
will be relative to the repository root. You can use all relative link operands, such as ./
and ../.
:
[Contribution guidelines](../docs/CONTRIBUTING.md)
Images
To add an image in a markdown file, add a !
and then wrap the alt text in []
. Then, wrap the image link with parentheses ()
.
It looks like this:
![Markdown](https://img.shields.io/badge/markdown-%23000000.svg?style=for-the-badge&logo=markdown&logoColor=white)
Lists
A list helps record essential information in order, which can be vital for the reader and makes it easy for people to understand and find information.
Markdown files support three types of lists:
- Ordered list
- Unordered list
- Task list
Ordered list
The first type is an ordered list. To create an ordered list, start with numbers followed by periods.
Unordered list
The second type is an unordered list. To create an unordered list, use -
, +
or *
(depending on your preference – they’ll all render as an unordered list):
Task list
The third type is a task list. To create a task list, list items start with a hyphen, followed by a space, followed by square brackets []
. You can use an x
in the bracket [x]
to mark a task as complete.
Mentioning people and teams
To mention a person or team in a GitHub markdown file, type @
and write the username or team username.
Referencing issues and pull requests
To mention issues and pull requests in a GitHub markdown file, type a #
, then type the issue or pull request number or title. Then press either tab or enter to complete the highlighted result.
Using emoji
To add an emoji to your writing, type the emoji’s code between two colons. If you just type :
, a list of suggested emojis on GitHub will appear.
Once you find the emoji you’re looking for, press Tab or Enter to choose the highlighted result.
To add a footnote reference, add a caret and an identifier inside brackets ([^1])
using the following syntax:
Alerts
Alerts are a Markdown extension based on the block quote syntax that you can use to emphasize important information.
GitHub Flavored Markdown supports five types of alerts: [!NOTE]
, [!TIP]
, [!IMPORTANT]
, [!WARNING]
, and [!CAUTION]
. You can use any of them:
The Alert syntax looks like this in the browser:
Advanced Formatting Syntax
This advanced formatting syntax section contains advanced use cases, such as adding diagrams and tables, collapsed sections, mathematical expressions, and more.
Creating a table
To create tables in Markdown, you can use pipes |
and hyphens -
. Hyphens are used to create a column’s header, while pipes are used to separate columns.
The table looks like this in the browser:
Creating a collapsed section
To create a collapsed section in a markdown file, you can use the <details>
tag. This tag is an HTML element that you can easily use to extend the functionality of GitHub Flavored Markdown. Here’s how it works:
The collapsed syntax looks like this in the browser:
Creating diagrams
To add diagrams to a Markdown file, use triple backticks and wrap them inside quadruple backticks. Then, tell which identifier (Mermaid, GeoJSON, TopJSON, ASCII STL) you used for the diagram.
GitHub supports diagrams using four syntaxes: mermaid, geoJSON, topoJSON, and ASCII STL.
Mermaid
Mermaid is a Markdown-inspired tool that renders text into diagrams. You can create flow charts, sequence diagrams, pie charts, and more with Mermaid.
The GitHub-flavored Markdown has extended the functionality of using Mermaid with Markdown.
You can create flow charts, sequence diagrams, pie charts, and so on inside Markdown. GitHub handles the rest of that. So how do you render diagrams on the screen?
The mermaid syntax looks like this in the browser.
GeoJSON and TopoJSON
You can use GeoJSON or TopoJSON to add an interactive map to a GitHub repository in a README file or GitHub Wiki.
You can use code block syntax to add an interactive map.
- GeoJSON can create a map by specifying coordinates. To add an interactive map, use the following syntax:
```geojson your code ```
- TopoJSON can create a map by specifying coordinates and shapes. To add an interactive map, use the following syntax:
```topojson your code ```
Example using GeoJSON:
Example of TopJSON:
ASCII STL
GitHub Flavored Markdown supports STL syntax. STL syntax allows you to add interactive 3D models in markdown. You can use the following syntax: ```stl your code.```
The STL syntax looks like this in the browser:
Mathematical expressions
You can add mathematical expressions, such as equations, terms, formulas, and so on, to a GitHub markdown file. GitHub uses LaTeX formatted within Markdown. There are two ways to add these expressions:
- Writing inline math expressions
- Writing math expressions as code blocks
Writing inline math expressions
An inline math expression starts with $
and ends with $
.
The inline math syntax looks like this in the browser:
Writing math expressions as code blocks
To add a math expression’s code block to the Markdown file, use the ```math
code block and wrap it inside ```
backticks to display the expression as a block.
To add a math expression’s code block to the Markdown file, use the ““math code block and wrap it inside triple backticks to display the expression as a block.
The math code block syntax looks like this in the browser:
Conclusion
Markdown syntax works well in GitHub and all other central Git servers, such as GitLab, Gitea, and so on.
Different tools name their markdown differently. For example, GitHub extends markdown functionality in its own way and builds GitHub Flavored Markdown. GitLab also extends markdown functionality and builds and creates a GitLab-flavored markdown.
Markdown syntax is mostly the same in every Git service. But alerts, diagrams, and a few other features only work in GitHub Flavored Markdown.