Markdown语法

标题语法

若需创建标题,则应在单词或者短语前添加井号(#)。#的数量代表了标题的级别。例如,添加三个 # 表示创建一个三级标题 (<h3>) (例如:### My Header)。级别同字体大小成负相关。

image-20240813202630908

还可以在文本下方添加任意数量的 == 号(记得要跟高亮区分开吼,应该没人会搞混吧)来标识一级标题,或者 – 号来标识二级标题。如下图。

image-20240813202946025

由于不同的Markdown应用程序处理#和标题之间的空格方式并不一致。因此为了兼容考虑,请用一个空格在 # 和标题之间进行分隔。# Here’s a Heading ==√== #Here’s a Heading==╳==

段落语法

要创建段落,请使用空白行将一行或多行文本进行分隔。

Markdown语法 HTML 预览效果
I really like using Markdown. <p>I really like using Markdown.</p> I really like using Markdown.

==温馨提示:==

不要用==空格(spaces)==或==制表符( tabs)==缩进段落。

Don't put tabs or spaces in front of your paragraphs.

Keep lines left-aligned like this. ==√==

This can result in unexpected formatting problems.

Don't add tabs or spaces in front of paragraphs.==╳==

换行语法

在一行的末尾添加两个或多个空格(真的容易忘),然后按回车键,即可创建一个换行(<br>)。 (可别一高兴光点回车就以为是换行了

Markdown语法 HTML 预览效果
This is the first line. <br/>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 应用程序都支持两个或多个空格进行换行,称为 结尾空格(trailing whitespace) 的方式,但这是有争议的,因为很难在编辑器中直接看到空格,并且很多人在每个句子后面都会有意或无意地添加两个空格。由于这个原因,你可能要使用除结尾空格以外的其它方式来换行。幸运的是,几乎每个 Markdown 应用程序都支持另一种换行方式:HTML 的 <br> 标签。

为了兼容性,请在行尾添加==“结尾空格”==或==HTML 的 <br>== 标签来实现换行。

强调语法(粗体和斜体)

粗体

要加粗文本,请在单词或短语的==前后==各添加两个星号(asterisks)或__下划线(underscores)__。如需加粗一个单词或短语的中间部分用以表示强调的话,请在要加粗部分的两侧各添加两个星号(asteris__k__s)。

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

Markdown 应用程序在如何处理单词或短语中间的下划线上并==不一致==。为兼容考虑,在单词或短语中间部分加粗的话,请使用==星号(asterisks)*==。

斜体

要用斜体显示文本,请在单词或短语==前后==添加一个星号(asterisk)或_下划线(underscore)_。要斜体突出单词的中间部分,请在字母==前后==各添加一个星号,==中间不要带空格==。

Markdown语法 HTML 预览效果
I just love *bold text*. I just love <em>bold text</em>. I just love bold text.
I just love _bold text_. I just love <em>bold text</em>. I just love bold text.
Love*is*bold Love<em>is</em>bold Loveisbold

粗体与斜体的合体

要同时用==粗体和斜体==突出显示文本,请在单词或短语的前后各添加==三个星号***==或==下划线___==。要加粗并用斜体显示单词或短语的中间部分,请在要突出显示的部分前后各添加三个星号,中间不要带空格。

稳妥用法

Markdown 应用程序在处理单词或短语中间添加的下划线上并不一致。为了实现兼容性,请使用==星号==将单词或短语的中间部分加粗并以斜体显示,以示重要。

引用用法

要创建块引用,请在段落前添加一个 ==>==符号。

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

渲染效果如下所示:

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

多个段落的块引用

块引用可以包含多个段落。为段落之间的空白行添加一个 ==>==符号。

1
2
3
> 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
2
3
> 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 格式的元素。并非所有元素都可以使用,你需要进行实验(官方文档就这样水灵灵的说了)以查看哪些元素有效。

1
2
3
4
5
6
> #### 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== 起始(好像typora里无法通过这个方法实现?,不过还好有相应的格式应用)。

Markdown语法 HTML 预览效果
\1. First item<br/>\2. Second item<br/>\3. Third item<br/>\4. Fourth item <ol><br/><li>First item</li><br/><li>Second item</li><br/><li>Third item</li><br/><li>Fourth item</li><br/></ol> 1. First item
2. Second item
3. Third item
4. Fourth item
\1. First item<br/>\1. Second item<br/>\1. Third item<br/>\1. Fourth item <ol><br/><li>First item</li><br/><li>Second item</li><br/><li>Third item</li><br/><li>Fourth item</li><br/></ol> 1. First item
2. Second item
3. Third item
4. Fourth item
\1. First item<br/>\8. Second item<br/>\3. Third item<br/>\5. Fourth item <ol><br/><li>First item</li><br/><li>Second item</li><br/><li>Third item</li><br/><li>Fourth item</li><br/></ol> 1. First item
2. Second item
3. Third item
4. Fourth item
\1. First item<br/>\2. Second item<br/>\3. Third item<br/> \1. Indented item<br/> \2. Indented item<br/>\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

有的软件支持1)作为列表开头,但并非全部都允许,为了保证兼容性,建议使用==1.==。

无序列表

要创建无序列表,请在每个列表项前面添加破折号 (-)、星号 (*) 或加号 (+) 。缩进一个或多个列表项可创建嵌套列表(好像typora里无法通过这个方法实现?梅开二度?,不过还好有相应的格式应用)。

image-20240813232921183

同样的,为了兼容性建议全使用-

在列表中嵌套其他元素

要在保留列表连续性的同时在列表中添加另一种元素,请将该元素缩进==四个空格==或==一个制表符==,如下例所示:

段落

1
2
3
4
5
6
*   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.

引用块

1
2
3
4
5
6
*   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
2
3
4
5
6
7
8
9
1.  Open the file.
2. Find the following code block on line 21:

<html>
<head>
<title>Test</title>
</head>

3. Update the title to match the name of your website.

渲染效果如下:

  1. Open the file.

  2. Find the following code block on line 21:

    1
    2
    3
    4
    <html>
    <head>
    <title>Test</title>
    </head>
  3. Update the title to match the name of your website.

图片

1
2
3
4
5
6
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.

渲染效果如下:

  1. Open the file containing the Linux mascot.

  2. Marvel at its beauty.

    Tux, the Linux mascot

  3. Close the file.

列表

你可以在一个有序列表中嵌套无序列表,反之亦然。

1
2
3
4
5
6
1. First item
2. Second item
3. Third item
- Indented item
- Indented item
4. Fourth item

渲染效果如下:

  1. First item
  2. Second item
  3. Third item
    • Indented item
    • Indented item
  4. 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语法中的样式实现),则可以通过将单词或短语包裹在双反引号(````)中。

