Markdown基本语法
本文为antwork原创,转载请注明作者及出处!
翻译原文网站:http://daringfireball.net/projects/markdown/,根据原文分为两篇来开展,有兴趣且英文好的童鞋建议看原版 :)
本文原文地址:http://daringfireball.net/projects/markdown/basics
学习Markdonw语法主旨
本页面提供一个使用Markdown的简单使用方法,语法页面提供更加完整详细的文档和语法特征,Markdown很容易学,你可以查阅一些实例就能学会.本文的实例采用样式前后对比的形式,展示实例的语法和输出的HTML,(译者再加入了实际的显示效果)
学习markdown最简单有效的方法就是实践, Dingus可以让你在线输入Markdown格式的纯文本,然后转化为XHTML.
注释:原文档本身就是用Markdown;查看原网页源代码请单击这里
##段落,标题,区块
段落是由一到多行连续的文本组成,使用一到多个空行进行分隔.(Markdown的空行看起来像一个空行: 一行只包含空白和tabs的话就被当做一个空行).普通的段落不应该使用空白或tab来进行缩进
Markdown 提供两种样式的标题:Setext 和 atx
采用Setext样式的的
和分别通过在文字底部添加等号(=)和连字号(-)来实现
atx样式的标题通过在行首添加1-6个井字号(#)来标记标题等级(h1~h6)
atx样式的标题通过在行首添加1-6个井字号(#)来标记标题等级(h1~h6)
Blockquotes使用邮件格式的’>’大于号来标明
- ####MarkDown代码:
A First Level Header
====================
A Second Level Header
---------------------
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.
### Header 3
> This is a blockquote.
>
> This is the second paragraph in the blockquote.
>
> ## This is an H2 in a blockquote
- ####输出HTML代码:
<h1>A First Level Header</h1>
<h2>A Second Level Header</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>Header 3</h3>
<blockquote>
<p>This is a blockquote.</p>
<p>This is the second paragraph in the blockquote.</p>
<h2>This is an H2 in a blockquote</h2>
</blockquote>
- ####实际效果:
A First Level Header
A Second Level Header
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.
Header 3
This is a blockquote.
This is the second paragraph in the blockquote.
This is an H2 in a 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__.
####输出为HTML:
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.
- ####显示效果:
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.
##列表
无序列表采用星号,减号和加号来标记
- ####Markdown源码
* Candy.
* Gum.
* Booze.
- ####输出HTML
<ul>
<li>Candy.</li>
<li>Gum.</li>
<li>Booze.</li>
</ul>
- ####实际效果
- Candy.
- Gum.
- Booze.
有序列表采用常规数字加句号
- ####Markdown有序列表源码
1. Red
2. Green
3. Blue
- ####输出为HTML源码
<ol>
<li>Red </li>
<li>Green</li>
<li>Blue</li>
</ol>
- ####实际效果
- Red
- Green
- Blue
如果你在行间添加了空行,你会得到段落标识
来标记列表项,你可以使用4个空格或者1个tab来创造多段落列表项
- ####Markdown源码:
* A list item.
With multiple paragraphs.
* Another item in the list.
- ####输出:
<ul>
<li><p>A list item.</p>
<p>With multiple paragraphs.</p></li>
<li><p>Another item in the list.</p></li>
</ul>
- ####显示效果
A list item.
With multiple paragraphs.
Another item in the list.
##链接
Markdown支持两种形式的链接:内联和引用,两种形式都采用方括号来划定链接文字界限.
内联链接使用圆括号将链接紧跟在链接文字后,例如:
这是一个[链接](http://example.com/).
输出为HTML:
<p>这是一个<a href="http://example.com/">链接</a>.</p>
显示效果:
这是一个链接.
可选择的,你可以在圆括号内包含一个title属性:
This is an [example link](http://example.com/ "With a Title").
输出为HTML:
<p>This is an <a href="http://example.com/" title="With a Title">
example link.
显示效果:
his is an example link.
关联样式链接允许你通过名字去关联链接,允许你在文档的其他地方定义:
I get 10 times more traffic from [Google][1] than from
[1]: http://google.com/ "Google"
[2]: http://search.yahoo.com/ "Yahoo Search"
[3]: http://search.msn.com/ "MSN Search"
转化为HTML代码:
<p>I get 10 times more traffic from <a href="http://google.com/"
title=”Google”>Google than from Yahoo or MSN.
实际显示效果:
I get 10 times more traffic from Google than from
Yahoo or MSN.
title属性是可选的,超链接名字可以包含字母,数字和空格,但是大小写不敏感,例如以下源码:
I start my morning with a cup of coffee and
[The New York Times][NY Times].
[ny times]: http://www.nytimes.com/
输出为HTML代码:
<p>I start my morning with a cup of coffee and
实际显示效果:
I start my morning with a cup of coffee and
The New York Times.
##图片
图片语法非常像链接语法.
内联样式如下(标题可选):

引用样式:
![alt text][id]
[id]: /path/to/img.jpg "Title"
输出HTML为:
<img src="/path/to/img.jpg" alt="alt text" title="Title" />
##代码
在常规段落中,你可以用反引号将文字括起来,任何&号和尖括号(<>)将会自动被翻译为HTML实体,这样就方便了使用Markdown来创建HTML的示例:
I strongly recommend against using any `<blink>` tags.
I wish SmartyPants used named entities like `—`
instead of decimal-encoded entites like `—`.
输出为HTML:
<p>I strongly recommend against using any
<code><blink></code> tags.</p>
<p>I wish SmartyPants used named entities like
<code>&mdash;</code> instead of decimal-encoded
entites like <code>&#8212;</code>.</p>
实际显示效果:
I strongly recommend against using any <blink>
tags.
I wish SmartyPants used named entities like —
instead of decimal-encoded entites like —
.
为了指定某一块预定格式(pre-formatted)代码,你可以在每一行代码前添加4个空格或者一个tab.就像使用代码区域,&,<,和>字符一样,会被自动转义.
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>
得到HTML代码如下:
<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>
(完)