参考
区块元素
段落和换行
一个 Markdown 段落是由一个或多个连续的文本行组成,它的前后要有一个以上的空行(空行的定义是显示上看起来像是空的,便会被视为空行。比方说,若某一行只包含空格和制表符,则该行也会被视为空行)。普通段落不该用空格或制表符来缩进。
「由一个或多个连续的文本行组成」这句话其实暗示了 Markdown 允许段落内的强迫换行(插入换行符),这个特性和其他大部分的 text-to-HTML 格式不一样(包括 Movable Type 的「Convert Line Breaks」选项),其它的格式会把每个换行符都转成 ``标签。
如果你确实想要依赖 Markdown 来插入 ``标签的话,在插入处先按入两个以上的空格然后回车。
的确,需要多费点事(多加空格)来产生 ,但是简单地「每个换行都转换为」的方法在 Markdown 中并不适合, Markdown 中 email 式的 区块引用 和多段落的 列表 在使用换行来排版的时候,不但更好用,还更方便阅读。
标题
Markdown 支持两种标题的语法,类 Setext 和类 atx 形式。
类 Setext 形式是用底线的形式,利用 = (最高阶标题)和 - (第二阶标题),例如:
1 | This is an H1 |
任何数量的 = 和 - 都可以有效果。
类 Atx 形式则是在行首插入 1 到 6 个 # ,对应到标题 1 到 6 阶,例如:
1 | # 这是 H1 |
你可以选择性地「闭合」类 atx 样式的标题,这纯粹只是美观用的,若是觉得这样看起来比较舒适,你就可以在行尾加上 #,而行尾的 # 数量也不用和开头一样(行首的井字符数量决定标题的阶数):
1 | # 这是 H1 # |
区块引用 Blockquotes
Markdown 标记区块引用是使用类似 email 中用 > 的引用方式。如果你还熟悉在 email 信件中的引言部分,你就知道怎么在 Markdown 文件中建立一个区块引用,那会看起来像是你自己先断好行,然后在每行的最前面加上 > :
1 | > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet, |
区块引用可以嵌套(例如:引用内的引用),只要根据层次加上不同数量的 > :
1 | > This is the first level of quoting. |
引用的区块内也可以使用其他的 Markdown 语法,包括标题、列表、代码区块等:
1 | > ## 这是一个标题。 |
任何像样的文本编辑器都能轻松地建立 email 型的引用。例如在 BBEdit 中,你可以选取文字后然后从选单中选择增加引用阶层。
列表
Markdown 支持有序列表和无序列表。
无序列表使用星号、加号或是减号作为列表标记:1
2
3* Red
* Green
* Blue
等同于:1
2
3+ Red
+ Green
+ Blue
也等同于:1
2
3- Red
- Green
- Blue
有序列表则使用数字接着一个英文句点:1
2
31. Bird
2. McHale
3. Parish
很重要的一点是,你在列表标记上使用的数字并不会影响输出的 HTML 结果,上面的列表所产生的 HTML 标记为:1
2
3
4
5<ol>
<li>Bird</li>
<li>McHale</li>
<li>Parish</li>
</ol>
如果你的列表标记写成:1
2
31. Bird
1. McHale
1. Parish
或甚至是:1
2
33. Bird
1. McHale
8. Parish
你都会得到完全相同的 HTML 输出。重点在于,你可以让 Markdown 文件的列表数字和输出的结果相同,或是你懒一点,你可以完全不用在意数字的正确性。
如果你使用懒惰的写法,建议第一个项目最好还是从 1. 开始,因为 Markdown 未来可能会支持有序列表的 start 属性。
列表项目标记通常是放在最左边,但是其实也可以缩进,最多 3 个空格,项目标记后面则一定要接着至少一个空格或制表符。
要让列表看起来更漂亮,你可以把内容用固定的缩进整理好:1
2
3
4
5
6* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.
* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing.
但是如果你懒,那也行:1
2
3
4
5* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.
* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing.
如果列表项目间用空行分开,在输出 HTML 时 Markdown 就会将项目内容用 `` 标签包起来,举例来说:1
2* Bird
* Magic
会被转换为:1
2
3
4<ul>
<li>Bird</li>
<li>Magic</li>
</ul>
但是这个:1
2
3* Bird
* Magic
会被转换为:1
2
3
4<ul>
<li><p>Bird</p></li>
<li><p>Magic</p></li>
</ul>
列表项目可以包含多个段落,每个项目下的段落都必须缩进 4 个空格或是 1 个制表符:1
2
3
4
5
6
7
8
91. This is a list item with two paragraphs. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit. Aliquam hendrerit
mi posuere lectus.
Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
sit amet velit.
2. Suspendisse id sem consectetuer libero luctus adipiscing.
如果你每行都有缩进,看起来会看好很多,当然,再次地,如果你很懒惰,Markdown 也允许:1
2
3
4
5
6
7* This is a list item with two paragraphs.
This is the second paragraph in the list item. You're
only required to indent the first line. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit.
* Another item in the same list.
如果要在列表项目内放进引用,那 > 就需要缩进:1
2
3
4* A list item with a blockquote:
> This is a blockquote
> inside a list item.
如果要放代码区块的话,该区块就需要缩进两次,也就是 8 个空格或是 2 个制表符:1
2
3* 一列表项包含一个列表区块:
<代码写在这>
当然,项目列表很可能会不小心产生,像是下面这样的写法:
1986. What a great season.
换句话说,也就是在行首出现数字-句点-空白,要避免这样的状况,你可以在句点前面加上反斜杠。
1986\. What a great season.
代码区块
和程序相关的写作或是标签语言原始码通常会有已经排版好的代码区块,通常这些区块我们并不希望它以一般段落文件的方式去排版,而是照原来的样子显示,Markdown 会用 和 标签来把代码区块包起来。
要在 Markdown 中建立代码区块很简单,只要简单地缩进 4 个空格或是 1 个制表符就可以,例如,下面的输入:
这是一个普通段落:
```
这是一个代码区块。1
2
Markdown 会转换成:
这是一个普通段落:
这是一个代码区块。
1 |
|
Here is an example of AppleScript:
tell application “Foo”
beep
end tell
1 |
|
Here is an example of AppleScript:
tell application "Foo"
beep
end tell
1 |
|
1 |
|
<div class="footer">
© 2004 Foo Corporation
</div>
1 |
|
1 |
|
This is an example inline link.
This link has no title attribute.1
2
会产生:
This is an example inline link.
This link has no title attribute.
1 |
|
See my About page for details.
1 |
|
This is an example reference-style link.
1 |
|
This is an example reference-style link.
1 |
|
1 |
|
foo: http://example.com/ ‘Optional Title Here’
1 |
|
1 |
|
"Optional Title Here"
1 |
|
[link text][a]
[link text][A]1
2
隐式链接标记功能让你可以省略指定链接标记,这种情形下,链接标记会视为等同于链接文字,要用隐式链接标记只要在链接文字后面加上一个空的方括号,如果你要让 "Google" 链接到 google.com,你可以简化成:
Google1
2
然后定义链接内容:
1 |
|
Visit Daring Fireball for more information.1
然后接着定义链接:
1 |
|
I get 10 times more traffic from Google than from
Yahoo or MSN.
1 |
|
I get 10 times more traffic from Google than from
Yahoo or MSN.
1 |
|
I get 10 times more traffic from Google than from Yahoo or MSN.
1 |
|
I get 10 times more traffic from Google
than from Yahoo or
MSN.
1 |
|
single asterisks
_single underscores_
double asterisks
double underscores
1 |
|
single asterisks
single underscores
double asterisks
double underscores
1 |
|
unfriggingbelievable
1 |
|
*this text is surrounded by literal asterisks*
1 |
|
Use the
printf()
function.1 |
|
Use the printf()
function.
1 |
|
There is a literal backtick (`) here.
1 |
|
A single backtick in a code span:
`
A backtick-delimited string in a code span:
`foo`
1 |
|
A single backtick in a code span: `
A backtick-delimited string in a code span: `foo`
1 |
|
Please don’t use any
<blink>
tags.1 |
|
Please don't use any <blink>
tags.
1 |
|
—
is the decimal-encoded equivalent of —
.1 |
|
—
is the decimal-encoded
equivalent of —
.
1 |
|


