Markdown 语法指南

格式化文档的基本语法

概述

Markdown 是一种轻量级标记语言,用于为纯文本文档添加格式。由 John Gruber 于 2004 年创建,现已成为全球最流行的标记语言之一。

使用 Markdown 不同于 WYSIWYG 编辑器。在 Word 等应用中,你点击按钮格式化文字,效果立即可见。Markdown 不同:你在文本中加入 Markdown 语法,标出哪些词、句应显示不同。

标题

在词或短语前加 # 创建标题。# 的数量对应标题级别(例如 ### 为三级标题)。

Markdown 语法
HTML 标签
渲染结果
# Heading level 1
<h1>Heading level 1</h1>

Heading level 1

## Heading level 2
<h2>Heading level 2</h2>

Heading level 2

### Heading level 3
<h3>Heading level 3</h3>

Heading level 3

#### Heading level 4
<h4>Heading level 4</h4>

Heading level 4

##### Heading level 5
<h5>Heading level 5</h5>
Heading level 5
###### Heading level 6
<h6>Heading level 6</h6>
Heading level 6

段落

用空行分隔一行或多行文字即可形成段落。

Markdown 语法
HTML 标签
渲染结果
I really like using Markdown.

It lets me write easily.
<p>I really like using Markdown.</p>
<p>It lets me write easily.</p>

I really like using Markdown.

It lets me write easily.

换行

行末加两个或多个空格再回车即可换行。

Markdown 语法
HTML 标签
渲染结果
This is the first line.
And this is the second line.
<p>This is the first line.<br>
And this is the second line.</p>

This is the first line.
And this is the second line.

强调

可通过加粗或斜体强调文字。

粗体

在词或短语前后加 ** 或 __ 即可加粗。

Markdown 语法
HTML 标签
渲染结果
I just love **bold text**.
I just love <strong>bold text</strong>.

I just love bold text.

I just love __bold text__.
I just love <strong>bold text</strong>.

I just love bold text.

斜体

在词或短语前后加 * 或 _ 即可斜体。

Markdown 语法
HTML 标签
渲染结果
The *cat* meows.
The <em>cat</em> meows.

The cat meows.

The _cat_ meows.
The <em>cat</em> meows.

The cat meows.

粗体与斜体

在词或短语前后加 *** 或 ___ 即可同时加粗和斜体。

Markdown 语法
HTML 标签
渲染结果
This text is ***really important***.
This text is <strong><em>really important</em></strong>.

This text is really important.

This text is ___really important___.
This text is <strong><em>really important</em></strong>.

This text is really important.

引用

在段落前加 > 即可创建引用。

Markdown 语法
HTML 标签
渲染结果
> Dorothy followed her through many of the beautiful rooms in her castle.
<blockquote><p>Dorothy followed her through many of the beautiful rooms in her castle.</p></blockquote>

Dorothy followed her through many of the beautiful rooms in her castle.

嵌套引用

引用可嵌套。在要嵌套的段落前加 >>。

Markdown 语法
渲染结果
> Dorothy followed her through many of the beautiful rooms in her castle.
>
>> The Witch bade her clean the pots and kettles and sweep the floor and keep the fire fed with wood.

Dorothy followed her through many of the beautiful rooms in her castle.

The Witch bade her clean the pots and kettles and sweep the floor and keep the fire fed with wood.

列表

可将项目组织为有序或无序列表。

有序列表

每行以数字加句点开头即可创建有序列表。数字不必连续,但列表应从 1 开始。

Markdown 语法
HTML 标签
渲染结果
1. First item
2. Second item
3. Third item
4. Fourth item
<ol>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ol>
  1. First item
  2. Second item
  3. Third item
  4. Fourth item

无序列表

在每行前加 -、* 或 + 即可创建无序列表。

Markdown 语法
HTML 标签
渲染结果
- First item
- Second item
- Third item
- Fourth item
<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth item</li>
</ul>
  • First item
  • Second item
  • Third item
  • Fourth item

代码

用反引号 ` 将词或短语围住即可标记为代码。

Markdown 语法
HTML 标签
渲染结果
At the command prompt, type `nano`.
At the command prompt, type <code>nano</code>.

At the command prompt, type nano.

代码块

每行缩进至少四个空格或一个制表符,或在代码块前后用 ``` 围住,即可创建代码块。

Markdown 语法
渲染结果
```
{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25
}
```
{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25
}

水平线

单独一行使用三个及以上 ***、--- 或 ___ 即可创建水平线。

Markdown 语法
HTML 标签
渲染结果
***
<hr>

---
<hr>

___
<hr>

图片

添加图片:先写 !,再写 [替代文字],最后在括号里写图片路径或 URL。可选在 URL 后加引号包住的标题。

Markdown 语法
HTML
渲染结果
![The San Juan Mountains](https://mdg.imgix.net/assets/images/san-juan-mountains.jpg "San Juan Mountains")
<img src="https://mdg.imgix.net/assets/images/san-juan-mountains.jpg" alt="The San Juan Mountains" title="San Juan Mountains">
The San Juan Mountains

转义字符

若要显示本用于格式化的字符本义,在该字符前加反斜杠 \。

Markdown 语法
渲染结果
\* Without the backslash, this would be a bullet in an unordered list.

* Without the backslash, this would be a bullet in an unordered list.

可转义的字符

可用反斜杠转义以下字符:

\backslash
`backtick
*asterisk
_underscore
{ }curly braces
[ ]brackets
( )parentheses
#pound sign
+plus sign
-minus sign (hyphen)
.dot
!exclamation mark
|pipe

表格

用三个及以上 --- 做每列表头,用 | 分隔列。可选在表格两端也加 |。

Markdown 语法
渲染结果
| Syntax    | Description |
| --------- | ----------- |
| Header    | Title       |
| Paragraph | Text        |
SyntaxDescription
HeaderTitle
ParagraphText

对齐

在表头行的连字符左侧、右侧或两侧加冒号 (:) 可分别左对齐、右对齐或居中对齐列内文字。

Markdown 语法
渲染结果
| Align Left | Align Center | Align Right |
| :--- | :---: | ---: |
| This | This | This |
| column | column | column |
| will | will | will |
| align | align | align |
| left | center | right |
左对齐居中右对齐
ThisThisThis
columncolumncolumn
willwillwill
alignalignalign
leftcenterright