天天看點

MarkDowm——文法篇前言标題區塊引用清單代碼區塊代碼區塊HTML 實體分隔線連結強調代碼圖檔表格公式其它

前言

文檔位址: http://www.markdown.cn/

為了自己更熟悉MarkDown的文法,做個練習吧,以後博文打算用MarkDown直接寫了。

标題

Markdown 支援兩種标題的文法,類 Setext 和類 atx 形式。

類 Setext

類 Setext 形式是用底線的形式,利用 = (最高階标題)和 - (第二階标題),例如:

H1
=============

H2
-------------
           
任何數量的 = 和 - 都可以有效果。

類 Atx

類 Atx 形式則是在行首插入 1 到 6 個 # ,對應到标題 1 到 6 階,例如:

# 這是 H1 

## 這是 H2 

### 這是 H3 

#### 這是 H4 

##### 這是 H5 

###### 這是 H6
           

你可以選擇性地「閉合」類 atx 樣式的标題,這純粹隻是美觀用的,若是覺得這樣看起來比較舒适,你就可以在行尾加上 #,而行尾的 # 數量也不用和開頭一樣(行首的井字元數量決定标題的階數):

# 這是 H1 #

## 這是 H2 ##

### 這是 H3 ### 

#### 這是 H4 ####

##### 這是 H5 ##### 

###### 這是 H6######
           

區塊引用

使用 > 
           

這是一個測試的内容哦

這是一個測試的内容哦

這是一個測試的内容哦

> 這是一個測試的内容哦
這是一個測試的内容哦
這是一個測試的内容哦
           

嵌套使用

嵌套标題

  1. 這是第一行清單項。
  2. 這是第二行清單項。
給出例子代碼:
return shell_exec("echo $input | $markdown_script");
           
區塊嵌套
> ## 這是一個标題。
> 
> 1.   這是第一行清單項。
> 2.   這是第二行清單項。
> 
> 給出例子代碼:
> 
>     return shell_exec("echo $input | $markdown_script");
> >區塊嵌套
           

清單

Markdown 支援有序清單和無序清單。

無序清單

無序清單使用

星号

加号

或是

減号

作為清單标記:

  • Red
  • Green
  • Blue

    等同于:

  • Red
  • Green
  • Blue

    也等同于:

  • Red
  • Green
  • Blue
*   Red
*   Green
*   Blue
等同于:

+   Red
+   Green
+   Blue
也等同于:

-   Red
-   Green
-   Blue
           

有序清單

有序清單則使用數字接着一個英文句點:

  1. Bird
  2. McHale
  3. Parish
1.  Bird
2.  McHale
3.  Parish
           
很重要的一點是,你在清單标記上使用的數字并不會影響輸出的 HTML 結果,上面的清單所産生的 HTML 标記為:
<ol>
<li>Bird</li>
<li>McHale</li>
<li>Parish</li>
</ol>
           

如果你的清單标記寫成:

  1. Bird
  2. McHale
  3. Parish
1.  Bird
1.  McHale
1.  Parish
           

或甚至是:

  1. Bird
  2. McHale
  3. Parish
3. Bird
1. McHale
8. Parish
           

你都會得到完全相同的 HTML 輸出。重點在于,你可以讓 Markdown 檔案的清單數字和輸出的結果相同,或是你懶一點,你可以完全不用在意數字的正确性。

如果你使用懶惰的寫法,建議第一個項目最好還是從 1. 開始,因為 Markdown 未來可能會支援

有序清單的 start 屬性

清單項目标記通常是放在最左邊,但是其實也可以縮進,

最多 3 個空格

,項目标記後面則一定要接着

至少一個空格

制表符

要讓清單看起來更漂亮,你可以把内容用固定的縮進整理好:

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

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

但是如果你懶,那也行:

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

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

如果清單項目間用空行分開,在輸出 HTML 時 Markdown 就會将項目内容用

