博文利器Markdown之基本语法篇

推荐使用电脑阅读💻

Markdown基本语法

总览🚀

几乎所有Markdown应用程序都支持John Gruber原始设计文档中列出的Markdown基本语法。但是,Markdown处理程序之间存在着细微的变化和差异,我们都会尽可能标记出来。

标题 (Headings)🌈

要创建标题,请在单词或短语前面添加井号(#)。井号的数量代表了标题的级别。例如,添加三个井号即创建一个三级标题(<h3>)(例如: ### My Header)。

MarkdownHTML渲染效果
# Heading level1<h1>Heading level1</h1>

Heading level1

## Heading level2<h2>Heading level2</h2>

Heading level2

### Heading level3<h3>Heading level3</h3>

Heading level3

#### Heading level4<h4>Heading level4</h4>
Heading level4
##### Heading level5<h5>Heading level5</h5>
Heading level5
###### Heading level6<h6>Heading level6</h6>
Heading level6

可选语法

还可以在文本下方添加任意数量的==来表示一级标题,或者–号来表示二级标题。

MarkdownHTML渲染效果
Heading level1
==========
<h1>Heading level1</h1>

Heading leve2

Heading level2
----------
<h2>Heading level2</h2>

Heading leve2

标题 (Heading) 用法的最佳实践

Markdown应用程序在处理标题和周围段落之间紧挨着的其情况时并不一致。为了兼容考虑,请使用一个或多个空白行对段落和标题进行分隔。

✅这样做❌不要这样做
This is a paragraph.

# Here’s the Heading

And this is another paragrph.
This is a paragraph.
# Here’s the Heading
And this is another paragrph.

段落 (Paragraphs)🌈

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

MarkdownHTML渲染效果
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.

段落 (Paragraphs) 用法的最佳实践

不要用空格(spaces)或制表符( tabs)缩进段落。

✅这样做❌不要这样做
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.

换行 (Line Breaks)🌈

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

MarkdownHTML渲染效果
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.

换行(Line Break)用法的最佳实践

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

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

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

✅这样做❌不要这样做
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 two spaces after.\
And the next line.

First line with the HTML tag after.
And the next line.

强调(Emphasis)🌈

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

粗体(Bold)

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

MarkdownHTML渲染效果
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**boldLove<strong>is</strong>boldLoveisbold

粗体(Bold)用法最佳实践

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

✅这样做❌不要这样做
Love**is**boldLove__is__bold

斜体(Italic)

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

MarkdownHTML渲染效果
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*meowA<em>cat</em>meowAcatmeow

斜体(Italic)用法的最佳实践

Markdown 的众多应用程序在如何处理单词中间的下划线上意见不一致。为了兼容起见,请用星号标注单词中间的斜体来表示着重。

✅这样做❌不要这样做
A*cat*meowA_cat_meow

粗体(Bold)和斜体(Italic)

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

MarkdownHTML渲染效果
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 really***very***important text.

粗体(Bold)和斜体(Italic)用法的最佳实践

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

✅这样做❌不要这样做
This is really***very***important text.This is really___very___important text.

块引用(Blockquotes)🌈

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

>Dorothy followed her through many of the beautiful rooms in her castle.

渲染效果如下所示:

Dorothy followed her through many of the beautiful rooms in her castle.

多个段落的块引用(Blockquotes)

块引用可以包含多个段落。为段落之间的空白行各添加一个 > 符号。

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

嵌套块引用(Nested Blockquotes)

块引用可以嵌套。在要嵌套的段落前添加一个 >> 符号。

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

带有其它元素的块引用(Blockquotes with Other Elements)

块引用可以包含其他 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.

列表(Lists)🌈

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

有序列表(Ordered Lists)

要创建有序列表,请在每个列表项前添加数字并紧跟一个英文句点。数字不必按数学顺序排列,但是列表应当以数字 1 起始。

MarkdownHTML渲染效果
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
1. Second item
1. Third item
1. 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</li>
<ol>
<li>Indented item</li>
<li>Indented item</li>
</ol>
<li>Fourth item</li>
</ol>
1. First item
2. Second item
3. Third item
 1. Indented item
 2. Indented item
4. Fourth item

无序列表(Unordered Lists)

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

MarkdownHTML渲染效果
- 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
+ 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</li>
<li>Fourth item</li>
</ul>
● First item
● Second item
● Third item
 ◌ Indented item
 ◌ Indented item
● Fourth item

在列表中添加列表项

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

段落(Paragraphs)

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

引用块(Blockquotes)

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

代码块(Code Blocks)

代码块(Code blocks) 通常采用四个空格或一个制表符缩进。当它们被放在列表中时,请将它们缩进八个空格或两个制表符。

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:

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

图像(Images)

1. Open the file containing the Linux mascot.
2. Marvel at its beauty.

  在这里插入图片描述

3. Close the file.

代码🌈

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

MarkdownHTML渲染效果
At the command prompt, type `nano`.At the command prompt, type <code>nano</code>.At the command prompt, type nano.

转义反引号

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

MarkdownHTML渲染效果
``Use `code` in your Markdown file.``<code>Use `code` in your Markdown file.</code>Use `code` in your Markdown file.

代码块(Code Blocks)

要创建代码块,请将代码块的每一行缩进至少四个空格或一个制表符。

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

渲染效果如下:

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

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

分隔线(Horizontal Rules)🌈

要创建分隔线,请在单独一行上使用三个或多个星号 (***)、破折号 (—) 或下划线 (___) ,并且不能包含其他内容。

***

---

_________________

以上三个分隔线的渲染效果看起来都一样:


分隔线(Horizontal Rule)用法最佳实践

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

✅这样做❌不要这样做
Try to put a blank line before…

- - -

…and after a horizontal rule.
Without blank lines, this would be a heading.
- - -
Don’t do this!

连接(Links)🌈

要创建一个链接,请将链接文本括在方括号中(例如,[Bai Du]),然后在其后面加上括号中的URL(例如(https://baidu.com)).

My favorite search engine is [Bai Du](https://baidu.com).

渲染效果如下:

My favorite search engine is Bai Du.

添加标题(Add Titles)

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

My favorite search engine is [Bai Du](https://baidu.com "The best search engine").

渲染效果如下:

My favorite search engine is Bai Du.

网址和电子邮件地址(URLs and Email Addresses)

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

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

渲染效果如下:

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

设置链接格式(Formatting Links)

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

I love supporting the **[Bai Du](https://baidu.com)**.
This is the *[Markdown Guide](https://www.markdownguide.org)*.
See the section on [`code`](#code).

渲染效果如下:

I love supporting the Bai Du.
This is the Markdown Guide.
See the section on code.

引用类型连接(Reference-style Links)

引用类型链接是一种特殊的链接,它使url在Markdown中更易于显示和读取。引用类型链接分为两部分:与文本保持内联的部分和存储在文件中其他位置以使文本易于阅读的部分。

设置链接的第一部分(Formatting the First Part of the Link)

引用类型链接的第一部分用两组括号格式化。第一组括号包围着应该显示为链接的文本。第二组括号显示一个标签,用于指向文档中其他位置存储的链接。

虽然没有规定,在第一组和第二组括号之间要包含空格,但你可以这么做。第二组括号中的标签不区分大小写,可以包括字母、数字、空格或标点符号。

这意味着以下示例格式中,引用类型链接的第一部分大致相同:

  • [hobbit-hole][1]
  • [hobbit-hole] [1]

设置链接的第二部分(Formatting the Second Part of the Link)

引用类型连接的格式化要符合以下规则:

  1. 括号中的标签,紧跟一个冒号和至少一个空格(例如,[标签]:)。

  2. 链接的URL,可以选择放在尖括号中。

  3. 链接的标题可选,可以用双引号、单引号或圆括号括起来。

这意味着以下示例格式中,引用类型链接的第二部分大致相同:

  • [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文档中的任何位置。有些人把它们放在它们出现的段落之后,而另一些人则把它们放在文档的末尾(例如尾注或脚注)。

链接(Links)用法最佳实践

Markdown应用程序处理URL中间的空格时并不一致。为了实现兼容性,请尝试用%20对URL中的任何空格进行编码。

✅这样做❌不要这样做
[link](https://www.example.com/my%20great%20page)[link](https://www.example.com/my great page)

图片(Images)🌈

要添加图像,请使用感叹号(!),后面使用方括号括中alt文本,圆括号中为图像资源的路径或URL。你可以选择在圆括号中的URL后面添加一个用双引号包裹起来的标题。

![Philadelphia's Magic Gardens. This place was so cool!](img/philly-magic-gardens.jpg "Philadelphia's Magic Gardens")

渲染效果如下:

Philadelphia's Magic Gardens. This place was so cool!

图片链接(Linking Images)

若要向图片中添加链接,请将图片的连接全部放在方括号中,然后将添加的链接放到后面的圆括号中。

[![An old rock in the desert](img/shiprock.jpg "百度一下,你就知道")](https://baidu.com)

渲染效果如下:
(注:CSDN不支持图片链接,故该实例无法正确演示。)

An old rock in the desert

转义字符(Escaping Characters)🌈

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

\* 如果没有开头的反斜杠字符的话,这一行将显示为无序列表。

渲染效果如下:

* 如果没有开头的反斜杠字符的话,这一行将显示为无序列表。

可做转义的(英文)字符

以下列出的字符都可以通过使用反斜杠字符从而达到转义目的。

字符名称
\反斜杠(backslash)
`反引号(backtick ))
*星号(asterisk)
_下划线(underscore)
{}花括号(curly braces)
[]方括号(brackets)
()圆括号或括号(parentheses)
#井号(pound sign)
+加号(plus sign)
-减号(minus sign) (也叫连字符 hyphen)
.句点(dot)
!感叹号(exclamation mark)
|管道符(pipe)

HTML 标签🌈

大多 Markdown 应用程序允许你在 Markdown 格式文本中添加 HTML 标签。如果你喜欢某些 HTML 标签胜于 Markdown 语法的话,这将何有帮助。例如,某些人发现通过 HTML 标签添加图像更加容易。当你需要更改元素的属性时(例如为文本指定颜色或更改图像的宽度),使用 HTML 标签更方便些。

如需使用 HTML,请将 HTML 标签添加到 Markdown 格式文本中即可。

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>,请在其前后使用空行(blank lines)与其它内容进行分隔。尽量不要使用制表符(tabs)或空格(spaces)对 HTML 标签做缩进,否则将影响格式。

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


Markdown基础篇(完).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值