Markdown基础语法(上)

Markdown是一种轻量级标记语言,创始人为约翰·格鲁伯。Markdown是一种轻量级标记语言,排版语法简洁,让人们更多地关注内容本身而非排版。它使用易读易写的纯文本格式编写文档,可与HTML混编,可导出 HTML、PDF 以及本身的 .md 格式的文件。因简洁、高效、易读、易写,Markdown被大量使用,如Github、Wikipedia等网站,如各大博客平台:WordPress、Drupal、等。


基本语法


标题

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

)(例如:### My Header)。

Markdown语法
HTML
预览效果
# Heading level 1

Heading level 1

# Heading level 1

## Heading level 2

Heading level 2

# Heading level 2

### Heading level 3

Heading level 3

# Heading level 3

#### Heading level 4

Heading level 4

# Heading level 4

##### Heading level 5
Heading level 5
# Heading level 5
###### Heading level 6
Heading level 6
# Heading level 6

可选语法

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

Markdown语法
HTML
预览效果
Heading level 1
===============

Heading level 1

# Heading level 1

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

Heading level 2

# Heading level 2

最佳实践

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

✅这样做 ❌不要这样做
# Here's a Heading #Here's a Heading

段落

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

Markdown语法
HTML
预览效果
I really like using Markdown.

I really like using Markdown.

Heading level 1

I really like using Markdown.
I think I'll use it to format all of my documents from now on.

I think I'll use it to format all of my documents from now on.

I think I'll use it to format all of my documents from now on.

最佳实践

不要用 空格 制表符 缩进段落。

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

换行语法

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

Markdown语法
HTML
预览效果
This is the first line.
And this is the second line.

This is the first line.

And this is the second line.

This is the first line.
And this is the second line.

最佳实践

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

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

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

✅这样做 ❌不要这样做
First line with two spaces after.
And the next line.

First line with the HTML tag after.
And the next line.
First line with a backslash after.\
And the next line.

First line with nothing after.
And the next line.

强调语法

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

粗体

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

Markdown语法
HTML
预览效果
I just love **bold text**. I just love bold text. I just love bold text.
I just love __bold text__. I just love bold text. I just love bold text.
Love**is**bold Loveisbold Loveisbold

最佳实践

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

✅这样做 ❌不要这样做
Love**is**bold Love__is__bold

斜体

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

Markdown语法
HTML
预览效果
Italicized text is the *cat's meow*. Italicized text is the cat's meow. Italicized text is the cat's meow.
Italicized text is the _cat's meow_. Italicized text is the cat's meow. Italicized text is the cat's meow.
A*cat*meow Acatmeow Acatmeow

最佳实践

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

✅这样做 ❌不要这样做
A*cat*meow A_cat_meow

粗体和斜体

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

Markdown语法
HTML
预览效果
This text is ***really important***.*. This text is really important. This text is really important.
This text is ___really important___. This text is really important. This text is really important.
This text is __*really important*__. This text is really important. This text is really important.
This text is **_really important_**. This text is really important. This text is really important.
This is really***very***important text. This is reallyveryimportant text. This is reallyveryimportant text.

最佳实践

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

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

引用语法

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

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


列表语法

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

有序列表

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

Markdown语法
HTML
预览效果
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. First item
2. Second item
3. Third item
4. Fourth item
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. First item
2. Second item
3. Third item
4. Fourth item
1. First item
8. Second item
3. Third item
5. Fourth item

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

  1. First item

  2. Second item

  3. Third item

    1. Indented item

    2. Indented item



  4. Fourth item

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

最佳实践

CommonMark 和其它几种轻量级标记语言可以让你使用 括号( ) ) 作为分隔符(例如 1) First item),但并非所有的 Markdown 应用程序都支持此种用法,因此,从兼容的角度来看,此用法不推荐。为了兼容起见,请只使用 英文句点 英文句点作为分隔符。

✅这样做 ❌不要这样做
1. First item
2. Second item
1) First item
2) Second item

无序列表

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

Markdown语法
HTML
预览效果
- First item
- Second item
- Third item
- Fourth item

  • First item

  • Second item

  • Third item

  • Fourth item

  • First item
  • Second item
  • Third item
  • Fourth item
  • * First item
    * Second item
    * Third item
    * Fourth item

    • First item

    • Second item

    • Third item

    • Fourth item

  • First item
  • Second item
  • Third item
  • Fourth item
  • + First item
    + Second item
    + Third item
    + Fourth item

    • First item

    • Second item

    • Third item

    • Fourth item

  • First item
  • Second item
  • Third item
  • Fourth item
  • - First item
    - Second item
    - Third item
            - Indented item
            - Indented item
    - Fourth item

    • First item

    • Second item

    • Third item

      • Indented item

      • Indented item



    • Fourth item

    • First item
    • Second item
    • Third item
      • Indented item
      • Indented item
    • Fourth item

    以数字开头的无序列表项

    如果你需要以数字开头并且紧跟一个英文句号(也就是 .)的无序列表项,则可以使使用 反斜线( \ ) 来 转义 这个英文句号。

    Markdown语法
    HTML
    预览效果
    - 1968\. A great year!
    - I think 1969 was second best.

    • 1968. A great year!

    • I think 1969 was second best.

  • 1968. A great year!
  • I think 1969 was second best.
  • 最佳实践

    Markdown 应用程序在如何处理同一列表中混用不同分隔符上并不一致。为了兼容起见,请不要在同一个列表中混用不同的分隔符,最好选定一种分隔符并一直用下去。

    ✅这样做 ❌不要这样做
    - First item
    - Second item
    - Third item
    - Fourth item
    + First item
    * Second item
    - Third item
    + Fourth 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.
    

    渲染效果如下:

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

    代码块

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

    1.  Open the file.
    2.  Find the following code block on line 21:
    
            
              
                Test
              
    
    3.  Update the title to match the name of your website.
    

    渲染效果如下:

    1. Open the file.

    2. Find the following code block on line 21:

      
        
          Test
        
      
    3. Update the title to match the name of your website.

    图片

    1.  Open the file containing the Linux mascot.
    2.  Marvel at its beauty.
    
        ![Tux, the Linux mascot](/assets/images/tux.png)
    
    3.  Close the file.
    

    渲染效果如下:

    1. Open the file containing the Linux mascot.

    2. Marvel at its beauty.

      ![image][img1]

    3. Close the file.

    列表

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

    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

    代码语法

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

    Markdown语法
    HTML
    预览效果
    At the command prompt, type `nano`. At the command prompt, type nano. At the command prompt, type nano.

    转义反引号

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

    Markdown语法
    HTML
    预览效果
    ``Use `code` in your Markdown file.`` Use `code` in your Markdown file. Use `code` in your Markdown file.

    代码块

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

        
          
          
        
    

    渲染效果如下:

    
      
      
      
    

    注意: 要创建不用缩进的代码块,请使用 围栏式代码块


    分隔线语法

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

    ***
    
    ---
    
    _________________
    

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


    最佳实践

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

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

    ---

    ...and after a horizontal rule.
    Without blank lines, this would be a heading.

    ---

    Don't do this!

    你可能感兴趣的:(Markdown基础语法(上))