掌握 Markdown 語法的要旨
這篇文章對 Markdown 進(jìn)行了簡單的概述娇斑。語法篇 對 Markdown 的每一個功能進(jìn)行了完整策添、詳細(xì)的說明毫缆,通過瀏覽一些例子就可以很輕松的掌握 Markdown 語法。這篇文章中的實例采用了 前/后 風(fēng)格來展示實例中的語法和其對應(yīng)生成的 HTML苦丁。
你可以輕松地在線體驗 Markdown; Dingus 是一個可以在線編輯 Markdown 文檔并轉(zhuǎn)換成 XHTML 格式的一個 Web 應(yīng)用旺拉。
段落、標(biāo)題账阻、引用
一個段落是簡單的一行或多行的連續(xù)文字,段落之間通過一個或多個空行分隔姻僧。 (任何看起來像空行的都是空行 —— 只包含空格或者制表符行也被視為空行。) 正常的段落不因用空格或者制表符縮進(jìn)撇贺。
Markdown 提供了兩種風(fēng)格的標(biāo)題: Setext 和 atx冰抢。
Setext 風(fēng)格的標(biāo)題的分別使用等號 (=
) 和連字符 (-
) 用作 "下劃線" 來產(chǎn)生 <h1>
和 <h2>
松嘶。
而使用 atx 風(fēng)格的標(biāo)題挎扰,你需要在行首輸入 1-6 個井號 (#
) —— 井號的個數(shù)等價于 HTML 標(biāo)題的級數(shù)。
引用使用 email 風(fēng)格的 '>' 尖括號來表示遵倦。
Markdown:
一級標(biāo)題
=======
二級標(biāo)題
-------
Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.
The quick brown fox jumped over the lazy
dog's back.
### 三級標(biāo)題
> 這是一個引用。
>
> 這是這個引用中的第二段梧躺。
>
> ## 這是這個引用中的二級標(biāo)題
Output:
<h1>一級標(biāo)題</h1>
<h2>二級標(biāo)題</h2>
<p>Now is the time for all good men to come to
the aid of their country. This is just a
regular paragraph.</p>
<p>The quick brown fox jumped over the lazy
dog's back.</p>
<h3>三級標(biāo)題</h3>
<blockquote>
<p>這是一個引用。</p>
<p>這是這個引用中的第二段巩踏。</p>
<h2>這是這個引用中的二級標(biāo)題</h2>
</blockquote>
重點短語
Markdown 使用星號和下劃線來突出重點续搀。
Markdown:
Some of these words *are emphasized*.
Some of these words _are emphasized also_.
Use two asterisks for **strong emphasis**.
Or, if you prefer, __use two underscores instead__.
Output:
<p>Some of these words <em>are emphasized</em>.
Some of these words <em>are emphasized also</em>.</p>
<p>Use two asterisks for <strong>strong emphasis</strong>.
Or, if you prefer, <strong>use two underscores instead</strong>.</p>
列表
無序列表使用星號塞琼、加號和連字符 (*
,
+
, -
) 作為列表的標(biāo)記目代。三中符號可互換嗤练; 像這樣:
* Candy.
* Gum.
* Booze.
這樣:
+ Candy.
+ Gum.
+ Booze.
以及這樣:
- Candy.
- Gum.
- Booze.
都產(chǎn)生相同的輸出:
<ul>
<li>Candy.</li>
<li>Gum.</li>
<li>Booze.</li>
</ul>
有序 (編號) 列表使用常規(guī)數(shù)字加上逗點符號作為列表的標(biāo)記:
1. Red
2. Green
3. Blue
Output:
<ol>
<li>Red</li>
<li>Green</li>
<li>Blue</li>
</ol>
如果你在列表元素之間插入空行的話在讶,每個列表元素都會包裹進(jìn) <p> 標(biāo)記。你可以用 4 個空格或者一個制表符縮進(jìn)段落來創(chuàng)建一個多段落的列表元素:
* A list item.
With multiple paragraphs.
* Another item in the list.
Output:
<ul>
<li><p>A list item.</p>
<p>With multiple paragraphs.</p></li>
<li><p>Another item in the list.</p></li>
</ul>
超鏈接
Markdown 支持兩種方式來創(chuàng)建超鏈接: 內(nèi)聯(lián) 和
引用构哺。兩種方式都使用方括號來標(biāo)記作為超鏈接的文字。
內(nèi)聯(lián)風(fēng)格的超鏈接使用緊跟在超鏈接文字后的花括號標(biāo)記鏈接目標(biāo)曙强。
比如:
This is an example link.
Output:
<p>This is an <a >
example link</a>.</p>
Optionally, you may include a title attribute in the parentheses:
This is an [example link](http://example.com/ "With a Title").
Output:
<p>This is an <a title="With a Title">
example link</a>.</p>
引用風(fēng)格的超鏈接可以通過名稱鏈接到文章的任意定義了錨點的位置:
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"
Output:
<p>I get 10 times more traffic from <a
title="Google">Google</a> than from <a
title="Yahoo Search">Yahoo</a> or <a
title="MSN Search">MSN</a>.</p>
title 屬性是可選的。鏈接名稱可以是字母溪食,數(shù)字和空格,不區(qū)分大小寫:
I start my morning with a cup of coffee and
[The New York Times][NY Times].
[ny times]: http://www.nytimes.com/
Output:
<p>I start my morning with a cup of coffee and
<a >The New York Times</a>.</p>
圖片
圖片的語法與超鏈接的語法很相似娜扇。
內(nèi)聯(lián)風(fēng)格 (標(biāo)題可選):
![替換文字](/path/to/img.jpg "Title")
引用風(fēng)格:
![替換文字][id]
[id]: /path/to/img.jpg "Title"
以上兩個實例都產(chǎn)生相同的輸:
<img src="/path/to/img.jpg" alt="替換文字" title="Title" />
代碼
在一個常規(guī)的段落中,你可以將文字包裹在反引號中來創(chuàng)建行內(nèi)代碼雀瓢。任何的 (&
) 符號和尖括號 (<
或者
>
) 會被自動的轉(zhuǎn)換成 HTML 的實體符號。這使得 Markdown 能更輕松的書寫 HTML 示例代碼:
I strongly recommend against using any `<blink>` tags.
I wish SmartyPants used named entities like `—`
instead of decimal-encoded entites like `—`.
Output:
<p>I strongly recommend against using any
<code><blink></code> tags.</p>
<p>I wish SmartyPants used named entities like
<code>—</code> instead of decimal-encoded
entites like <code>—</code>.</p>
要指定一個預(yù)覽樣式的代碼區(qū)塊刃麸,用 4 個空格或 1 個制表符縮進(jìn)區(qū)塊內(nèi)的每一行。 與行內(nèi)代碼一樣泊业,&
,<
脱吱,和 >
符號會自動轉(zhuǎn)義。
Markdown:
If you want your page to validate under XHTML 1.0 Strict,
you've got to put paragraph tags in your blockquotes:
<blockquote>
<p>For example.</p>
</blockquote>
Output:
<p>If you want your page to validate under XHTML 1.0 Strict,
you've got to put paragraph tags in your blockquotes:</p>
<pre><code><blockquote>
<p>For example.</p>
</blockquote>
</code></pre>
本文翻譯自 daringfireball,轉(zhuǎn)載請注明出處垦垂。