代码
要将单词或短语表示为代码,请将其括在反引号 ( `
) 中。
Markdown | HTML | 渲染输出 |
---|---|---|
At the command prompt, type `nano`. | At the command prompt, type <code>nano</code>. | 在命令提示符下,键入nano 。 |
转义反引号
如果要用代码表示的单词或短语包含一个或多个反引号,则可以通过将单词或短语括在双反引号 ( ``
) 中来将其转义。
Markdown | HTML | 渲染输出 |
---|---|---|
``Use `code` in your Markdown file.`` | <code>Use `code` in your Markdown file.</code> | Use `code` in your Markdown file. |
代码块
要创建代码块,请将块的每一行缩进至少四个空格或一个制表符。
<span style="color:#212529"><span style="background-color:#ffffff"><span style="color:#212529"><code> <html>
<head>
</head>
</html>
</code></span></span></span>
渲染的输出如下所示:
<span style="color:#212529"><span style="background-color:#ffffff"><span style="color:#212529"><code><html>
<head>
</head>
</html>
</code></span></span></span>
水平线
要创建水平线,请在一行上使用三个或更多星号 ( ***
)、破折号 ( ---
) 或下划线 ( )。___
***
---
_________________
三者的渲染输出看起来相同:
水平线最佳实践
为了兼容性,在水平线之前和之后放置空行。
✅ 这样做 | ❌ 不要这样做 |
---|---|
Try to put a blank line before... | Without blank lines, this would be a heading. |
链接
要创建链接,请将链接文本括在括号中(例如[Duck Duck Go]
),然后在其后紧接着括号中的 URL(例如(https://duckduckgo.com)
)。
My favorite search engine is [Duck Duck Go](https://duckduckgo.com).
渲染的输出如下所示:
我最喜欢的搜索引擎是Duck Duck Go。
添加标题
您可以选择为链接添加标题。当用户将鼠标悬停在链接上时,标题将显示为工具提示。要添加标题,请在 URL 后用引号将其括起来。
My favorite search engine is [Duck Duck Go](https://duckduckgo.com "The best search engine for privacy").
URL 和电子邮件地址
要快速将 URL 或电子邮件地址转换为链接,请将其括在尖括号中。
<https://www.markdownguide.org>
<fake@example.com>
渲染的输出如下所示:
https://www.markdownguide.org
fake@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).
渲染的输出如下所示:
我喜欢支持EFF。
这是Markdown 指南。
请参阅 部分code。
参考样式链接
引用样式链接是一种特殊的链接,它使 URL 在 Markdown 中更易于显示和阅读。引用样式链接由两部分组成:与文本保持内联的部分以及存储在文件其他位置以保持文本易于阅读的部分。
格式化链接的第一部分
引用式链接的第一部分采用两组括号格式。第一组括号包围应显示为链接的文本。第二组括号显示一个标签,用于指向您存储在文档其他位置的链接。
虽然不是必需的,但您可以在第一组和第二组括号之间添加一个空格。第二组括号中的标签不区分大小写,可以包含字母、数字、空格或标点符号。
这意味着以下示例格式对于链接的第一部分大致等效:
[hobbit-hole][1]
[hobbit-hole] [1]
格式化链接的第二部分
引用样式链接的第二部分采用以下属性的格式:
- 标签位于括号中,后面紧跟冒号和至少一个空格(例如
[label]:
)。 - 链接的 URL,您可以选择将其括在尖括号中。
- 链接的可选标题,您可以将其括在双引号、单引号或括号中。
这意味着以下示例格式对于链接的第二部分来说都是大致等效的:
[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 中看起来像这样:
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:
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"
在上述两种情况下,渲染的输出将是相同的:
地上的一个洞里住着一个霍比特人。这个洞不是肮脏、潮湿、满是虫子尾巴、散发着黏液气味的洞,也不是干燥、光秃、满是沙子、没有东西可以坐或吃的洞:这是一个霍比特人的洞,这意味着舒适。
该链接的 HTML 内容为:
<a href="https://en.wikipedia.org/wiki/Hobbit#Lifestyle" title="Hobbit lifestyles">hobbit-hole</a>
链接最佳实践
Markdown 应用程序对如何处理 URL 中间的空格意见不一。为了兼容,请尝试使用 对任何空格进行 URL 编码%20
。或者,如果您的 Markdown 应用程序支持 HTML,则可以使用a
HTML 标签。
✅ 这样做 | ❌ 不要这样做 |
---|---|
[link](https://www.example.com/my%20great%20page) | [link](https://www.example.com/my great page) |
URL 中间的括号也可能有问题。为了兼容,请尝试使用 来(
对左括号 ( )进行 URL 编码,使用 来%28
对右括号 ( )
)进行 URL 编码%29
。或者,如果您的 Markdown 应用程序支持 HTML,则可以使用a
HTML 标签。
✅ 这样做 | ❌ 不要这样做 |
---|---|
[a novel](https://en.wikipedia.org/wiki/The_Milagro_Beanfield_War_%28novel%29) | [a novel](https://en.wikipedia.org/wiki/The_Milagro_Beanfield_War_(novel)) |