Markdown语法 HTML 预览效果
Use `code` in your Markdown file. <code>Use code in your Markdown file.</code> Use `code` in your Markdown file.

代码块

要创建代码块,请将代码块的每一行缩进==至少四个空格或一个制表符====。

1
2
3
4
<html>
<head>
</head>
</html>

渲染效果如下:

1
2
3
4
<html>
<head>
</head>
</html>

分割线语法

要创建分隔线,请在单独一行上使用==三个或多个星号 (***)==、==破折号 (---) ==或==下划线 (___) ==,并且不能包含其他内容。

为了兼容性,请在分隔线的前后均添加==空白行==。

就像这样:

Try to put a blank line before…

-–

…and after a horizontal rule.

链接语法

超链接Markdown语法代码:[超链接显示名](超链接地址 "超链接title")

对应的HTML代码:<a href="超链接地址" title="超链接title">超链接显示名</a>

给链接加title

链接title是==当鼠标悬停在链接上时会出现的文字==,这个title是可选的,它放在圆括号中链接地址后面,跟链接地址之间以==空格==分隔。

1
这是一个链接 [Markdown语法](https://markdown.com.cn "最好的markdown教程")。

渲染效果如下:

这是一个链接 Markdown语法

网址和Email地址

使用==尖括号==(大小于号)可以很方便地把URL或者email地址变成可点击的链接。

1
2
<https://markdown.com.cn>
<fake@example.com>

渲染效果如下:

https://markdown.com.cn
fake@example.com

带格式化的链接

强调链接, 在链接语法前后增加==星号==。 要将链接表示为代码,请在方括号中添加==反引号==。

1
2
3
I love supporting the **[EFF](https://eff.org)**.
This is the *[Markdown Guide](https://www.markdownguide.org)*.
See the section on [`code`](#code).

渲染效果如下:

I love supporting the EFF.
This is the Markdown Guide.
See the section on code.

引用类型链接

引用样式链接是一种特殊的链接,它使URL在Markdown中更易于显示和阅读。参考样式链接分为两部分:与文本保持内联的部分以及存储在文件中其他位置的部分,以使文本易于阅读。

链接的第一部分格式

引用类型的链接的第一部分使用两组括号进行格式设置。第一组方括号包围==应显示为链接的文本==。第二组括号显示了==一个标签==,该标签用于指向您存储在文档其他位置的链接。

尽管不是必需的,可以在第一组和第二组括号之间包含一个==空格==。第二组括号中的标签不区分大小写,可以包含字母,数字,空格或标点符号。

以下示例格式对于链接的第一部分效果相同:

  • [hobbit-hole][1]
  • [hobbit-hole] [1]

链接的第二部分格式

引用类型链接的第二部分使用以下属性设置格式:

  1. 放在括号中的标签,其后==紧跟==一个冒号(:)和==至少一个==空格(例如[label]:)。
  2. 链接的URL,可以选择将其括在尖括号(<、>)中。
  3. 链接的可选标题(title),可以将其括在双引号(“ ”),单引号(‘ ’)或括号((、 ))中。

以下示例格式对于链接的第二部分效果相同:

  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle
  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle "Hobbit lifestyles"
  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle 'Hobbit lifestyles'
  • [1]: https://en.wikipedia.org/wiki/Hobbit#Lifestyle (Hobbit lifestyles)
  • [1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> "Hobbit lifestyles"
  • [1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> 'Hobbit lifestyles'
  • [1]: <https://en.wikipedia.org/wiki/Hobbit#Lifestyle> (Hobbit lifestyles)

可以将链接的第二部分放在Markdown文档中的任何位置。有些人将它们放在出现的段落之后,有些人则将它们放在文档的末尾(例如尾注或脚注)。

为了兼容性,请==尽量使用%20代替空格==。

图片语法

要添加图像,请使用感叹号 ==(!)==(唯一区别于链接的地方,本质就是打开链接), 然后在方括号增加替代文本,图片链接放在圆括号里,括号里的链接后可以增加一个可选的图片标题文本。

插入图片Markdown语法代码:![图片alt](图片链接 "图片title")

对应的HTML代码:<img src="图片链接" alt="图片alt" title="图片title">

1
![这是图片](/img/default.jpg "曹华")

这是图片

链接照片

给图片增加链接,请将图像的Markdown括在方括号中,然后将链接添加在圆括号中。

1
[![图片](/img/蔡夫人.jpg "蔡夫人")](https://wiki.biligame.com/sgs/%E8%94%A1%E5%A4%AB%E4%BA%BA)

渲染效果如下:

图片

转义字符语法

要显示原本用于格式化 Markdown文档的字符,要在字符前面添加反斜杠字符 \ 。

1
\* 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(反引号)
***** asterisk(星号)
_ underscore(下划线)
{ } curly braces(花括号)
[ ] brackets(中括号)
() parentheses(圆括号)
# pound sign(井号)
+ plus sign(加号)
- minus sign(hyphen) (负号(连字符))
. dot(点)
! exclamation mark(感叹号)
| pipe(通道)

特殊字符的自动转义

在 HTML 文件中,有两个字符需要特殊处理: <&< 符号用于起始标签,& 符号则用于标记 HTML 实体,如果你只是想要使用这些符号,你必须要使用实体的形式,像是&lt;&amp;

&(&amp;)

& 符号其实很容易让写作网页文件的人感到困扰,如果你要打「AT&T」 ,你必须要写成「AT&amp;T」 ,还得转换网址内的 & 符号,如果你要链接到:

1
http://images.google.com/images?num=30&q=larry+bird

你必须要把网址转成:

1
http://images.google.com/images?num=30&amp;q=larry+bird

才能放到链接标签的 href 属性里。不用说也知道这很容易忘记,这也可能是 HTML 标准检查所检查到的错误中,数量最多的。

不过Markdown允许直接使用&。若想使用&作为HTML实体的一部分,则其将不会被转化,而在其他情况下其会转化为&amp;。所以你如果要在文件中插入一个著作权的符号,你可以这样写:

1
&copy;

Markdown 将不会对这段文字做修改,但是如果你这样写:

1
AT&T

Markdown 就会将它转为:

1
AT&amp;T

<(&lt;)

同样的,若使用 < 符号作为 HTML 标签的分隔符,那 Markdown 也不会对它做任何转换,但是如果你是写:

1
4 < 5

Markdown 将会把它转换为:

1
4 &lt; 5

温馨提示

==在 Markdown 的块级元素和内联元素中, <& 两个符号都会被自动转换成 HTML 实体。==

内嵌HTML标签

注:对于 Markdown 涵盖范围之外的标签,都可以直接在文件里面用 HTML 本身。如需使用 HTML,不需要额外标注这是 HTML 或是 Markdown,只需 HTML 标签添加到 Markdown 文本中即可。

行级内联标签

HTML 的行级內联标签如 ==<span>==、==<cite>==、==<del>== 不受限制,可以在 Markdown 的段落、列表或是标题里任意使用。依照个人习惯,甚至可以不用 Markdown 格式,而采用 HTML 标签来格式化

HTML 行级內联标签和区块标签不同,在內联标签的范围内, Markdown 的语法是可以解析的。

1
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 表格:

1
2
3
4
5
6
7
8
9
This is a regular paragraph.

<table>
<tr>
<td>Foo</td>
</tr>
</table>

This is another regular paragraph.

请注意,Markdown 语法在 HTML 区块标签中将不会被进行处理。例如,你无法在 HTML 区块内使用 Markdown 形式的*强调*(如下)。

This is a regular paragraph.

**Foo**

This is another regular paragraph.