Markdown语法速查表

Markdown语法速查表

基本语法

https://www.markdownguide.org/basic-syntax/#html

概述

几乎所有 Markdown 应用程序都支持原始 Markdown 设计文档中概述的基本语法。Markdown 处理器之间存在细微的差异和差异 - 只要有可能,这些差异和差异都会内联注明。

标题

要创建标题,请在单词或短语前面添加数字符号 (#)。您使用的数字符号的数量应与标题级别相对应。例如,要创建三级标题 ( <h3>),请使用三个数字符号(例如### My Header)。

Markdown HTML Rendered Output
# 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

替代语法

或者,在文本下方的行中,添加任意数量的==标题级别 1 字符或–标题级别 2 字符。

Markdown HTML Rendered Output
Heading level 1
===============
<h1>Heading level 1</h1>

Heading level 1

Heading level 2
---------------
<h2>Heading level 2</h2>

Heading level 2

标题最佳实践

Markdown 应用程序对于如何处理数字符号 (#) 和标题名称之间缺少的空格没有达成一致。为了兼容性,请始终在数字符号和标题名称之间添加空格。

✅  Do this ❌  Don't do this
# Here's a Heading #Here's a Heading

为了兼容性,您还应该在标题前后放置空行。

✅  Do this ❌  Don't do this
Try to put a blank line before...

# Heading

...and after a heading.
Without blank lines, this might not look right.
# Heading
Don't do this!

段落

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

Markdown HTML Rendered Output
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.

段落最佳实践

除非段落位于列表中,否则不要使用空格或制表符缩进段落。

注意:如果您需要在输出中缩进段落,请参阅有关如何缩进(制表符)的部分。

✅  Do this ❌  Don't do this
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 Rendered Output
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 应用程序中,您都可以使用两个或多个空格(通常称为“尾随空白”)作为换行符,但这是有争议的。在编辑器中很难看到尾随空格,许多人无意或故意在每个句子后添加两个空格。因此,您可能需要使用尾随空格以外的其他内容作为换行符。如果您的 Markdown 应用程序支持 HTML,您可以使用<br>HTML 标签。

为了兼容性,请在行尾使用尾随空格或HTML<br>标记。

我不建议使用其他两个选项。CommonMark 和其他一些轻量级标记语言允许您在行尾键入反斜杠 (),但并非所有 Markdown 应用程序都支持此功能,因此从兼容性角度来看,这不是一个很好的选择。至少有一些轻量级标记语言不需要在行尾添加任何内容——只需键入返回,它们就会创建一个换行符。

✅  Do this ❌  Don't do this
First line with two spaces after.  
And the next line.

First line with the HTML tag after.<br>
And the next line.
First line with a backslash after.\
And the next line.

First line with nothing after.
And the next line.

强调

您可以通过将文本设为粗体或斜体来强调。

粗体

要加粗文本,请在单词或短语前后添加两个星号或下划线。要加粗单词的中间以强调,请在字母周围添加两个星号,且字母周围不留空格。

Markdown HTML Rendered Output
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 应用程序对于如何处理单词中间的下划线没有达成一致。为了兼容性,请使用星号将单词中间加粗以示强调。

✅  Do this ❌  Don't do this
Love**is**bold Love__is__bold

斜体

要将文本设置为斜体,请在单词或短语之前和之后添加一个星号或下划线。要使单词的中间斜体以强调,请在字母周围添加一个星号,且字母周围不留空格。

Markdown HTML Rendered Output
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 Acatmeow

斜体最佳实践

Markdown 应用程序对于如何处理单词中间的下划线没有达成一致。为了兼容性,请使用星号将单词中间斜体化以强调。

✅  Do this ❌  Don't do this
A*cat*meow A_cat_meow

粗体和斜体

要同时用粗体和斜体强调文本,请在单词或短语前后添加三个星号或下划线。要将单词中间加粗和斜体以强调,请在字母周围添加三个星号,且字母周围不留空格。

Markdown HTML Rendered Output
This text is ***really important***. This text is <em><strong>really important</strong></em>. This text is really important.
This text is ___really important___. This text is <em><strong>really important</strong></em>. This text is really important.
This text is __*really important*__. This text is <em><strong>really important</strong></em>. This text is really important.
This text is **_really important_**. This text is <em><strong>really important</strong></em>. This text is really important.
This is really***very***important text. This is really<em><strong>very</strong></em>important text. This is reallyveryimportant text.

注意:em和标签的顺序strong可能会颠倒,具体取决于您使用的 Markdown 处理器。

粗体和斜体最佳实践

Markdown 应用程序对于如何处理单词中间的下划线没有达成一致。为了兼容性,请使用星号将单词中间加粗和斜体以强调。

✅  Do this ❌  Don't do this
This is really***very***important text. This is really___very___important text.

块引用

要创建块引用,请在段落前面添加>

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
7
> #### 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.

块引用最佳实践

为了兼容性,请在块引号之前和之后放置空行。

✅  Do this ❌  Don't do this
Try to put a blank line before...

> This is a blockquote

...and after a blockquote.
Without blank lines, this might not look right.
> This is a blockquote
Don't do this!

列表

您可以将项目组织到有序列表和无序列表中。

有序列表

要创建有序列表,请添加带有数字和句点的行项目。数字不必按数字顺序排列,但列表应从数字 1 开始。

Markdown HTML Rendered Output
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
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
5. First item
6. Second item
7. Third item
8. 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
9. First item
10. Second item
11. Third item
    1. Indented item
    2. Indented item
12. 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

有序列表最佳实践

CommonMark 和其他一些轻量级标记语言允许您使用括号 ( )) 作为分隔符(例如1) First item),但并非所有 Markdown 应用程序都支持这一点,因此从兼容性角度来看,这不是一个很好的选择。为了兼容性,仅使用句点。