标簽包起來,舉例來說:

  • Bird
  • Magic
*   Bird
*   Magic
           

會被轉換為:

<ul>
<li>Bird</li>
<li>Magic</li>
</ul>
           

但是這個:

  • Bird
  • Magic
*   Bird

*   Magic
           

會被轉換為:

<ul>
<li><p>Bird</p></li>
<li><p>Magic</p></li>
</ul>
           

清單項目可以包含多個段落,每個項目下的段落都

必須縮進 4 個空格或是 1 個制表符

  1. This is a list item 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.

  2. Suspendisse id sem consectetuer libero luctus adipiscing.
1.  This is a list item 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.

2.  Suspendisse id sem consectetuer libero luctus adipiscing.
           

如果你每行都有縮進,看起來會看好很多,當然,再次地,如果你很懶惰,Markdown 也允許:

  • This is a list item with two paragraphs.

    This is the second paragraph in the list item. You're

    only required to indent the first line. Lorem ipsum dolor

    sit amet, consectetuer adipiscing elit.

  • Another item in the same list.
*   This is a list item with two paragraphs.

    This is the second paragraph in the list item. You're
only required to indent the first line. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit.

*   Another item in the same list.
           

如果要在清單項目内放進引用,那 > 就需要縮進:

  • 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 個空格或是 2 個制表符:

  • 一清單項包含一個清單區塊:
    <代碼寫在這>
               
*   一清單項包含一個清單區塊:

         <代碼寫在這>
           

當然,項目清單很可能會不小心産生,像是下面這樣的寫法

  1. What a great season.
1986. What a great season.
           

也就是在行首出現數字-句點-空白,要避免這樣的狀況,你可以在句點前面加上反斜杠。

1986\. What a great season.
           

代碼區塊

和程式相關的寫作或是标簽語言原始碼通常會有已經排版好的代碼區塊,通常這些區塊我們并不希望它以一般段落檔案的方式去排版,

而是照原來的樣子顯示,Markdown 會用<pre> 和 <code> 标簽來把代碼區塊包起來。

