Markdown 教程
一个Markdown示例展示了如何编写Markdown文件。本文整合了核心语法和扩展(GMF)。
块元素
段落和换行
段落
HTML标签:<p>
一个或多个空行。(空行是指仅包含空格或制表符的行。)
代码:
This will beinline.
This is second paragraph.预览:
This will be inline.
This is second paragraph.
换行
HTML标签:<br />
在行尾使用两个或更多空格。
代码:
This will be notinline.预览:
This will be not
inline.
标题
Markdown支持两种标题样式:Setext和atx。
Setext
HTML标签:<h1>, <h2>
使用**等号(=)**作为<h1>,**破折号(-)**作为<h2>,数量不限。
代码:
This is an H1=============This is an H2-------------预览:
This is an H1
This is an H2
atx
HTML标签:<h1>, <h2>, <h3>, <h4>, <h5>, <h6>
在行首使用1-6个井号字符(#),对应<h1>到<h6>。
代码:
# This is an H1## This is an H2###### This is an H6预览:
This is an H1
This is an H2
This is an H6
可选地,你可以”关闭”atx样式的标题。关闭的井号不需要与打开标题的井号数量匹配。
代码:
# This is an H1 ### This is an H2 ##### This is an H3 ######预览:
This is an H1
This is an H2
This is an H3
块引用
HTML标签:<blockquote>
Markdown使用电子邮件风格的**>**字符进行块引用。如果你对文本进行硬换行并在每行前加>,效果最佳。
代码:
> This is a blockquote 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. Suspendisse> id sem consectetuer libero luctus adipiscing.预览:
This is a blockquote 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. Suspendisse id sem consectetuer libero luctus adipiscing.
Markdown允许你偷懒,只在硬换行段落的第一行前加>。
代码:
> This is a blockquote 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. Suspendisseid sem consectetuer libero luctus adipiscing.预览:
This is a blockquote 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. Suspendisse id sem consectetuer libero luctus adipiscing.
块引用可以嵌套(即在块引用中再嵌套块引用),通过添加额外的>层级实现。
代码:
> This is the first level of quoting.>> > This is nested blockquote.>> Back to the first level.预览:
This is the first level of quoting.
This is nested blockquote.
Back to the first level.
块引用可以包含其他Markdown元素,包括标题、列表和代码块。
代码:
> ## This is a header.>> 1. This is the first list item.> 2. This is the second list item.>> Here's some example code:>> return shell_exec("echo $input | $markdown_script");预览:
This is a header.
- This is the first list item.
- This is the second list item.
Here’s some example code:
return shell_exec("echo $input | $markdown_script");
列表
Markdown支持有序(编号)列表和无序(项目符号)列表。
无序列表
HTML标签:<ul>
无序列表使用星号(*)、加号(+)和连字符(-)。
代码:
* Red* Green* Blue预览:
- Red
- Green
- Blue
等同于:
代码:
+ Red+ Green+ Blue和:
代码:
- Red- Green- Blue有序列表
HTML标签:<ol>
有序列表使用数字后跟句点:
代码:
1. Bird2. McHale3. Parish预览:
- Bird
- McHale
- Parish
有时可能会意外触发有序列表,比如这样写:
代码:
1986. What a great season.预览:
- What a great season.
你可以使用**反斜杠转义(\)**句点:
代码:
1986\. What a great season.预览:
1986. What a great season.
缩进
块引用
要在列表项中包含块引用,块引用的>分隔符需要缩进:
代码:
* A list item with a blockquote:
> This is a blockquote > inside a list item.预览:
-
A list item with a blockquote:
This is a blockquote inside a list item.
代码块
要在列表项中包含代码块,代码块需要缩进两次——8个空格或两个制表符:
代码:
* A list item with a code block:
<code goes here>预览:
-
A list item with a code block:
<code goes here>
嵌套列表
代码:
* A * A1 * A2* B* C预览:
- A
- A1
- A2
- B
- C
代码块
HTML标签:<pre>
将代码块的每一行缩进至少4个空格或1个制表符。
代码:
This is a normal paragraph:
This is a code block.预览:
This is a normal paragraph:
This is a code block.代码块会持续到遇到没有缩进的行(或文章结束)为止。
在代码块中,&符号(&)和**尖括号(<和>)**会自动转换为HTML实体。
代码:
<div class="footer"> © 2004 Foo Corporation </div>预览:
<div class="footer"> © 2004 Foo Corporation</div>以下部分“围栏代码块”和“语法高亮”是扩展功能,你可以使用其他方式编写代码块。
围栏代码块
只需用```包裹你的代码(如下所示),就不需要缩进四个空格。
代码:
Here's an example:
```function test() { console.log("notice the blank line before this function?");}```预览:
Here’s an example:
function test() { console.log("notice the blank line before this function?");}语法高亮
在围栏代码块中,添加可选的语言标识符,我们会进行语法高亮(支持的语言)。
代码:
```rubyrequire 'redcarpet'markdown = Redcarpet.new("Hello World!")puts markdown.to_html```预览:
require 'redcarpet'markdown = Redcarpet.new("Hello World!")puts markdown.to_html水平线
HTML标签:<hr />
在一行中放置三个或更多连字符(-)、星号(*)或下划线(_)。你可以在连字符或星号之间使用空格。
代码:
* * *********- - ----------------------------------------___预览:
表格
HTML标签:<table>
这是一个扩展。
使用**竖线(|)**分隔列,**破折号(-)**分隔表头,**冒号(:)**用于对齐。
外层的竖线(|)和对齐方式是可选的。每个单元格至少需要3个分隔符来分隔表头。
代码:
| Left | Center | Right ||:-----|:------:|------:||aaa |bbb |ccc ||ddd |eee |fff |
A | B---|---123|456
A |B--|--12|45预览:
| Left | Center | Right |
|---|---|---|
| aaa | bbb | ccc |
| ddd | eee | fff |
| A | B |
|---|---|
| 123 | 456 |
| A | B |
|---|---|
| 12 | 45 |
行内元素
链接
HTML标签:<a>
Markdown支持两种链接样式:行内链接和引用链接。
行内链接
行内链接格式如下:[链接文本](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.预览:
See my About page for details.
引用链接
你可以预定义链接引用。格式如下:[id]: URL "标题"
标题也是可选的。引用链接时,格式如下:[链接文本][id]
代码:
[id]: http://example.com/ "Optional Title Here"This is [an example][id] reference-style link.预览:
This is an example reference-style link.
即:
- 包含链接标识符的方括号(不区分大小写,可以使用最多三个空格从左边缘缩进);
- 后跟冒号;
- 后跟一个或多个空格(或制表符);
- 后跟链接的URL;
- 链接URL可以选择用尖括号包围。
- 可以选择后跟链接的标题属性,用双引号或单引号括起来,或用括号括起来。
以下三个链接定义是等效的:
代码:
[foo]: http://example.com/ "Optional Title Here"[foo]: http://example.com/ 'Optional Title Here'[foo]: http://example.com/ (Optional Title Here)[foo]: <http://example.com/> "Optional Title Here"使用空方括号,链接文本本身用作名称。
代码:
[Google]: http://google.com/[Google][]预览:
强调
HTML标签:<em>, <strong>
Markdown将**星号(*)和下划线(_)**视为强调指示符。单个分隔符将显示为<em>;双分隔符将显示为<strong>。
代码:
*single asterisks*
_single underscores_
**double asterisks**
__double underscores__预览:
single asterisks
single underscores
double asterisks
double underscores
但是如果你用空格包围*或_,它将被视为字面量的星号或下划线。
你可以用反斜杠转义它:
代码:
\*this text is surrounded by literal asterisks\*预览:
*this text is surrounded by literal asterisks*
代码
HTML标签:<code>
用**反引号(`)**包裹。
代码:
Use the `printf()` function.预览:
Use the printf() function.
要在代码跨度中包含字面量的反引号字符,可以使用多个反引号作为开头和结尾的分隔符:
代码:
``There is a literal backtick (`) here.``预览:
There is a literal backtick (`) here.
包围代码跨度的反引号分隔符可以包含空格——一个在开头之后,一个在结尾之前。这允许你在代码跨度的开头或结尾放置字面量的反引号字符:
代码:
A single backtick in a code span: `` ` ``
A backtick-delimited string in a code span: `` `foo` ``预览:
A single backtick in a code span: `
A backtick-delimited string in a code span: `foo`
图片
HTML标签:<img />
Markdown使用的图片语法类似于链接语法,允许两种样式:行内图片和引用图片。
行内图片
行内图片语法如下:
标题是可选的。
代码:

