Markdown學習(六)

鏈接

Markdown 支持兩種形式的鏈接語法: 行內式和參考式兩種形式。

不管是哪一種,鏈接文字都是用 [方括號] 來標記。


要建立一個行內式的鏈接,只要在方塊括號後面緊接着圓括號並插入網址鏈接即可,如果你還想要加上鍊接的 title 文字,只要在網址後面,用雙引號把 title 文字包起來即可,例如:

Source:

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

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

Result:

This is an example inline link.

This link has no title attribute.

如果你是要鏈接到同樣主機的資源,你可以使用相對路徑:

Source:

See my [About](/about/) page for details.

Result:

See my About page for details.


參考式的鏈接是在鏈接文字的括號後面再接上另一個方括號,而在第二個方括號裏面要填入用以辨識鏈接的標記:

Source:

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

Result:

This is an example reference-style link.

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

Source:

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

Result:

This is an example reference-style link.

接着,在文件的任意處,你可以把這個標記的鏈接內容定義出來:

Source:

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

Result:

id defines here

鏈接內容定義的形式爲:

方括號(前面可以選擇性地加上至多三個空格來縮進),裏面輸入鏈接文字
接着一個冒號
接着一個以上的空格或製表符
接着鏈接的網址
選擇性地接着 title 內容,可以用單引號、雙引號或是括弧包着

下面這三種鏈接的定義都是相同:

Source:

[foo]: http://example.com/  "Optional Title Here"
[foo]: http://example.com/  'Optional Title Here'
[foo]: http://example.com/  (Optional Title Here)

Result:

foo: http://example.com/ ‘Optional Title Here’

foo defines here

鏈接網址也可以用尖括號包起來:

Source:

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

Result:

id defines here

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

Source:

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

Result:

id defines here

網址定義只有在產生鏈接的時候用到,並不會直接出現在文件之中。
鏈接辨別標籤可以有字母、數字、空白和標點符號,但是並不區分大小寫.


隱式鏈接標記功能讓你可以省略指定鏈接標記,這種情形下,鏈接標記會視爲等同於鏈接文字,要用隱式鏈接標記只要在鏈接文字後面加上一個空的方括號,如果你要讓 “Google” 鏈接到 google.com,你可以簡化成:

Source:

[Google][]

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

Result:

Google
鏈接的定義可以放在文件中的任何一個地方,我比較偏好直接放在鏈接出現段落的後面,你也可以把它放在文件最後面,就像是註解一樣。

下面是一個參考式鏈接的範例:

Source:

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"

如果改成用鏈接名稱的方式寫:

Source:

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"

下面是用行內式寫的同樣一段內容的 Markdown 文件,提供作爲比較之用:

Source:

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

Result:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章