Markdown基础语法(上)

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


基本语法


标题

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
# Heading level 1 <h1>Heading level 1</h1> <h1># Heading level 1</h1>
## Heading level 2 <h2>Heading level 2</h2> <h2># Heading level 2<h2>
### Heading level 3 <h3>Heading level 3</h3> <h3># Heading level 3<h3>
#### Heading level 4 <h4>Heading level 4</h4> <h4># Heading level 4<h4>
##### Heading level 5 <h5>Heading level 5</h5> <h5># Heading level 5<h5>
###### Heading level 6 <h6>Heading level 6</h6> <h6># Heading level 6<h6>

可选语法

还可以在文本下方添加任意数量的 <font size=5 color=red> == </font> 号来标识一级标题,或者 <font size=5 color=red> -- </font> 号来标识二级标题。

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
Heading level 1
===============
<h1>Heading level 1</h1> <h1># Heading level 1</h1>
Heading level 2
---------------
<h2>Heading level 2</h2> <h2># Heading level 2<h2>

最佳实践

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

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

段落

要创建段落,请使用 <font size=5 color=red>空白行</font> 将一行或多行文本进行分隔。

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
I really like using Markdown.
<p>I really like using Markdown.</p>
<h1>Heading level 1</h1> I really like using Markdown.
I think I'll use it to format all of my documents from now on. <p>I think I'll use it to format all of my documents from now on.</p> I think I'll use it to format all of my documents from now on.

最佳实践

不要用<font size=5 color=red> 空格 </font>或 <font size=5 color=red> 制表符 </font> 缩进段落。

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

换行语法

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
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.

最佳实践

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

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

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

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

强调语法

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

粗体

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
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**bold Love<strong>is</strong>bold Loveisbold

最佳实践

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

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

斜体

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
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*meow A<em>cat</em>meow Acatmeow

最佳实践

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

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

粗体和斜体

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
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 reallyveryimportant text.

最佳实践

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

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

引用语法

块引用可以包含多个段落。为段落之间的空白行各添加一个 <font size=5 color=red> > </font> 符号。

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

渲染效果如下所示:

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

多个段落的块引用

块引用可以包含多个段落。为段落之间的空白行各添加一个 <font size=5 color=red> > </font> 符号。

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

嵌套块引用

块引用可以嵌套。在要嵌套的段落前添加一个 <font size=5 color=red> >> </font> 符号。

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


列表语法

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

有序列表

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
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
<ol>
<li>Indented item</li>
<li>Indented item</li>
</ol>
</li>
<li>Fourth item</li>
</ol>
1. First item
2. Second item
3. Third item
        1. Indented item
        2. Indented item
4. Fourth item

最佳实践

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

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

无序列表

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
- 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>
<li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li>
* 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>
<li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li>
+ 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>
<li>First item</li> <li>Second item</li> <li>Third item</li> <li>Fourth item</li>
- First item
- Second item
- Third item
        - Indented item
        - Indented item
- Fourth item
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item
<ul>
<li>Indented item</li>
<li>Indented item</li>
</ul>
</li>
<li>Fourth item</li>
</ul>
<ul><li>First item</li><li>Second item</li><li>Third item<ul><li>Indented item</li><li>Indented item</li></ul></li><li>Fourth item</li></ul>

以数字开头的无序列表项

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
- 1968\. A great year!
- I think 1969 was second best.
<ul>
<li>1968. A great year!</li>
<li>I think 1969 was second best.</li>
</ul>
<li>1968. A great year!</li><li>I think 1969 was second best.</li>

最佳实践

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

✅这样做 ❌不要这样做
- First item
- Second item
- Third item
- Fourth item
+ First item
* Second item
- Third item
+ Fourth item

在列表中嵌套其他元素

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

段落

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

代码块

代码块通常采用<font size=5 color=red> 四个空格 </font>或<font size=5 color=red> 一个制表符 </font>缩进。当它们被放在列表中时,请将它们缩进<font size=5 color=red> 八个空格 </font>或<font size=5 color=red> 两个制表符 </font>。

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.

图片

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

代码语法

要将单词或短语表示为代码,请将其包裹在<font size=5 color=red> 反引号( ` ) </font> 中。

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
At the command prompt, type `nano`. At the command prompt, type <code>nano</code>. At the command prompt, type nano.

转义反引号

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

Markdown语法<div style="width: 250px"> HTML<div style="width: 250px"> 预览效果<div style="width: 300px">
``Use `code` in your Markdown file.`` <code>Use `code` in your Markdown file.</code> Use `code` in your Markdown file.

代码块

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

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

渲染效果如下:

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

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


分隔线语法

要创建分隔线,请在单独一行上使用<font size=5 color=red> 三个 </font>或<font size=5 color=red> 多个星号 (***) </font>、<font size=5 color=red> 破折号 (---) </font> 或<font size=5 color=red> 下划线 (___) </font> ,并且不能包含其他内容。

***

---

_________________

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


最佳实践

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

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

---

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

---

Don't do this!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容