Markdown入门手册

Markdown入门手册

简介:Markdown 是一种轻量级的标记语言,可用于在纯文本文档中添加格式化元素。能将文本换成有效的XHTML(或者HTML)文档,它的目标是实现易读易写,成为一种适用于网络的书写语言。

本文所使用编辑器为Typora

基本语法

标题

Markdown分为六级标题。

在Typora中可以使用快捷键Ctrl+数字实现设置对应标题

1
2
3
4
5
6
#  H1 标题(Ctrl + 1
## H2 标题(Ctrl + 2
### H3 标题(Ctrl + 3
#### H4 标题(Ctrl + 4
##### H5 标题(Ctrl + 5
###### H6 标题(Ctrl + 6

效果如下:

H1 标题

H2 标题

H3 标题

H4 标题

H5 标题
H6标题

Markdown支持两种标题的语法,上面是类Atx,下面是类Setext形式

-=数目任意,最好三个及以上,比较直观

1
2
3
4
5
Heading level 1
===============

Heading level 2
---------------

效果如下图:

Heading level 1

Heading level 2


  • 建议:不同的 Markdown 应用程序处理 # 和标题之间的空格方式并不一致。为了兼容考虑,请用一个空格在 # 和标题之间进行分隔。

换行语法

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

md3

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

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

还有两种其他方式我并不推荐使用。CommonMark 和其它几种轻量级标记语言支持在行尾添加反斜杠 (\) 的方式实现换行,但是并非所有 Markdown 应用程序都支持此种方式,因此从兼容性的角度来看,不推荐使用。并且至少有两种轻量级标记语言支持无须在行尾添加任何内容,只须键入回车键(return)即可实现换行。

效果如下:

​ 这是回车换行

​ 这是标签换行

强调语法

粗体

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

1
2
3
** *’号加粗 **

__ 下划线加粗 __

效果如下:

这是星号加粗

这是下划线加粗

斜体

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

1
2
3
*‘***’*

_'___'_

效果如下:

这是星号斜体

这是下划线斜体

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

斜体加粗体使用三个*号来标识

1
***斜体加粗***

效果如下:

这是斜体加粗!!!

引用语法

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

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 起始。

1
2
3
4
1. First item
2. Second item
3. Third item
4. Fourth item

效果如下:

  1. First item
  2. Second item
  3. Third item
  4. Fourth item
1
2
3
4
1. First item
1. Second item
1. Third item
1. Fourth item

效果如下:

  1. First item
  2. Second item
  3. Third item
  4. Fourth item

无序列表

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

1
2
3
4
- First item
- Second item
- Third item
- Fourth item

效果如下:

  • First item
  • Second item
  • Third item
  • Fourth item
1
2
3
4
* First item
* Second item
* Third item
* Fourth item

效果如下:

  • First item
  • Second item
  • Third item
  • Fourth item
1
2
3
4
+ First item
+ Second item
+ Third item
+ Fourth item

效果如下:

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

效果如下:

  • First item
  • Second item
  • Third item
    • Indented item
    • Indented 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.

    I need to add another paragraph 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:

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

列表

您可以将无序列表嵌套在有序列表中,反之亦然。

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

链接语法

链接文本放在中括号内,链接地址放在后面的括号中,链接title可选。

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

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

1
这是一个链接 [Markdown语法](https://markdown.com.cn)。

效果如下:

这是一个链接Markdown语法

给链接增加 Title

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

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

效果如下:

这是一个链接 Markdown语法

网址和Email地址

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

1
2
<https://markdown.com.cn>
<https://www.zhoumx.net>

https://markdown.com.cn

https://www.zhoumx.net

带格式化的链接

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

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.

图片语法

要添加图像,请使用感叹号 (!), 然后在方括号增加替代文本,图片链接放在圆括号里,括号里的链接后可以增加一个可选的图片标题文本。

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

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

1
![这是图片](/assets/img/philly-magic-garden.jpg "Magic Gardens")

效果如下:

这是图片

链接图片

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

1
[![沙漠中的岩石图片](/assets/img/shiprock.jpg "Shiprock")](https://markdown.com.cn)

效果如下:

沙漠中的岩石图片

表格语法

要添加表,请使用三个或多个连字符(---)创建每列的标题,并使用管道(|)分隔每列。您可以选择在表的任一端添加管道。

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 |
Syntax Description
Header Title
Paragraph Text

Tip: 使用连字符和管道创建表可能很麻烦。为了加快该过程,请尝试使用Markdown Tables Generator。使用图形界面构建表,然后将生成的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

任务列表语法

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

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

效果如下:

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

内嵌 HTML 标签

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

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

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

效果如下:

This word is bold. This word is italic.

注意

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

对于 HTML 的块级元素 <div><table><pre><p>,请在其前后使用空行(blank lines)与其它内容进行分隔。尽量不要使用制表符(tabs)或空格(spaces)对 HTML 标签做缩进,否则将影响格式。

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


Markdown入门手册
https://www.zhoumx.net/Markdown入门手册.html
作者
阿星
发布于
2021年2月9日
许可协议