预览:


即:
- 一个感叹号:!;
- 后跟一对方括号,包含图片的alt属性文本;
- 后跟一对圆括号,包含图片的URL或路径,以及可选的用双引号或单引号括起来的标题属性。
引用图片
引用样式图片语法如下:![替代文本][id]
代码:
[img id]: https://s2.loli.net/2024/08/20/5fszgXeOxmL3Wdv.webp "Optional title attribute"![Alt text][img id]预览:

删除线
HTML标签:<del>
这是一个扩展。
GFM添加了删除线文本的语法。
代码:
~~Mistaken text.~~预览:
Mistaken text.
其他功能
自动链接
Markdown支持一种快捷方式,用于创建URL和电子邮件地址的”自动”链接:只需用尖括号包围URL或电子邮件地址。
代码:
<http://example.com/>
<address@example.com>预览:
GFM会自动链接标准URL。
代码:
https://github.com/emn178/markdown预览:
https://github.com/emn178/markdown
反斜杠转义
Markdown允许你使用反斜杠转义来生成字面量字符,否则这些字符在Markdown的格式化语法中具有特殊含义。
代码:
\*literal asterisks\*预览:
*literal asterisks*
Markdown为以下字符提供反斜杠转义:
代码:
\ 反斜杠` 反引号* 星号_ 下划线{} 花括号[] 方括号() 圆括号# 井号+ 加号- 减号(连字符). 点! 感叹号行内HTML
对于Markdown语法未涵盖的任何标记,你可以直接使用HTML本身。无需前缀或分隔符来指示你从Markdown切换到HTML;只需使用标签即可。
代码:
This is a regular paragraph.
<table> <tr> <td>Foo</td> </tr></table>
This is another regular paragraph.预览:
This is a regular paragraph.
| Foo |
This is another regular paragraph.
注意,Markdown格式化语法在块级HTML标签内不会被处理。
与块级HTML标签不同,Markdown语法在行内级标签内会被处理。
代码:
<span>**Work**</span>
<div> **No Work**</div>预览:
Work
部分信息可能已经过时