✅  Do this ❌  Don't do this
1. First item
2. Second item
1) First item
2) Second item

无序列表

要创建无序列表,请在行项目前面添加破折号 ( -)、星号 ( *) 或加号 (+ )。缩进一项或多项以创建嵌套列表。

Markdown HTML Rendered Output
- 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 HTML Rendered Output
- 1968\. A great year!
- I think 1969 was second best.
<ul>
  <li>1968. A great year!</li>
  <li>I think 1969 was second best.</li>
</ul>
  • 1968. A great year!
  • I think 1969 was second best.

无序列表最佳实践

Markdown 应用程序对于如何处理同一列表中的不同分隔符并不一致。为了兼容性,不要在同一列表中混合和匹配分隔符 - 选择一个并坚持使用。

✅  Do this ❌  Don't do this
- First item
- Second item
- Third item
- Fourth item
+ First item
* Second item
- Third item
+ Fourth item

在列表中添加元素

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

提示:如果内容未按您预期的方式显示,请仔细检查是否已将列表中的元素缩进四个空格或一个制表符。

段落

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
1. Open the file.
2. Find the following code block on line 21:

<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:

     <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 Rendered Output
At the command prompt, type `nano`. At the command prompt, type <code>nano</code>. At the command prompt, type nano.

转义反引号

如果您想要表示为代码的单词或短语包含一个或多个反引号,则可以通过将单词或短语括在双反引号 (``) 中来转义它。

Markdown HTML Rendered Output
``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>

渲染的输出如下所示:

<html>
<head>
</head>
</html>

注意:要创建不缩进的代码块,请使用围栏代码块

水平线

要创建水平线,请在一行上单独使用三个或更多星号 ( ***)、—破折号 ( ) 或下划线 ( )。___

1
2
3
4
5
***

---

_________________

所有三个的渲染输出看起来相同:


水平规则最佳实践

为了兼容性,请在水平线之前和之后放置空行。

✅  Do this ❌  Don't do this
Try to put a blank line before...

---

...and after a horizontal rule.
Without blank lines, this would be a heading.

---

Don't do this!

链接