1 |
|
「id」是图片参考的名称,图片参考的定义方式则和连结参考一样:
1 |
|
http://example.com/1
2
Markdown 会转为:
http://example.com/1
2
邮址的自动链接也很类似,只是 Markdown 会先做一个编码转换的过程,把文字字符转成 16 进位码的 HTML 实体,这样的格式可以糊弄一些不好的邮址收集机器人,例如:
address@example.com1
2
Markdown 会转成:
address@exa
mple.com1
2
3
4
5
6
7
8
9
10
在浏览器里面,这段字串(其实是 `[address@example.com](mailto:address@example.com)`)会变成一个可以点击的「address@example.com」链接。
(这种作法虽然可以糊弄不少的机器人,但并不能全部挡下来,不过总比什么都不做好些。不管怎样,公开你的信箱终究会引来广告信件的。)
### 反斜杠
Markdown 可以利用反斜杠来插入一些在语法中有其它意义的符号,例如:如果你想要用星号加在文字旁边的方式来做出强调效果(但不用 `` 标签),你可以在星号的前面加上反斜杠:
`\*literal asterisks\*`
Markdown 支持以下这些符号前面加上反斜杠来帮助插入普通的符号:
\ 反斜线
` 反引号
- 加号
- 减号
. 英文句点
! 惊叹号
```