Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,Markdown文件的后缀名便是“.md”。
要创建标题,请在单词或短语前面添加井号 (#
) 。#
的数量代表了标题的级别。例如,添加三个 #
表示创建一个三级标题 () (例如:
### My Header
)。
Markdown语法 | HTML | 预览效果 |
---|---|---|
# Heading level 1 |
| Heading level 1 |
## Heading level 2 |
| Heading level 2 |
### Heading level 3 |
| Heading level 3 |
#### Heading level 4 |
| Heading level 4 |
##### Heading level 5 |
| Heading level 5 |
###### Heading level 6 |
| Heading level 6 |
不同的 Markdown 应用程序处理 #
和标题之间的空格方式并不一致。为了兼容考虑,请用一个空格在 #
和标题之间进行分隔。
✅ Do this | ❌ Don't do this |
---|---|
# Here's a Heading | #Here's a Heading |
要创建段落,请使用空白行将一行或多行文本进行分隔。
Markdown语法 | HTML | 预览效果 |
---|---|---|
I really like using Markdown. |
| I really like using Markdown. I think I'll use it to format all of my documents from now on. |
不要用空格(spaces)或制表符( tabs)缩进段落。
✅ Do this | ❌ Don't do this |
---|---|
Don't put tabs or spaces in front of your paragraphs. | This can result in unexpected formatting problems. |
在一行的末尾添加两个或多个空格,然后按回车键,即可创建一个换行(
)。
Markdown语法 | HTML | 预览效果 |
---|---|---|
This is the first line. |
| This is the first line. |
几乎每个 Markdown 应用程序都支持两个或多个空格进行换行,称为 结尾空格(trailing whitespace)
的方式,但这是有争议的,因为很难在编辑器中直接看到空格,并且很多人在每个句子后面都会有意或无意地添加两个空格。由于这个原因,你可能要使用除结尾空格以外的其它方式来换行。幸运的是,几乎每个 Markdown 应用程序都支持另一种换行方式:HTML 的
标签。
为了兼容性,请在行尾添加“结尾空格”或 HTML 的
标签来实现换行。
还有两种其他方式我并不推荐使用。CommonMark 和其它几种轻量级标记语言支持在行尾添加反斜杠 (\
) 的方式实现换行,但是并非所有 Markdown 应用程序都支持此种方式,因此从兼容性的角度来看,不推荐使用。并且至少有两种轻量级标记语言支持无须在行尾添加任何内容,只须键入回车键(return
)即可实现换行。
✅ Do this | ❌ Don't do this |
---|---|
First line with two spaces after. | First line with a backslash after.\ |
通过将文本设置为粗体或斜体来强调其重要性。
要加粗文本,请在单词或短语的前后各添加两个星号(asterisks)或下划线(underscores)。如需加粗一个单词或短语的中间部分用以表示强调的话,请在要加粗部分的两侧各添加两个星号(asterisks)。
Markdown语法 | HTML | 预览效果 |
---|---|---|
I just love bold text. | I just love bold text. | I just love bold text. |
I just love bold text. | I just love bold text. | I just love bold text. |
Loveisbold | Loveisbold | Loveisbold |
Markdown 应用程序在如何处理单词或短语中间的下划线上并不一致。为兼容考虑,在单词或短语中间部分加粗的话,请使用星号(asterisks)。
✅ Do this | ❌ Don't do this |
---|---|
Love**is**bold | Love__is__bold |
要用斜体显示文本,请在单词或短语前后添加一个星号(asterisk)或下划线(underscore)。要斜体突出单词的中间部分,请在字母前后各添加一个星号,中间不要带空格。
Markdown语法 | HTML | 预览效果 |
---|---|---|
Italicized text is the *cat's meow*. | Italicized text is the cat's meow. | Italicized text is the cat’s meow. |
Italicized text is the _cat's meow_. | Italicized text is the cat's meow. | Italicized text is the cat’s meow. |
A*cat*meow | Acatmeow | Acatmeow |
要同时用粗体和斜体突出显示文本,请在单词或短语的前后各添加三个星号或下划线。要加粗并用斜体显示单词或短语的中间部分,请在要突出显示的部分前后各添加三个星号,中间不要带空格。
✅ Do this | ❌ Don't do this |
---|---|
A*cat*meow | A_cat_meow |
要同时用粗体和斜体突出显示文本,请在单词或短语的前后各添加三个星号或下划线。要加粗并用斜体显示单词或短语的中间部分,请在要突出显示的部分前后各添加三个星号,中间不要带空格。
Markdown语法 | HTML | 预览效果 |
---|---|---|
This text is ***really important***. | This text is really important. | This text is really important. |
This text is ___really important___. | This text is really important. | This text is really important. |
This text is __*really important*__. | This text is really important. | This text is really important. |
This text is **_really important_**. | This text is really important. | This text is really important. |
This is really***very***important text. | This is reallyveryimportant text. | This is reallyveryimportant text. |
Markdown 应用程序在处理单词或短语中间添加的下划线上并不一致。为了实现兼容性,请使用星号将单词或短语的中间部分加粗并以斜体显示,以示重要。
✅ Do this | ❌ Don't do this |
---|---|
This is really***very***important text. | This is really___very___important text. |
要创建块引用,请在段落前添加一个 >
符号。
> 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.
可以将多个条目组织成有序或无序列表。
要创建有序列表,请在每个列表项前添加数字并紧跟一个英文句点。数字不必按数学顺序排列,但是列表应当以数字 1 起始。
Markdown语法 | HTML | 预览效果 |
---|---|---|
1. First item |
|
|
1. First item |
|
|
1. First item |
|
|
1. First item |
|
|
CommonMark and a few other lightweight markup languages let you use a parenthesis ()
) as a delimiter (e.g., 1) First item
), but not all Markdown applications support this, so it isn’t a great option from a compatibility perspective. For compatibility, use periods only.
✅ Do this | ❌ Don't do this |
---|---|
1. First item | 1) First item |
要创建无序列表,请在每个列表项前面添加破折号 (-)、星号 (*) 或加号 (+) 。缩进一个或多个列表项可创建嵌套列表。
Markdown语法 | HTML | 预览效果 |
---|---|---|
- First item |
|
|
* First item |
|
|
+ First item |
|
|
- First item |
|
|
Markdown applications don’t agree on how to handle different delimiters in the same list. For compatibility, don't mix and match delimiters in the same list — pick one and stick with it.
✅ Do this | ❌ Don't do this |
---|---|
- First item | + First item |
要在保留列表连续性的同时在列表中添加另一种元素,请将该元素缩进四个空格或一个制表符,如下例所示:
#段落
* This is the first list item.
* Here's the second list item.I need to add another paragraph below the second list item.* And here's the third list item.
渲染效果如下:
This is the first list item.
Here's the second list item.
I need to add another paragraph below the second list item.
And here's the third list item.
* This is the first list item.
* Here's the second list item.> A blockquote would look great below the second list item.* And here's the third list item.
渲染效果如下:
This is the first list item.
Here's the second list item.
A blockquote would look great below the second list item.
And here's the third list item.
代码块通常采用四个空格或一个制表符缩进。当它们被放在列表中时,请将它们缩进八个空格或两个制表符。
1. Open the file.
2. Find the following code block on line 21:3. Update the title to match the name of your website.
渲染效果如下:
Open the file.
Find the following code block on line 21:
Update the title to match the name of your website.
1. Open the file containing the Linux mascot.
2. Marvel at its beauty.![Tux, the Linux mascot](/assets/images/tux.png)3. Close the file.
渲染效果如下:
Open the file containing the Linux mascot.
Marvel at its beauty.
Close the file.
列表
You can nest an unordered list in an ordered list, or vice versa.
1. First item
2. Second item
3. Third item- Indented item- Indented item
4. Fourth item
渲染效果如下:
要创建分隔线,请在单独一行上使用三个或多个星号 (***
)、破折号 (---
) 或下划线 (___
) ,并且不能包含其他内容。
***---_________________
以上三个分隔线的渲染效果看起来都一样:
为了兼容性,请在分隔线的前后均添加空白行。
✅ Do this | ❌ Don't do this |
---|---|
Try to put a blank line before... | Without blank lines, this would be a heading. |
要显示原本用于格式化 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.
以下列出的字符都可以通过使用反斜杠字符从而达到转义目的。
Character | Name |
---|---|
\ | backslash |
` | backtick (see also escaping backticks in code) |
* | asterisk |
_ | underscore |
{ } | curly braces |
[ ] | brackets |
( ) | parentheses |
# | pound sign |
+ | plus sign |
- | minus sign (hyphen) |
. | dot |
! | exclamation mark |
| | pipe (see also escaping pipe in tables) |
在 HTML 文件中&#xff0c;有两个字符需要特殊处理&#xff1a; <
和 &
。 <
符号用于起始标签&#xff0c;&
符号则用于标记 HTML 实体&#xff0c;如果你只是想要使用这些符号&#xff0c;你必须要使用实体的形式&#xff0c;像是 <
和 &
。
&
符号其实很容易让写作网页文件的人感到困扰&#xff0c;如果你要打「AT&T」 &#xff0c;你必须要写成「AT&T
」 &#xff0c;还得转换网址内的 &
符号&#xff0c;如果你要链接到&#xff1a;
http://images.google.com/images?num&#61;30&q&#61;larry&#43;bird
你必须要把网址转成&#xff1a;
http://images.google.com/images?num&#61;30&q&#61;larry&#43;bird
才能放到链接标签的 href
属性里。不用说也知道这很容易忘记&#xff0c;这也可能是 HTML 标准检查所检查到的错误中&#xff0c;数量最多的。
Markdown 允许你直接使用这些符号&#xff0c;它帮你自动转义字符。如果你使用 &
符号的作为 HTML 实体的一部分&#xff0c;那么它不会被转换&#xff0c;而在其它情况下&#xff0c;它则会被转换成 &
。所以你如果要在文件中插入一个著作权的符号&#xff0c;你可以这样写&#xff1a;
©
Markdown 将不会对这段文字做修改&#xff0c;但是如果你这样写&#xff1a;
AT&T
Markdown 就会将它转为&#xff1a;
AT&T
类似的状况也会发生在 <
符号上&#xff0c;因为 Markdown 支持 行内 HTML &#xff0c;如果你使用 <
符号作为 HTML 标签的分隔符&#xff0c;那 Markdown 也不会对它做任何转换&#xff0c;但是如果你是写&#xff1a;
4 <5
Markdown 将会把它转换为&#xff1a;
4 <5
需要特别注意的是&#xff0c;在 Markdown 的块级元素和内联元素中&#xff0c; <
和 &
两个符号都会被自动转换成 HTML 实体&#xff0c;这项特性让你可以很容易地用 Markdown 写 HTML。&#xff08;在 HTML 语法中&#xff0c;你要手动把所有的 <
和 &
都转换为 HTML 实体。&#xff09;