要在 Markdown 中建立代碼區塊很簡單,隻要簡單地縮進 4 個空格或是 1 個制表符或是```就可以,例如,下面的輸入:

這是一個普通段落:

這是一個代碼區塊。
           
這是一個代碼區塊。
           
這是一個普通段落:

    這是一個代碼區塊。

\```
這是一個代碼區塊。
\```
           

Markdown 會轉換成:

<p>這是一個普通段落:</p>

<pre><code>這是一個代碼區塊。
</code></pre>
           

這個每行一階的縮進(4 個空格或是 1 個制表符),都會被移除,例如:

Here is an example of AppleScript:

tell application "Foo"
    beep
end tell
           
Here is an example of AppleScript:

    tell application "Foo"
        beep
    end tell
           

會被轉換為:

<p>Here is an example of AppleScript:</p>

<pre><code>tell application "Foo"
    beep
end tell
</code></pre>
           

一個代碼區塊會一直持續到

沒有縮進的那一行

(或是檔案結尾)。

HTML 實體

在代碼區塊裡面, & 、 < 和 > 會自動轉成 HTML 實體,這樣的方式讓你非常容易使用 Markdown 插入範例用的 HTML 原始碼,隻需要複制貼上,再加上縮進就可以了,剩下的 Markdown 都會幫你處理,例如:

© 2004 Foo Corporation

<div class="footer">
  &copy; 2004 Foo Corporation
</div>
           

會被轉換為:

<pre><code>&lt;div class="footer"&gt;
    &amp;copy; 2004 Foo Corporation
&lt;/div&gt;
</code></pre>
           

代碼區塊中,

一般的 Markdown 文法不會被轉換

,像是星号便隻是星号,這表示你可以很容易地以 Markdown 文法撰寫 Markdown 文法相關的檔案。

分隔線

你可以在一行中用三個以上的星号、減号、底線來建立一個分隔線,行内不能有其他東西。你也可以在星号或是減号中間插入空格。下面每種寫法都可以建立分隔線:

* * *

***

*****

- - -

---------------------------------------
           

連結

Markdown 支援兩種形式的連結文法:

行内式

參考式

兩種形式。

不管是哪一種,連結文字都是用 [方括号] 來标記。

行内式

要建立一個行内式的連結,隻要在方塊括号後面緊接着圓括号并插入網址連結即可,如果你還想要加上連結的 title 文字,隻要在網址後面,用雙引号把 title 文字包起來即可,例如:

This is an example inline link.

This link has no title attribute.

This is [an example](http://example.com/ "Title") inline link.

[This link](http://example.net/) has no title attribute.
           

會産生:

<p>This is <a href="http://example.com/" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  title="Title">
an example</a> inline link.</p>

<p><a href="http://example.net/" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >This link</a> has no
title attribute.</p>
           
要連結到同樣主機的資源,你可以使用相對路徑:See my About page for details.
See my [About](/about/) page for details.
           

參考式

參考式的連結是在連結文字的括号後面再接上另一個方括号,而在第二個方括号裡面要填入用以辨識連結的标記:

This is an example reference-style link.

This is [an example][id] reference-style link.
           

你也可以選擇性地在兩個方括号中間加上一個空格:

This is [an example] id reference-style link.

This is [an example] [id] reference-style link.
           

接着,在檔案的任意處,你可以把這個标記的連結内容定義出來:

[id]: http://example.com/  "Optional Title Here"
           

連結内容定義的形式為:

  1. 方括号(前面可以選擇性地加上至多三個空格來縮進),裡面輸傳入連結接文字
  2. 接着一個冒号
  3. 接着一個以上的空格或制表符
  4. 接着連結的網址
  5. 選擇性地接着 title 内容,可以用單引号、雙引号或是括弧包着

下面這三種連結的定義都是相同:

[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"
[foo]: http://example.com/  'Optional Title Here'
[foo]: http://example.com/  (Optional Title Here)
           
請注意:有一個已知的問題是

Markdown.pl 1.0.1 會忽略單引号包起來的連結 title

連結網址也可以用方括号包起來:

[id]: "Optional Title Here"
           

你也可以把 title 屬性放到下一行,也可以加一些縮進,若網址太長的話,這樣會比較好看:

[id]: http://example.com/longish/path/to/resource/here
    "Optional Title Here"
           

網址定義隻有在産生連結的時候用到,并

不會直接出現在檔案

之中。

連結辨識标簽可以有字母、數字、空白和标點符号,但是并

不區分大小寫

,是以下面兩個連結是一樣的:

[link text][a]
[link text][A]
           

隐式連結标記功能

讓你可以省略指定連結标記,這種情形下,連結标記會視為等同于連結文字,要用隐式連結标記隻要在連結文字後面加上一個

空的方括号

,如果你要讓 "Google" 連結到 google.com,你可以簡化成:

Google

[Google][]
           

然後定義連結内容:

Google: http://google.com/

[Google]: http://google.com/
           

連結的定義可以放在檔案中的任何一個地方,我比較偏好直接放在連結出現段落的後面,你也可以把它放在檔案最後面,就像是注解一樣。

下面是一個參考式連結的範例:

I get 10 times more traffic from Google 1 than from

Yahoo 2 or MSN 3.

I get 10 times more traffic from [Google] [1] than from
[Yahoo] [2] or [MSN] [3].

  [1]: http://google.com/        "Google"
  [2]: http://search.yahoo.com/  "Yahoo Search"
  [3]: http://search.msn.com/    "MSN Search"
           

如果改成用連結名稱的方式寫:

I get 10 times more traffic from Google than from

Yahoo or MSN.

I get 10 times more traffic from [Google][] than from
[Yahoo][] or [MSN][].

  [google]: http://google.com/        "Google"
  [yahoo]:  http://search.yahoo.com/  "Yahoo Search"
  [msn]:    http://search.msn.com/    "MSN Search"
           

上面兩種寫法都會産生下面的 HTML。

<p>I get 10 times more traffic from <a href="http://google.com/" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" 
title="Google">Google</a> than from
<a href="http://search.yahoo.com/" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  title="Yahoo Search">Yahoo</a>
or <a href="http://search.msn.com/" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  title="MSN Search">MSN</a>.</p>
           

下面是用行内式寫的同樣一段内容的 Markdown 檔案,提供作為比較之用:

I get 10 times more traffic from [Google](http://google.com/ "Google")
than from [Yahoo](http://search.yahoo.com/ "Yahoo Search") or
[MSN](http://search.msn.com/ "MSN Search").
           

參考式的連結其實重點不在于它比較好寫,而是它比較好讀,比較一下上面的範例,使用參考式的文章本身隻有 81 個字元,但是用行内形式的卻會增加到 176 個字元,如果是用純 HTML 格式來寫,會有 234 個字元,在 HTML 格式中,标簽比文本還要多。

使用 Markdown 的參考式連結,可以讓檔案更像是浏覽器最後産生的結果,讓你可以把一些标記相關的中繼資料移到段落文字之外,你就可以增加連結而不讓文章的閱讀感覺被打斷。

強調

Markdown 使用星号(*)和底線(_)作為标記強調字詞的符号,被 * 或 _ 包圍的字詞會被轉成用 <em> 标簽包圍,用兩個 * 或 _ 包起來的話,則會被轉成<strong>,例如:

single asterisks

single underscores

double asterisks

double underscores

*single asterisks*

_single underscores_

**double asterisks**

__double underscores__
           

會轉成:

<em>single asterisks</em>

<em>single underscores</em>

<strong>double asterisks</strong>

<strong>double underscores</strong>
           

你可以随便用你喜歡的樣式,唯一的限制是,你用什麼符号開啟标簽,就要用什麼符号結束。

強調也可以直接插在文字中間:

unfriggingbelievable

un**frigging**believable
           
如果你的 * 和 _ 兩邊都有空白的話,它們就隻會被當成普通的符号。

如果要在文字前後直接插入普通的星号或底線,你可以用反斜線:

*this text is surrounded by literal asterisks*

\*this text is surrounded by literal asterisks\*
           

代碼

如果要标記一小段行内代碼,你可以用反引号把它包起來(`),例如:

