Markdown 教程

本人关于使用Markdown的总结。


1. 简介

Markdown 是一种轻量级标记语言,允许人们使用易读易写的纯文本格式编写文档。

在 2004 由约翰·格鲁伯(英语:John Gruber)创建。

可与HTML混编,可导出 HTML、PDF 以及本身的 .md 格式的文件。

因简洁、高效、易读、易写,Markdown被大量使用,如Github、Wikipedia、简书等。

有关语法规则,请参阅 John 的 语法文档

2. 基本语法

2.1. 标题

要创建标题,请在单词或短语前面添加井号 (#) 。#的数量代表了标题的级别。例如,添加三个 # 表示创建一个三级标题 (<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 应用程序处理 # 和标题之间的空格方式并不一致。为了兼容考虑,请用一个空格在 # 和标题之间进行分隔。

2.2. 段落和换行

2.2.1. 段落

要创建段落,请使用空白行将一行或多行文本进行分隔。 不要用空格(spaces)或制表符( tabs)缩进段落。

2.2.2. 换行

在一行的末尾添加两个或多个空格,然后按回车键,即可创建一个换行(<br>)。

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

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

2.3. 字体

通过将文本设置为粗体或斜体来强调其重要性。

2.3.1. 粗体

要加粗文本,请在单词或短语的前后各添加两个星号(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

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

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

2.3.2. 斜体

要用斜体显示文本,请在单词或短语前后添加一个星号(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 Acatmeow

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

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

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

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 reallyveryimportant text.

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

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

2.3.3. 下划线

下划线可以通过 HTML 的 <u> 标签来实现:

HTML 预览效果
<u>带下划线文本</u> 带下划线文本

2.4. 块引用

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

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

2.5. 列表

可以将多个条目组织成有序或无序列表。

2.5.1. 有序列表

要创建有序列表,请在每个列表项前添加数字并紧跟一个英文句点。数字不必按数学顺序排列,但是列表应当以数字 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 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

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

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

2.5.2. 无序列表

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

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 应用程序不同意如何处理同一列表中的不同分隔符。为了兼容性,不要在同一个列表中混合和匹配分隔符 -- 选择一个并坚持使用它。

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

2.5.3. 在列表中嵌套其他元素

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

*   This is the first list item.
*   Here's the second list item.

    I need to add another paragraph below the second list item.
    > A blockquote would look great below the second list item.

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

    ![Tux, the Linux mascot](/img/tux.png)

    1. First item
    2. Second item
    3. Third item
    4. Fourth 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.

    A blockquote would look great below the second list item.

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

    Tux, the Linux mascot

    1. First item
    2. Second item
    3. Third item
    4. Fourth item
  • And here's the third list item.

2.6. 代码语法

要将单词或短语表示为代码,请将其包裹在反引号 (`) 中。

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

Markdown语法 HTML 预览效果
At the command prompt, type `nano`. At the command prompt, type <code>nano</code>. At the command prompt, type nano.
``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>

Note: 要创建不用缩进的代码块,请使用 围栏式代码块(fenced code blocks).

2.7. 分隔线

您可以通过在一行上单独放置三个或更多连字符 (---) 、星号 (***)或下划线 (___)来生成水平规则标记 ( <hr />)。 如果您愿意,可以在 -* 之间使用空格。以下每一行都会产生一条水平线:

* * *

***

*****

- - -

---------------------------------------

_________________

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

Markdown 支持两种样式的链接:内联(inline)和引用(reference)。在这两种样式中,链接文本由 [方括号] 分隔。

要创建内联链接,请在链接文本的右方括号之后立即使用一组常规括号。在括号内,将 URL 放在您希望链接指向的位置,以及链接的可选标题,并用引号括起来。例如:

This is [an example](http://example.com/ "Title") inline link.

[This link](http://example.net/) has no title attribute.

将产生:

This is an example inline link.

This link has no title attribute.

如果您指的是同一服务器上的本地资源,则可以使用相对路径:

See my [About](/about/) page for details.

引用样式链接使用第二组方括号,您可以在其中放置您选择的标签来标识链接,可以选择使用空格来分隔括号组:

This is [an example][id] reference-style link.
This is [an example] [id] reference-style link.

然后,在文档中的任何位置,您都可以像这样在一行上定义链接标签:

[id]: http://example.com/  "Optional Title Here"

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

  • 包含链接标识符的方括号(可选从左边距缩进最多三个空格);
  • 后跟一个冒号;
  • 后跟一个或多个空格(或制表符);
  • 后跟链接的 URL;
  • 可选地后跟链接的标题属性,用双引号或单引号括起来,或者用括号括起来。

以下几种链接定义是等价的:

[foo]: http://example.com/  "Optional Title Here"
[foo]: http://example.com/  'Optional Title Here'
[foo]: http://example.com/  (Optional Title Here)

链接 URL 可以选择用尖括号括起来:

[id]: <http://example.com/>  "Optional Title Here"

您可以将 title 属性放在下一行并使用额外的空格或制表符进行填充,这对于较长的 URL 看起来会更好:

[id]: http://example.com/longish/path/to/resource/here
    "Optional Title Here"

链接定义仅用于在 Markdown 处理期间创建链接,并在 HTML 输出中从您的文档中删除。

链接定义名称可能包含字母、数字、空格和标点符号——但它们不区分大小写。例如这两个链接是等价的:

[link text][a]
[link text][A]

隐式链接方式允许您省略链接名称,在这种情况下,链接文本本身用作名称。只需使用一组空方括号——例如,将“Google”这个词链接到 google.com 网站,你可以简单地写:

[Google][]

[Google]: http://google.com/

因为链接名称可能包含空格,所以此快捷方式甚至适用于链接文本中的多个单词:

Visit [Daring Fireball][] for more information.

[Daring Fireball]: http://daringfireball.net/

Markdown 支持为 URL 和电子邮件地址创建“自动”链接的快捷方式:只需将 URL 或电子邮件地址用尖括号括起来。这意味着,如果您想显示 URL 或电子邮件地址的实际文本,并使其成为可点击的链接,您可以这样做:

<http://example.com/>

Markdown 会将其变成:

<a href="http://example.com/">http://example.com/</a>

电子邮件地址的自动链接的工作方式类似,除了 Markdown 还将执行一些随机的十进制和十六进制实体编码,以帮助从地址收集垃圾邮件机器人中隐藏您的地址。例如,Markdown 会将这样:

<address@example.com>

变成这样的东西:

<a href="&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:&#x61;&#x64;&#x64;&#x72;&#x65;
&#115;&#115;&#64;&#101;&#120;&#x61;&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;
&#109;">&#x61;&#x64;&#x64;&#x72;&#x65;&#115;&#115;&#64;&#101;&#120;&#x61;
&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;&#109;</a>

它将在浏览器中呈现为“address@example.com”的可点击链接。

(这种实体编码技巧确实会欺骗许多(如果不是大多数)地址收集机器人,但它肯定不会欺骗所有这些。总比没有好,但以这种方式发布的地址最终可能会开始接收垃圾邮件。)

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

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.

链接定义可以放在 Markdown 文档中的任何位置。我倾向于将它们放在使用它们的每个段落之后,但是如果您愿意,可以将它们全部放在文档的末尾,有点像脚注。 以下是实际参考链接的示例:

I get 10 times more traffic from [Google] [1] than from
[Yahoo] [2] or [MSN] [3].

  [1]: http://google.com/        "Google"
  [2]: http://search.yahoo.com/  "Yahoo Search"
  [3]: http://search.msn.com/    "MSN Search"

使用隐式链接名称快捷方式,您可以改为编写:

I get 10 times more traffic from [Google][] than from
[Yahoo][] or [MSN][].

  [google]: http://google.com/        "Google"
  [yahoo]:  http://search.yahoo.com/  "Yahoo Search"
  [msn]:    http://search.msn.com/    "MSN Search"

为了比较,这里是使用 Markdown 的内联链接样式编写的相同段落:

I get 10 times more traffic from [Google](http://google.com/ "Google")
than from [Yahoo](http://search.yahoo.com/ "Yahoo Search") or
[MSN](http://search.msn.com/ "MSN Search").

引用风格链接的重点不是它们更容易编写。关键是,使用引用样式的链接,您的文档源代码的可读性大大提高。比较上面的例子:使用引用样式的链接,段落本身只有 81 个字符长;使用内联样式的链接,它是 176 个字符;作为原始 HTML,它是 234 个字符。在原始 HTML 中,标记比文本多。

使用 Markdown 的引用样式链接,源文档更接近于最终输出,如在浏览器中呈现的那样。通过允许您将与标记相关的元数据移出段落,您可以添加链接而不会中断散文的叙述流程。

2.9. 图片语法

Markdown 使用的图像语法旨在类似于链接的语法,允许两种样式:内联(inline)和引用(reference)。

内联图像语法如下所示:

![Alt text](/path/to/img.jpg)

![Alt text](/path/to/img.jpg "Optional title")

内联图像语法格式:

  • 感叹号:!;
  • 后跟一组方括号,包含图像的属性文本;
  • 后跟一组括号,包含图像的 URL 或路径,以及 title 用双引号或单引号括起来的可选属性。

图像引用使用与链接引用相同的语法定义。引用风格的图像语法如下所示:

![Alt text][id]

[id]: url/to/image  "Optional title attribute"

其中“id”是定义的图像引用的名称。 在撰写本文时,Markdown 没有用于指定图像尺寸的语法。如果这对您很重要,您可以简单地使用常规 HTML<img>标签。

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

[![image text](/assets/img/shiprock.jpg "Optional title")](https://jerry-se.github.io/)

2.10. 转义字符

Markdown 允许使用反斜杠转义来生成文字字符,否则这些字符在 Markdown 的格式化语法中具有特殊含义。例如,如果想用文字星号(而不是 HTML<em>标记)包围一个单词,可以在星号之前使用反斜杠,如下所示:

\*literal asterisks\*

Markdown 为以下字符提供反斜杠转义:

Character Name
\ backslash
` backtick
* asterisk
_ underscore
{} curly braces
[] square brackets
() parentheses
# hash mark
+ plus sign
- minus sign (hyphen)
. dot
! exclamation mark

2.11. 内嵌 HTML 标签

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

2.11.1. 行级内嵌标签

HTML 的行级內联标签如 <span><cite><del> 不受限制,可以在 Markdown 的段落、列表或是标题里任意使用。依照个人习惯,甚至可以不用 Markdown 格式,而采用 HTML 标签来格式化。例如:如果比较喜欢 HTML 的 <a><img> 标签,可以直接使用这些标签,而不用 Markdown 提供的链接或是图片语法。当你需要更改元素的属性时(例如为文本指定颜色或更改图像的宽度),使用 HTML 标签更方便些。

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

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

2.11.2. 块级标签

区块元素──比如 <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 区块标签中将不会被进行处理。例如<p>italic and **bold**</p>,你无法在 HTML 区块内使用 Markdown 形式的强调

2.11.3. 折叠示例

<details>
<summary>展开查看</summary>
AAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAA

AAAAAAAAAAAAAAAAAAAAAAAAAAAA

</details>

2.11.4. 可用性

出于安全原因,并非所有 Markdown 应用程序都支持在 Markdown 文档中添加 HTML。如有疑问,请查看相应 Markdown 应用程序的手册。某些应用程序只支持 HTML 标签的子集。

3. 扩展语法

John Gruber的原始设计文档中概述的基本语法主要是为了应付大多数情况下的日常所需元素,但对于某些人来说还不够,这就是扩展语法的用武之地。

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

3.1. 扩展的可用性

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

有几种轻量级标记语言是Markdown的超集。它们包含Gruber的基本语法,并通过添加其他元素(例如表,代码块,语法突出显示,URL自动链接和脚注)在此基础上构建。许多最受欢迎的Markdown应用程序使用以下轻量级标记语言之一:

有许多Markdown处理器可用。它们中的许多允许您添加启用扩展语法元素的扩展。查看您所使用处理器的文档以获取更多信息。

3.2. 表格

要添加表,请使用三个或多个连字符(---)创建每列的标题,并使用管道(|)分隔每列。您可以选择在表的任一端添加管道,单元格宽度可以变化。 使用连字符和管道创建表可能很麻烦。为了加快该过程,请尝试使用Markdown Tables Generator。使用图形界面构建表,然后将生成的Markdown格式的文本复制到文件中。

name | age
---- | ---
LearnShare | 12
Mike |  32

在表头下方的分隔线标记中加入 :,即可标记下方单元格内容的对齐方式:

  • :--- 代表左对齐
  • :--: 代表居中对齐
  • ---: 代表右对齐
| left | center | right |
| :--- | :----: | ----: |
| aaaa | bbbbbb | ccccc |
| a    | b      | c     |

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

您不能添加标题,块引用,列表,水平规则,图像或HTML标签。

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

3.3. 围栏代码块

Markdown基本语法允许您通过将行缩进四个空格或一个制表符来创建代码块。如果发现不方便,请尝试使用受保护的代码块。根据Markdown处理器或编辑器的不同,您将在代码块之前和之后的行上使用三个反引号(```)或三个波浪号(~~~)。

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

效果如下:

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

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

```json
{
"firstName": "John",
"lastName": "Smith",
"age": 25
}
```

效果如下:

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

支持以下语言种类:

bash
clojure,cpp,cs,css
dart,dockerfile, diff
erlang
go,gradle,groovy
haskell
java,javascript,json,julia
kotlin
lisp,lua
makefile,markdown,matlab
objectivec
perl,php,python
r,ruby,rust
scala,shell,sql,swift
tex,typescript
verilog,vhdl
xml
yaml

3.4. 脚注

脚注使您可以添加注释和参考,而不会使文档正文混乱。当您创建脚注时,带有脚注的上标数字会出现在您添加脚注参考的位置。读者可以单击链接以跳至页面底部的脚注内容。

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

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

Here's a simple footnote,[^1] and here's a longer one.[^bignote]

[^1]: This is the first footnote.

[^bignote]: Here's one with multiple paragraphs and code.

    Indent paragraphs to include them in the footnote.

    `{ my code }`

    Add as many paragraphs as you like.

效果如下:

Here's a simple footnote,1 and here's a longer one.2

3.5. 标题编号

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

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

HTML看起来像这样:

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

通过创建带有数字符号(#)和自定义标题ID的标准链接,可以链接到文件中具有自定义ID的标题。

Markdown语法 HTML 预览效果
[Heading IDs](#heading-ids) <a href="#heading-ids">Heading IDs</a> Heading IDs

其他网站可以通过将自定义标题ID添加到网页的完整URL(例如[Heading IDs](https://jerry-se.github.io/markdown/#heading-ids))来链接到标题。

3.6. 定义列表

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

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看起来像这样:

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

3.7. 删除线

您可以通过在单词中心放置一条水平线来删除单词。结果看起来像这样。此功能使您可以指示某些单词是一个错误,要从文档中删除。若要删除单词,请在单词前后使用两个波浪号~~

~~这里是删除线~~

3.8. 任务列表

任务列表使您可以创建带有复选框的项目列表。在支持任务列表的Markdown应用程序中,复选框将显示在内容旁边。要创建任务列表,请在任务列表项之前添加破折号-和方括号[ ],并在[ ]前面加上空格。要选择一个复选框,请在方括号[x]之间添加 x

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

效果如下:

  • [x] Write the press release
  • [ ] Update the website
  • [ ] Contact the media

3.9. Emoji表情

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

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

Tips: 如果您使用的是静态网站生成器,请确保将HTML页面编码为UTF-8。

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

去露营了! :tent: 很快回来。

真好笑! :joy:

效果如下:

去露营了!⛺很快回来。

真好笑!😂

Tips: 您可以使用此表情符号简码列表,但请记住,表情符号简码因应用程序而异。有关更多信息,请参阅Markdown应用程序的文档。

3.10. 数学公式

LaTeX是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由TeX所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。

MathJax是一个显示网络上数学公式的开源JavaScript引擎库,它可以在所有浏览器上面工作,其中就支持LaTeX,MathML和AsciiMath 符号,里面的数字会被MathJax使用JavaScript引擎解析成HTML,SVG或者是MathML 方程式,然后在现代的浏览器里面显示。 它的设计目标是利用最新的web技术,构建一个支持math的web平台。支持主要的浏览器和操作系统,包括那些移动设备。

KaTeX: 可汗学院出品,号称“最快”的数学公式渲染库。支持主流的浏览器:Chrome, Firefox, Safari, Opera和 IE8-IE11。有以下优点:

  • 快速:并发渲染,无需重排页面。根据这个测试,性能绝对秒杀MathJax;
  • 渲染效果好:采用TeX语法,渲染效果达到印刷出版级别;
  • 无依赖:不依赖其它库;
  • 支持服务器端渲染:例如,服务器端的Node.js程序调用KaTeX,把渲染好的HTML片段直接发送给客户端。

VS Code的 Markdown preview enhanced(MPE)插件支持 MathJaxKaTeX 两类渲染引擎。总结来说,两者的特点如下:

  • MathJax 渲染更快,支持若干简写字符,但是本身支持的范围较小;
  • KaTeX 渲染稍慢,支持更多复杂的表示

另外,Typora用的也是 MathJax 引擎,却能实现更多的功能,不知道是不是版本问题。

参考资料:

行内 MathJax 公式使用方法,比如这个化学公式:$\ce{Hg^2+ ->[I-] HgI2 ->[I-] [Hg^{II}I4]^2-}$

MathJax 块公式使用方法如下:

$$H(D_2) = -\left(\frac{2}{4}\log_2 \frac{2}{4} + \frac{2}{4}\log_2 \frac{2}{4}\right) = 1$$

矩阵:

$$
\begin{pmatrix}
1 & a_1 & a_1^2 & \cdots & a_1^n \\
1 & a_2 & a_2^2 & \cdots & a_2^n \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
1 & a_m & a_m^2 & \cdots & a_m^n \\
\end{pmatrix}
$$

This is an extension for Python Markdown which adds KaTeX support.

```math
f(x) = \int_{-\infty}^\infty
    \hat f(\xi)\,e^{2 \pi i \xi x}
    \,d\xi
```

很多平台,比如知乎、CSDN支持公式,对于微信不支持这种不支持的,目前的解决方案是转成 svg 图片放到微信中,无需调整,矢量不失真。

4. Python-Markdown

4.1. 介绍

这是 John Gruber 的 Markdown 的 Python 实现。尽管存在一些已知问题,但它几乎完全符合参考实现。 有关确切支持和不支持的信息,请参阅功能可用扩展支持附加功能。

4.2. 文档

pip install markdown
import markdown
html = markdown.markdown(your_text_string)

有关更高级的安装使用文档, 请参阅 docs/ 发行版目录或项目网站https://Python-Markdown.github.io/

Third Party Extensions

5. 参考资料

Markdown 官方教程


  1. This is the first footnote. 

  2. Here's one with multiple paragraphs and code.

    Indent paragraphs to include them in the footnote.

    { my code }

    Add as many paragraphs as you like.