Markdown语法
Markdown 标题语法
要创建标题,请在单词或短语前面添加井号 (#
) 。#
的数量代表了标题的级别。例如,添加三个 #
表示创建一个三级标题 (<h3>
) (例如:### My Header
)。
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 | 预览效果 |
---|---|---|
Heading level 1 =============== | <h1>Heading level 1</h1> | # Heading level 1 |
Heading level 2 --------------- | <h2>Heading level 2</h2> | ## Heading level 2 |
Markdown 段落
要创建段落,请使用空白行将一行或多行文本进行分隔。
Markdown语法 | HTML | 预览效果 |
---|---|---|
I really like using Markdown. I think I'll use it to format all of my documents from now on. | <p>I really like using Markdown.</p> <p>I think I'll use it to format all of my documents from now on.</p> | I really like using Markdown. I think I'll use it to format all of my documents from now on. |
Markdown 换行语法
在一行的末尾添加两个或多个空格,然后按回车键,即可创建一个换行(<br>
)。
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 强调语法
通过将文本设置为粗体或斜体来强调其重要性。
粗体(Bold)
要加粗文本,请在单词或短语的前后各添加两个星号(asterisks)或下划线(underscores)。如需加粗一个单词或短语的中间部分用以表示强调的话,请在要加粗部分的两侧各添加两个星号(asterisks)。
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. |
Love**is**bold | Love<strong>is</strong>bold | Loveisbold |
斜体(Italic)
要用斜体显示文本,请在单词或短语前后添加一个星号(asterisk)或下划线(underscore)。要斜体突出单词的中间部分,请在字母前后各添加一个星号,中间不要带空格。
Markdown语法 | HTML | 预览效果 |
---|---|---|
Italicized text is the *cat's meow*. | Italicized text is the <em>cat's meow</em>. | Italicized text is the cat’s meow. |
Italicized text is the _cat's meow_. | Italicized text is the <em>cat's meow</em>. | Italicized text is the cat’s meow. |
A*cat*meow | A<em>cat</em>meow | A_cat_meow |
粗体(Bold)和斜体(Italic)
要同时用粗体和斜体突出显示文本,请在单词或短语的前后各添加三个星号或下划线。要加粗并用斜体显示单词或短语的中间部分,请在要突出显示的部分前后各添加三个星号,中间不要带空格。
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. |
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. |
This is really***very***important text. | This is really<strong><em>very</em></strong>important text. | This is really**very**important text. |
Markdown 引用语法
要创建块引用,请在段落前添加一个 >
符号。
> Dorothy followed her through many of the beautiful rooms in her castle.
渲染效果如下所示:
Dorothy followed her through many of the beautiful rooms in her castle.
多个段落的块引用
块引用可以包含多个段落。为段落之间的空白行添加一个 >
符号。
> 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.
嵌套块引用
块引用可以嵌套。在要嵌套的段落前添加一个 >>
符号。
> 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.
带有其它元素的块引用
块引用可以包含其他 Markdown 格式的元素。并非所有元素都可以使用,你需要进行实验以查看哪些元素有效。
> #### The quarterly results look great!
>
> - Revenue was off the chart.
> - Profits were higher than ever.
>
> *Everything* is going according to **plan**.
渲染效果如下:
The quarterly results look great!
- Revenue was off the chart.
- Profits were higher than ever.
Everything is going according to plan.
Markdown 列表语法
可以将多个条目组织成有序或无序列表。
有序列表
要创建有序列表,请在每个列表项前添加数字并紧跟一个英文句点。数字不必按数学顺序排列,但是列表应当以数字 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 |
1. First item 1. Second item 1. Third item 1. 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 |
1. First item 8. Second item 3. Third item 5. 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 |
1. First item 2. Second item 3. Third item 1. Indented item 2. Indented item 4. Fourth item | <ol> <li>First item</li> <li>Second item</li> <li>Third item <ol> <li>Indented item</li> <li>Indented item</li> </ol> </li> <li>Fourth item</li> </ol> | 1. First item 2. Second item 3. Third item 1. Indented item 2. Indented 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 |
* 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 |
+ 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 |
- First item - Second item - Third item - Indented item - Indented item - Fourth item | <ul> <li>First item</li> <li>Second item</li> <li>Third item <ul> <li>Indented item</li> <li>Indented item</li> </ul> </li> <li>Fourth item</li> </ul> | - First item - Second item - Third item - Indented item - Indented item - Fourth item |
Markdown 代码语法
要将单词或短语表示为代码,请将其包裹在反引号 (`
) 中。
Markdown语法 | HTML | 预览效果 |
---|---|---|
At the command prompt, type `nano`. | At the command prompt, type <code>nano</code>. | At the command prompt, type nano . |
转义反引号
如果你要表示为代码的单词或短语中包含一个或多个反引号,则可以通过将单词或短语包裹在双反引号(``
)中。
Markdown语法 | HTML | 预览效果 |
---|---|---|
``Use `code` in your Markdown file.`` | <code>Use `code` in your Markdown file.</code> | Use `code` in your Markdown file. |
代码块
要创建代码块,请将代码块的每一行缩进至少四个空格或一个制表符。
<html>
<head>
</head>
</html>
渲染效果如下:
<html>
<head>
</head>
</html>
Markdown 分隔线语法
要创建分隔线,请在单独一行上使用三个或多个星号 (***
)、破折号 (---
) 或下划线 (___
) ,并且不能包含其他内容。
***
---
_________________
以上三个分隔线的渲染效果看起来都一样:
Markdown 内嵌 HTML 标签
对于 Markdown 涵盖范围之外的标签,都可以直接在文件里面用 HTML 本身。如需使用 HTML,不需要额外标注这是 HTML 或是 Markdown,只需 HTML 标签添加到 Markdown 文本中即可。
行级內联标签
HTML 的行级內联标签如 <span>
、<cite>
、<del>
不受限制,可以在 Markdown 的段落、列表或是标题里任意使用。依照个人习惯,甚至可以不用 Markdown 格式,而采用 HTML 标签来格式化。例如:如果比较喜欢 HTML 的 <a>
或 <img>
标签,可以直接使用这些标签,而不用 Markdown 提供的链接或是图片语法。当你需要更改元素的属性时(例如为文本指定颜色或更改图像的宽度),使用 HTML 标签更方便些。
HTML 行级內联标签和区块标签不同,在內联标签的范围内, Markdown 的语法是可以解析的。
This **word** is bold. This <em>word</em> is italic.
渲染效果如下:
This word is bold. This word is italic.
区块标签
区块元素──比如 <div>
、<table>
、<pre>
、<p>
等标签,必须在前后加上空行,以便于内容区分。而且这些元素的开始与结尾标签,不可以用 tab 或是空白来缩进。Markdown 会自动识别这区块元素,避免在区块标签前后加上没有必要的 <p>
标签。
例如,在 Markdown 文件里加上一段 HTML 表格:
This is a regular paragraph.
<table>
<tr>
<td>Foo</td>
</tr>
</table>
This is another regular paragraph.
请注意,Markdown 语法在 HTML 区块标签中将不会被进行处理。例如,你无法在 HTML 区块内使用 Markdown 形式的*强调*
。
HTML 用法最佳实践
出于安全原因,并非所有 Markdown 应用程序都支持在 Markdown 文档中添加 HTML。如有疑问,请查看相应 Markdown 应用程序的手册。某些应用程序只支持 HTML 标签的子集。
对于 HTML 的块级元素 <div>
、<table>
、<pre>
和 <p>
,请在其前后使用空行(blank lines)与其它内容进行分隔。尽量不要使用制表符(tabs)或空格(spaces)对 HTML 标签做缩进,否则将影响格式。
在 HTML 块级标签内不能使用 Markdown 语法。例如 <p>italic and **bold**</p>
将不起作用。