Use the

printf()

function.

Use the `printf()` function.
           

會産生:

<p>Use the <code>printf()</code> function.</p>
           

如果要在代碼區段内插入反引号,你可以用多個反引号來開啟和結束代碼區段:

There is a literal backtick (`) here.

\``There is a literal backtick (`) here.\``
           

這段文法會産生:

<p><code>There is a literal backtick (`) here.</code></p>
           

代碼區段的起始和結束端都可以放入一個空白,起始端後面一個,結束端前面一個,這樣你就可以在區段的一開始就插入反引号:

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

會産生:

<p>A single backtick in a code span: <code>`</code></p>

<p>A backtick-delimited string in a code span: <code>`foo`</code></p>
           

在代碼區段内,& 和方括号都會被自動地轉成 HTML 實體,這使得插入 HTML 原始碼變得很容易,Markdown 會把下面這段:

Please don't use any

<blink>

tags.

Please don't use any `<blink>` tags.
           

轉為:

<p>Please don't use any <code><blink></code> tags.</p>
           

你也可以這樣寫:

&#8212;

is the decimal-encoded equivalent of

&mdash;

.

`&#8212;` is the decimal-encoded equivalent of `&mdash;`.
           

以産生:

<p><code>&amp;#8212;</code> is the decimal-encoded
equivalent of <code>&amp;mdash;</code>.</p>
           

圖檔

很明顯地,要在純文字應用中設計一個「自然」的文法來插入圖檔是有一定難度的。

Markdown 使用一種和連結很相似的文法來标記圖檔,同樣也允許兩種樣式:

行内式

參考式

行内式

行内式的圖檔文法看起來像是:

MarkDowm——文法篇前言标題區塊引用清單代碼區塊代碼區塊HTML 實體分隔線連結強調代碼圖檔表格公式其它
MarkDowm——文法篇前言标題區塊引用清單代碼區塊代碼區塊HTML 實體分隔線連結強調代碼圖檔表格公式其它
![Alt text](https://k.zol-img.com.cn/sjbbs/7692/a7691405_s.jpg)

![Alt text](https://k.zol-img.com.cn/sjbbs/7692/a7691405_s.jpg "Optional title")
           

詳細叙述如下:

一個驚歎号 !

接着一個方括号,裡面放上圖檔的替代文字

接着一個普通括号,裡面放上圖檔的網址,最後還可以用引号包住并加上 選擇性的 'title' 文字。

參考式

參考式的圖檔文法則長得像這樣:

MarkDowm——文法篇前言标題區塊引用清單代碼區塊代碼區塊HTML 實體分隔線連結強調代碼圖檔表格公式其它
![Alt text][id]
[id]: https://k.zol-img.com.cn/sjbbs/7692/a7691405_s.jpg  "Optional title attribute"
           
「id」是圖檔參考的名稱,圖檔參考的定義方式則和連結參考一樣

到目前為止, Markdown 還沒有辦法指定圖檔的寬高,如果你需要的話,你可以使用普通的 标簽。

表格

字段 字段 字段
1 4 123
2 4 123
3 4 123
| 字段 | 字段| 字段|
| --- |-----| -----|
| 1 | 4 | 123 |
| 2 | 4 | 123 |
| 3 | 4 | 123 |
           

公式

借助線上 LaTeX 公式編輯器

線上 LaTeX 公式編輯器

MarkDowm——文法篇前言标題區塊引用清單代碼區塊代碼區塊HTML 實體分隔線連結強調代碼圖檔表格公式其它

借助MathJax引擎(嘗試沒成功)

文檔 | https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference

其它

反斜杠

Markdown 可以利用反斜杠來插入一些在文法中有其它意義的符号,例如:如果你想要用星号加在文字旁邊的方式來做出強調效果(但不用 <em> 标簽),你可以在星号的前面加上反斜杠:

*literal asterisks*

\*literal asterisks\*
           

Markdown 支援以下這些符号前面加上反斜杠來幫助插入普通的符号:

\   反斜線
`   反引号
*   星号
_   底線
{}  花括号
[]  方括号
()  括弧
#   井字号
+   加号
-   減号
.   英文句點
!   驚歎号
           

自動連結

Markdown 支援以比較簡短的自動連結形式來處理網址和電子郵件信箱,隻要是用方括号包起來, Markdown 就會自動把它轉成連結。一般網址的連結文字就和連結位址一樣,例如:

http://example.com/

<http://example.com/>
           

Markdown 會轉為:

<a href="http://example.com/" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >http://example.com/</a>
           

郵址的自動連結也很類似,隻是 Markdown 會先做一個編碼轉換的過程,把文字字元轉成 16 進位碼的 HTML 實體,這樣的格式可以糊弄一些不好的郵址收集機器人,例如:

<[email protected]>
           

Markdown 會轉成:

<a href="mailto:addre
[email protected]
m">[email protected]
mple.com</a>
           

在浏覽器裡面,這段字串(其實是

<a href="mailto:[email protected]" target="_blank" rel="external nofollow" >[email protected]</a>

)會變成一個可以點選的「[email protected]」連結。

轉載于:https://www.cnblogs.com/wangyang0210/p/10954213.html

繼續閱讀