要创建链接,请将链接文本括在方括号中(例如,[Duck Duck Go]),然后紧随其后,将 URL 放在括号中(例如,(https://duckduckgo.com))。

1
My favorite search engine is [Duck Duck Go](https://duckduckgo.com).

渲染的输出如下所示:

My favorite search engine is Duck Duck Go.

注意:要链接到同一页面上的元素,请参阅链接到标题ID。要创建在新选项卡或窗口中打开的链接,请参阅有关链接目标的部分。

添加标题

您可以选择为链接添加标题。当用户将鼠标悬停在链接上时,这将显示为工具提示。要添加标题,请将其放在 URL 后面的引号中。

1
My favorite search engine is [Duck Duck Go](https://duckduckgo.com "The best search engine for privacy").

渲染的输出如下所示:

My favorite search engine is Duck Duck Go.

URL 和电子邮件地址

要快速将 URL 或电子邮件地址转换为链接,请将其括在尖括号中。

1
2
<https://www.markdownguide.org>
<fake@example.com>

渲染的输出如下所示:

https://www.markdownguide.org
fake@example.com

设置链接格式

强调链接,请在方括号和括号前后添加星号。要将链接表示为code,请在括号中添加反引号。

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 中显示和阅读。参考样式链接由两部分构成:与文本内联的部分和存储在文件中其他位置以使文本易于阅读的部分。

设置链接第一部分的格式

参考样式链接的第一部分采用两组括号格式化。第一组括号包围应显示为链接的文本。第二组括号显示一个标签,用于指向您存储在文档中其他位置的链接。

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

这意味着以下示例格式与链接的第一部分大致相同:

1
2
[hobbit-hole][1]
[hobbit-hole] [1]

设置链接第二部分的格式

引用样式链接的第二部分采用以下属性进行格式化:

  1. 标签位于方括号中,后紧跟一个冒号和至少一个空格(例如,[label]: )。
  2. 链接的 URL,您可以选择将其括在尖括号中。
  3. 链接的可选标题,可以用双引号、单引号或括号括起来。

这意味着以下示例格式对于链接的第二部分大致相同:

1
2
3
4
5
6
7
[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 文档中的任何位置。有些人将它们放在它们出现的段落之后,而另一些人将它们放在文档的末尾(如尾注或脚注)。

将各个部分组合在一起的示例

假设您将 URL 作为标准 URL 链接添加到段落中,它在 Markdown 中如下所示:

1
2
3
In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends
of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to
eat: it was a [hobbit-hole](https://en.wikipedia.org/wiki/Hobbit#Lifestyle "Hobbit lifestyles"), and that means comfort.

尽管它可能指向有趣的附加信息,但显示的 URL 除了使其更难以阅读之外,实际上并没有给现有的原始文本添加太多内容。要解决此问题,您可以像这样设置 URL 格式:

1
2
3
4
5
In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends
of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to
eat: it was a [hobbit-hole][1], and that means comfort.

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

在上面的两个实例中,渲染的输出将是相同的:

In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends
of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to
eat: it was a hobbit-hole, and that means comfort.

链接的 HTML 为:

1
<a href="https://en.wikipedia.org/wiki/Hobbit#Lifestyle" title="Hobbit lifestyles">hobbit-hole</a>

链接最佳实践

Markdown 应用程序对于如何处理 URL 中间的空格并不一致。为了兼容性,请尝试对任何空格进行 URL 编码%20。或者,如果您的 Markdown 应用程序支持 HTML,您可以使用 HTML a标签。

✅  Do this ❌  Don't do this
[link](https://www.example.com/my%20great%20page)

<a href="https://www.example.com/my great page">link</a>
[link](https://www.example.com/my great page)

URL 中间的括号也可能会出现问题。为了兼容性,请尝试对左括号 ( ()进行 URL 编码%28,对右括号 ( )) 进行URL 编码%29。或者,如果您的 Markdown 应用程序支持 HTML,您可以使用HTML a标签。

✅  Do this ❌  Don't do this
[a novel](https://en.wikipedia.org/wiki/The_Milagro_Beanfield_War_%28novel%29)

<a href="https://en.wikipedia.org/wiki/The_Milagro_Beanfield_War_(novel)">a novel</a>
[a novel](https://en.wikipedia.org/wiki/The_Milagro_Beanfield_War_(novel))

图片

要添加图像,请添加感叹号 ( !),后跟括号中的替代文本,并在括号中添加图像资源的路径或 URL。您可以选择在路径或 URL 后面添加带引号的标题。

1
![The San Juan Mountains are beautiful!](https://www.markdownguide.org/assets/images/san-juan-mountains.jpg "San Juan Mountains")

渲染的输出如下所示:
The San Juan Mountains are beautiful!

注意:要调整图像大小,请参阅有关图像大小的部分。要添加标题,请参阅有关图像标题的部分。

链接图像

要添加图像链接,请将图像的 Markdown 括在括号中,然后在括号中添加链接。

1
[![An old rock in the desert](/assets/images/shiprock.jpg "Shiprock, New Mexico by Beau Rogers")](https://www.flickr.com/photos/beaurogers/31833779864/in/photolist-Qv3rFw-34mt9F-a9Cmfy-5Ha3Zi-9msKdv-o3hgjr-hWpUte-4WMsJ1-KUQ8N-deshUb-vssBD-6CQci6-8AFCiD-zsJWT-nNfsgB-dPDwZJ-bn9JGn-5HtSXY-6CUhAL-a4UTXB-ugPum-KUPSo-fBLNm-6CUmpy-4WMsc9-8a7D3T-83KJev-6CQ2bK-nNusHJ-a78rQH-nw3NvT-7aq2qf-8wwBso-3nNceh-ugSKP-4mh4kh-bbeeqH-a7biME-q3PtTf-brFpgb-cg38zw-bXMZc-nJPELD-f58Lmo-bXMYG-bz8AAi-bxNtNT-bXMYi-bXMY6-bXMYv)

渲染的输出如下所示:

An old rock in the desert

转义字符

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

1
\*  如果没有反斜杠,这将是无序列表中的项目符号。

渲染的输出如下所示:

* 如果没有反斜杠,这将是无序列表中的项目符号。

你可以转义的字符

您可以使用反斜杠转义以下字符。

特点 姓名
\ 反斜杠
` 反引号(另请参阅代码中转义反引号)
* 星号
_ 下划线
{ } 大括号
[ ] 括号
< > 尖括号
( ) 括号
# 英镑符号
+ 加号
- 减号(连字符)
感叹号
| 管道(另请参阅表中的转义管道)

HTML

许多 Markdown 应用程序允许您在 Markdown 格式的文本中使用 HTML 标签。如果您更喜欢某些 HTML 标签而不是 Markdown 语法,这会很有帮助。例如,有些人发现对图像使用 HTML 标签更容易。当您需要更改元素的属性(例如指定文本的颜色或更改图像的宽度)时,使用 HTML 也很有帮助。

要使用 HTML,请将标签放置在 Markdown 格式文件的文本中。

1
This **word** is bold. This <em>word</em> is italic.

渲染的输出如下所示:

This word is bold. This word is italic.

HTML 最佳实践

出于安全原因,并非所有 Markdown 应用程序都支持 Markdown 文档中的 HTML。如有疑问,请检查 Markdown 应用程序的文档。某些应用程序仅支持 HTML 标记的子集。

使用空行将块级 HTML 元素(如<div><table><pre><p>)与周围的内容分开。尽量不要用制表符或空格缩进标签——这可能会干扰格式。

您不能在块级 HTML 标记内使用 Markdown 语法。例如,<p>italic and **bold**</p>不会起作用。

扩展语法

https://www.markdownguide.org/extended-syntax/

概述

原始 Markdown 设计文档中概述的基本语法添加了日常所需的许多元素,但这对某些人来说还不够。这就是扩展语法的用武之地。

一些个人和组织自行扩展了基本语法,添加了表格、代码块、语法突出显示、URL 自动链接和脚注等附加元素。这些元素可以通过使用基于基本 Markdown 语法的轻量级标记语言或通过向兼容的 Markdown 处理器添加扩展来启用。

可用性

并非所有 Markdown 应用程序都支持扩展语法元素。您需要检查您的应用程序使用的轻量级标记语言是否支持您想要使用的扩展语法元素。如果没有,您仍然可以在 Markdown 处理器中启用扩展。

轻量级标记语言

有几种轻量级标记语言是Markdown 的超集。它们包括基本语法,并在此基础上添加其他元素,如表格、代码块、语法突出显示、URL 自动链接和脚注。许多最流行的 Markdown 应用程序都使用以下轻量级标记语言之一:

Markdown 处理器

数十种 Markdown 处理器可用。其中许多允许您添加启用扩展语法元素的扩展。检查处理器的文档以获取更多信息。

表格

要添加表,请使用三个或更多连字符 ( ---) 创建每列的标题,并使用竖线 (|) 分隔每列。为了兼容性,您还应该在行的两端添加管道。

1
2
3
4
| Syntax      | Description |
| ----------- | ----------- |
| Header | Title |
| Paragraph | Text |

渲染的输出如下所示:

Syntax Description
Header Title
Paragraph Text

单元格宽度可能会有所不同,如下所示。渲染的输出看起来是一样的。

1
2
3
4
| Syntax | Description |
| --- | ----------- |
| Header | Title |
| Paragraph | Text |

提示:创建带有连字符和竖线的表可能很乏味。要加快该过程,请尝试使用Markdown Tables GeneratorAnyWayData Markdown Export。使用图形界面构建表格,然后将生成的 Markdown 格式文本复制到文件中。

对齐

您可以通过在标题行中连字符的左侧、右侧或两侧添加冒号 (:),将列中的文本向左、向右或居中对齐。

1
2
3
4
| Syntax      | Description | Test Text     |
| :--- | :----: | ---: |
| Header | Title | Here's this |
| Paragraph | Text | And more |

渲染的输出如下所示:

Syntax Description Test Text
Header Title Here’s this
Paragraph Text And more

设置表格中文本的格式

您可以设置表格内文本的格式。例如,您可以添加链接代码反引号(`) 中的单词或短语,而不是代码块。

您不能使用标题、块引用、列表、水平线、图像或大多数 HTML 标记。

提示:您可以使用 HTML 创建换行符并在表格单元格内添加列表。

转义表格中的管道字符

你可以使用HTML字符代码 (&#124;)在表中显示竖线(|

围栏代码块

The basic Markdown syntax allows you to create code blocks by indenting lines by four spaces or one tab. If you find that inconvenient, try using fenced code blocks. Depending on your Markdown processor or editor, you’ll use three backticks (```) or three tildes (~~~) on the lines before and after the code block. The best part? You don’t have to indent any lines!
基本的 Markdown 语法允许您通过将行缩进四个空格或一个制表符来创建代码块。如果您发现这不方便,请尝试使用受隔离的代码块。根据您的 Markdown 处理器或编辑器,您将在代码块之前和之后的行上使用三个反引号 ( ```) 或三个波形符 ( ~~~)。最好的部分?您不必缩进任何行!

1
2
3
4
5
6
7
```
{
"firstName": "John",
"lastName": "Smith",
"age": 25
}
```

渲染的输出如下所示:

1
2
3
4
5
{
"firstName": "John",
"lastName": "Smith",
"age": 25
}

提示:需要在代码块内显示反引号吗?请参阅本节以了解如何逃避它们。

语法高亮

许多 Markdown 处理器支持围栏代码块的语法突出显示。此功能允许您为编写代码所用的任何语言添加颜色突出显示。要添加语法突出显示,请在受隔离的代码块之前的反引号旁边指定一种语言。

1
2
3
4
5
6
7
```json
{
"firstName": "John",
"lastName": "Smith",
"age": 25
}
```

渲染的输出如下所示:

1
{  "firstName":  "John",  "lastName":  "Smith",  "age":  25  } 

脚注

脚注允许您添加注释和参考文献,而不会弄乱文档的正文。创建脚注时,您添加脚注引用的位置会出现带有链接的上标编号。读者可以点击链接跳转至页面底部脚注内容。

要创建脚注引用,请在方括号 ([^1]) 内添加插入符号和标识符。标识符可以是数字或单词,但不能包含空格或制表符。标识符仅将脚注引用与脚注本身相关联 - 在输出中,脚注按顺序编号。

使用另一个插入符号和括号内的数字以及冒号和文本 ([^1]: 脚注笔记xxx.) 添加脚注。您不必在文档末尾添加脚注。您可以将它们放在除列表、块引用和表格等其他元素内之外的任何位置。

1
2
3
4
5
6
7
8
9
10
11
这是一个简单的脚注,[^1] 这是另一个更长的脚注.[^bignote]

[^1]: 这是第一个脚注.

[^bignote]: 这是一个有多段落和代码的脚注。

缩进段落以将其置于脚注内。

`{ test code }`

添加任意数量段落。

渲染的输出如下所示:

这是一个简单的脚注,^1 这是另一个更长的脚注.^bignote

缩进段落以将其置于脚注内。

`{ test code }`

添加任意数量段落。 

标题ID

许多 Markdown 处理器支持自定义标题ID——一些 Markdown 处理器会自动添加它们。添加自定义 ID 允许您直接链接到标题并使用 CSS 修改它们。要添加自定义标题 ID,请将自定义 ID 括在与标题同一行的大括号中。

1
### My Great Heading {#custom-id} 

HTML 看起来像这样:

1
<h3 id="custom-id">My Great Heading</h3> 

链接到标题ID

您可以通过创建一个带有数字符号 (#)后跟自定义标题ID 的标准链接来链接到文件中具有自定义 ID 的标题。这些通常称为锚链接。

Markdown HTML Rendered Output
[标题ID](#标题ID) <a href="#标题ID">标题ID</a> Heading IDs

其他网站可以通过将自定义标题 ID 添加到网页的完整 URL 来链接到该标题(例如,标题 ID)。

定义列表

某些 Markdown 处理器允许您创建术语及其相应定义的定义列表。要创建定义列表,请在第一行键入术语。在下一行中,键入冒号,后跟空格和定义。

1
2
3
4
5
6
First Term
: This is the definition of the first term.

Second Term
: This is one definition of the second term.
: This is another definition of the second term.

HTML 看起来像这样:

1
2
3
4
5
6
7
<dl>
<dt>First Term</dt>
<dd>This is the definition of the first term.</dd>
<dt>Second Term</dt>
<dd>This is one definition of the second term. </dd>
<dd>This is another definition of the second term.</dd>
</dl>

渲染的输出如下所示:

First Term
This is the definition of the first term.
Second Term
: This is one definition of the second term.
This is another definition of the second term.

删除线

您可以通过在单词中心划一条水平线来删除单词。结果看起来像这样。此功能允许您指出某些单词是错误的,不应包含在文档中。要删除单词,请在单词前后使用两个波浪号 (~~)。

1
~~The world is flat.~~ We now know that the world is round. 

渲染的输出如下所示:

The world is flat. We now know that the world is round.

任务列表

任务列表(也称为清单和待办事项列表)允许您创建带有复选框的项目列表。在支持任务列表的 Markdown 应用程序中,复选框将显示在内容旁边。要创建任务列表,请在任务列表项前面添加破折号 (-) 和方括号以及空格 ([ ])。要选择复选框,请在方括号 ([x]) 之间添加 x。

1
2
3
- [x] Write the press release
- [ ] Update the website
- [ ] Contact the media

渲染的输出如下所示:

  • Write the press release
  • Update the website
  • Contact the media

Emoji

有两种方法可以将表情符号添加到 Markdown 文件中:将表情符号复制并粘贴到 Markdown 格式的文本中,或者输入表情符号简码。

复制和粘贴表情符号

在大多数情况下,您只需从Emojipedia等来源复制表情符号并将其粘贴到文档中即可。许多 Markdown 应用程序会自动以 Markdown 格式的文本显示表情符号。从 Markdown 应用程序导出的 HTML 和 PDF 文件应显示表情符号。

提示:如果您使用静态站点生成器,请确保将HTML页面编码为 UTF-8。

使用表情符号短代码

某些 Markdown 应用程序允许您通过输入表情符号短代码来插入表情符号。它们以冒号开头和结尾,并包含表情符号的名称。

1
2
3
Gone camping! :tent: Be back soon.

That is so funny! :joy:

渲染的输出如下所示:

Gone camping! Be back soon.

That is so funny! 😂

注意:您可以使用此表情符号短代码列表,但请记住,表情符号短代码因应用程序而异。有关详细信息,请参阅 Markdown 应用程序的文档。

强调

这并不常见,但某些 Markdown 处理器允许您突出显示文本。结果看起来像这样。要突出显示单词,请在单词前后使用两个等号 (==)。

1
I need to highlight these ==very important words==. 

渲染的输出如下所示:

I need to highlight these ==very important words==.

或者,如果您的 Markdown 应用程序支持 HTML,您可以使用 mark HTML 标签

1
I need to highlight these <mark>very important words</mark>. 

下标

这并不常见,但某些 Markdown 处理器允许您使用下标将一个或多个字符定位在略低于正常类型行的位置。要创建下标,请在字符前后使用一个波浪号 (~)。

1
H~2~O

渲染的输出如下所示:

H2O

提示: 使用之前,请务必在 Markdown 应用程序中对其进行测试。某些 Markdown 应用程序在单词前后使用一个波形符,不是用于下标,而是用于删除线。

或者,如果您的 Markdown 应用程序支持 HTML,您可以使用 sub HTML 标签。

1
H<sub>2</sub>O

上标

这并不常见,但某些 Markdown 处理器允许您使用上标将一个或多个字符放置在稍高于正常类型行的位置。要创建上标,请在字符前后使用一个插入符号 (^)。

1
X^2^

渲染的输出如下所示:

X^2^

或者,如果您的 Markdown 应用程序支持 HTML,您可以使用sup HTML 标签。

1
X<sup>2</sup>

自动 URL 链接

许多 Markdown 处理器会自动将 URL 转换为链接。这意味着,如果您输入 http://www.example.com,即使您没有使用括号,您的 Markdown 处理器也会自动将其转换为链接。

1
http://www.example.com

渲染的输出如下所示:

http://www.example.com

禁用自动 URL 链接

如果您不希望自动链接某个 URL,则可以通过将 URL 表示为带反引号的代码来删除该链接。

渲染的输出如下所示:

http://www.example.com

作者

zhang

发布于

2021-10-21

更新于

2023-09-19

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×