Markdown 初学者指南
Markdown
![](/data/attachment/album/202204/24/140247d969nm9kkhxeknje.jpg)


在我的工作中，我经常要写代码、写与代码相配套的文档、创建网页、进行文本恢复项目。我在学校的时候还写过几篇正式的论文，也包括写课堂笔记，几乎每节课都写。


我几乎在我所有的写作中都使用 Markdown，它对我来说是一个节省时间的好工具。


在这篇文章中，我将分享我使用 Markdown 的体会。你将会了解以下内容：


* 什么是 Markdown ？
* 它是怎么工作的？
* Markdown 的基本语法和怎样使用它们。


### 什么是 Markdown ？


假如你是 Markdown 新手，它是一个为网络写手转换文本到 HTML 格式的工具。Markdown 文档遵循一种特定的语法，容易阅读和写作。它们是纯文本，所以能够通过电脑上的任何文本编辑器来创建。然后这些文档能够转换成网页 —— 而网页是用一个叫做 HTML 的语言标记创建的。


Markdown 只是一种不必（即使知道怎么做）写 HTML 代码就能够创建网页的方式。实际上，如果你不转换成 HTML 的话，Markdown 甚至是个对纯文本格式化的好方法。有人曾这样对我描述 Markdown ：



> 
> “ *它不是所见即所得，而是所见即所意* ”。
> 
> 
> 


然而，Markdown 不只是一个简单的格式化体系，它也是一个纯文本格式转化到 HTML 的一个软件工具。


这就是为什么语法很重要。如果你想在网页上起个标题，Markdown 将会基于你标题前面的字符来创建。Markdown 的一些语法示例如截图所示：


![Markdown to HTML conversion](/data/attachment/album/202204/24/140300s047qota4gtktiz0.png)


### 所以我怎样才能使纯文本文件转换成 HTML 格式呢?


John Gruber 的 Markdown 是一个运行在命令行上的 Perl 脚本。也就是说，它可以读取你创建的 Markdown 文本并用它来构建网页。


由于有 [许多著名的编辑器](https://itsfoss.com/best-markdown-editors-linux/) 可以为你做这个转换，我在这里尽量避免使用命令行。不仅如此，许多这样的编辑器会让你写下你的文本，并同时向你展示网页的样子（称为渲染）。


Markdown 编辑器总的来说设置了两帧：左边部分是你要写你文本的地方，右边部分是用 HTML 语言显示给你格式化后文本的样子：


![Most Markdown editors have two panes to write and preview the text](/data/attachment/album/202204/24/140300id898wdr0crydocc.png)


当你用它满意地完成你的写作，只需保存 Markdown 文件即可。就像这样，之后一旦你需要编辑和修改的时候，你就会用到它。文件保存后，你可以用编辑器将 Markdown 文档导出为 HTML 格式的文档。


编辑器用你的 Markdown 作为参考创建网页。你的 Markdown 文档在导出时不会修改，你仍然拥有源文档，同时创建一个独立的、新的 HTML 文档（网页），你可以把它放到一个网页服务器上。


**注意**：许多 Markdown 编辑器也可以将你的 Markdown 文档导出为其他格式，比如 `.doc`、`.docx` 和 `.pdf`。之后你可以了解这些高级设置和你可能需要的额外软件。


### Markdown 基础语法


为了让 Markdown 新用户快速了解，我将把这部分内容限制在我最常使用的语法上。我相信这些将是最有帮助的 - 你可以在现在就提高工作效率，同时了解更多关于 Markdown 以后能为你做什么。


#### 写标题


我经常使用 `#` 字符来表示标题。这里有六个等级：



```
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题

```

还有一种标题类型，是在文本下使用下划线。我很少使用这种类型的标题，我只限于使用两种：一种是使用 `=` 字符表示的双下划线，生成 `H1` 标题。一种是使用 `-` 字符表示的单下划线，生成 `H2` 标题：



```
一级标题
===============

二级标题
---------------

```

![](/data/attachment/album/202204/24/140301sv9ra8qdrxdwrvv9.png)


#### 段落


段落被空行隔开（确保段落之间有一个空行）。首行不要缩进。在 Markdown 里用 `Tab` 键或者空格键缩进有着不同的目的和作用。


段落是一个文本块，不应该用空格键和 `Tab` 键来缩进。它可以有一行或者多行。要结束这一段另起一段，点两下回车键；段落通过一个空行分开。


![](/data/attachment/album/202204/24/140302v6q1uqfaboabq6uu.png)


#### 换行


请记住，对于段落，必须用空行来分隔它们，这要通过按两次回车键来实现。Markdown 对此很严格。


Markdown 不支持 “<ruby> 强制插入换行符 <rt>  hard-wrapped </rt></ruby>” 或者 “<ruby> 固定行长度 <rt>  fixed-line-length </rt></ruby>” 段落。那就是说，单击回车键一次将不会强制文本换到新的一行。它可能在编辑窗口显示，但是 HTML 格式下不显示。


然而，有时你可能需要拆分段落或者换行。Markdown 确实有一种方法可以做到，但是它一开始看起来可能有一点奇怪：**换行是通过用两到多个空格键然后点一次回车键完成的。**


![](/data/attachment/album/202204/24/140302s1ap3vxqvvemp5vr.png)


这里有一个短诗的范例。每行以两个空格结束。最后一行，由于是这个小节的结束，没有额外的空格。因为它是这个诗句（段落）的结束，我双击回车键两次：



```
Baa, baa black sheep,  
Have you any wool?.  
Yes, sir. Yes, sir.  
Three bags full.

```

在一行的最后加两个空格来创建一个换行，可能需要时间去习惯。


![](/data/attachment/album/202204/24/140303hx1875xhoc3c9ko4.png)


#### 水平线


水平线非常适合将文本拆分为多个部分。


用三个或更多的破折号 `-` ，下划线 `_` ，或者星号 `*` 来做水平线，像这样：



```
---

***

___

```

你甚至可以在字符之间输入空格：



```
- - -

```

在文章和报告中我不经常使用水平线，但是在日记、系统日志和课堂笔记中对我很有用。


![](/data/attachment/album/202204/24/140304a8yjtzut9yyq989c.png)


#### 用加粗和斜体来强调文本


当你想让一个单词或者短语脱颖而出，引起注意，你可以让它加粗或者斜体显示。斜体和加粗文本可以在两种方式中任选其一。第一种是用星号 `*` 将其包括，第二种是用下划线 `_` 的方式。


使一个单词或者短语斜体显示，用一个下划线或者星号来括住文本。使一个单词或者短语加粗，使用两个下划线或者星号来括住文本：



```
用星号来显示 *斜体* 。

用下划线来显示 _斜体_ 。

用星号来显示 **加粗** 。

用下划线来显示 __加粗__ 。

```

记住两边使用相同的字符。单词或短语的一面是星号，另一面是下划线，将不会生效。相同字符必须在单词或者短语的两边。


![](/data/attachment/album/202204/24/140304ewopzcqgsms69fcd.png)


#### 块引用


块引用被用来直接引用。如果你正在写博客，你想重复本杰明•富兰克林说过的话，你可以使用块引用。


一个右尖括号被用来指定块引用：



```
> 这是块引用。

>> 如果你想进一步再次块引用，使用两个右尖括号。

```

![](/data/attachment/album/202204/24/140305ubttzpccggtugu5b.png)


#### 在 Markdown 中添加超链接


超链接非常酷。在基础版的 Markdown 语言有三种方式创建超链接，在这里我只讲两种：普通连接和自动连接。


第三种链接被称为参考链接，在 Markdown 语言的基础版和高级版都支持。我想让你快速入门。你可以在准备好的时候找找参考链接的资料。


普通链接可以让你链接到各种各样的网站。网站名称或者你要使用的短语被放置在中括号 `[]` 中。真实的链接地址在小括号 `()` 中。



```
现在去看看 [Linux 中国](https://linux.cn) 。

```

自动连接通过用尖括号 `<>` 括住链接地址。这个链接是一个实际的地址（可以是网址或者电子邮件地址）。拼写出来的链接被转换到 HTML 的时候，就变成了有效的链接。



```
<http://automatic-link-to-url.com/>

<name@emailaddress.com>

```

当你想在文本中拼写出网址时，这很有用：


![](/data/attachment/album/202204/24/140306v1bl7j3oqmut0q0z.png)


#### 在 Markdown 中添加图片


链接图片几乎与链接网站一样。网站链接和图片链接微小的不同是，图片链接以感叹号 `!` 开始。


图片名称或者图片描述放置在中括号 `[]` 里。实际链接放置在小括号 `()` 里。


你可以像这样嵌入图片：



```
![alternate text](./images/image.jpg)

```

这儿有一个示例图像链接。这是一个示例链接，没有这个图片，但是这是个好例子，显示了实际链接应该有的样子：



```
![a picture of bill](./images/my_photo_of_me.jpg)

```

![](/data/attachment/album/202204/24/140306yb8ihthb2o83o206.png)


#### 列表


制作列表有很多原因。它们可能用来作为代办事项、大纲中的主题元素、组装项目中的明细栏等等。列表有两种主要类型：无序列表和有序列表。


无序列表没有编号；这些就是我们在许多文档中所看到的列表项。有序列表是有编号的。


要创建一个有序（有编号的）列表，只需在每行用一个数字开始，像这样：



```
1. 项目 一。
2. 项目 二。
3. 项目 三。

```

无序列表没有编号，但要在列表每个项目的开始使用一个星号 `*` 、一个加号 `+` ，或者一个减号 `-` 。我偏向于使用一个星号或者减号，你可以选择适合你的：



```
* 项目 一。
+ 项目 二。
- 项目 三。

```

子项目可以通过缩进被添加到有序和无序列表中，像这样：



```
1. 项目 一
    1. 子项目 一
    2. 子项目 二
2. 项目 二
3. 项目 三

```

![](/data/attachment/album/202204/24/140307yo9bsk15a1not12u.png)


### Markdown 语法速查表


这是一个简短的 Markdown 语法列表为你作为参考，这个列表已经在本小结中介绍。


如果你决定使用 Markdown 作为写作工具，你会发现 Markdown 会让写作变得更加简单。


![](/data/attachment/album/202204/24/140308pu0hhevetvslezao.png)



> 
> **[下载 PDF 格式的 Markdown 速查表](https://drive.google.com/file/d/1y-Qz9PX_2HksEG5D_WwN-asNB-tpjZHV/view?usp=sharing)**
> 
> 
> 


### 结语


Markdown 可以做比我描述的更多的事情。我写作的很大一部分是用我在这里介绍的 Markdown 语法完成的 - 而这些是我最常使用的项目，即使是在更复杂的项目中。


如果这些看起来太简单了，那么说明它真的很容易。Markdown 用来完成简单的写作任务，但是你不必完全听我的。你可以尝试一下！不必安装 Markdown 编辑器；你可以在线尝试。这里有几个 [优秀的在线 Markdown 编辑器](https://itsfoss.com/online-markdown-editors/)。这里我喜欢用这三个：


* John Gruber’s [Dingus](https://daringfireball.net/projects/markdown/dingus)
* [Editor.md](http://editor.md.ipandao.com/en.html)
* [Dillinger](https://dillinger.io/)


[Editor.md](http://Editor.md) 和 Dillinger 会让你看到你的 Markdown 文本作为 HTML 实时渲染的效果。 Dingus 不能实时预览，但是在参考页有 Markdown 的语法速查表。


![](/data/attachment/album/202204/24/140309tougbaxmo7pxaqqu.png)


用以上这些在线编辑器尝试一下文章中的示例。也可以尝试一下你自己的想法。这将使你在可能致力于学习更多的知识之前习惯于 Markdown。




---


via: <https://itsfoss.com/markdown-guide/>


作者：[Bill Dyer](https://itsfoss.com/author/bill/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[hwlife](https://github.com/hwlife) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

In my work, I often have to write code, write the documentation that goes with that code, create Web pages, and work on text restoration projects, and have written several formal papers while I was in school. I can include class notes here, too; I needed to write them for nearly every class.

I use Markdown for nearly all of my writing and it is a major time-saver for me.

In this article, I am going to share my experience with Markdown. You’ll be learning the following:

- What is Markdown?
- How does it work?
- Markdown basic syntax and how to use them

## What is Markdown?

If you are new to Markdown, it is a text-to-HTML conversion tool for web writers. Markdown files follow a specific syntax that is easy to read and just as easy to write. They are plain text files so they can be created using any text editor on any computer. These files can then be turned into Web pages – and Web pages are built using a markup called HTML.

Markdown then, is just a way to create Web pages without the need (or even know how) to write HTML code. Actually, Markdown is an great way to format plain text even if you don’t have to convert to HTML. Someone once described Markdown to me this way:

It isn’twhat you see is what you get, butwhat you see is what you mean

Markdown, however, is more than an easy formatting scheme, it is also a software tool that converts plain text formatting to HTML.

This is why the syntax is important. If you want a title on your Web page, Markdown will create one based on the character(s) you use in front of your title. A sampling of some of Markdown’s syntax is shown this screenshot:

![retext window showing syntax and preview 2](https://itsfoss.com/content/images/wordpress/2021/04/retext_window_showing_syntax_and_preview-2.png)

## So how do I make this plain text to HTML conversion?

John Gruber’s Markdown is a Perl script that is run on the command line. Basically, it reads the Markdown text that you create and builds a Web page from it.

I will avoid the command line here since there are [many outstanding Markdown editors](https://itsfoss.com/best-markdown-editors-linux/) that can do this conversion for you. Not only that, many of these editors will let you write your text and show you what the Web page will look like (called *rendering*) at the same time.

Markdown editors are generally set up to show two frames. The left frame is where you write your text and the right frame shows you what the formatted text will look like in HTML:

![ghostwriter two frames 1](https://itsfoss.com/content/images/wordpress/2021/04/ghostwriter_two_frames-1.png)

When you are finished with your text and are happy with it, simply save the Markdown file. This way, you’ll always have it in case you need to edit or rewrite later. Once the file is saved, you can have the editor export the markdown file to HTML.

The editor will create the Web page, using your Markdown as a reference. Your Markdown file will not be changed during an export – you will still have it – along with a separate, newly created HTML (Web page) file that you can put on a Web server.

**Note**: Many Markdown editors can also export your Markdown files to other formats, such as `.doc`

, `.docx`

, and `.pdf`

. You can learn about those advanced setups, and extra software you might need, later on.

[11 Best Markdown Editors for LinuxMarkdown is a simple markup language. It’s lightweight and easy to learn. If you haven’t seen any markdown document yet, just head over to GitHub and enter any open-source project. The README file almost certainly will be written using Markdown. Basically, you use some ‘code’ before your text](https://itsfoss.com/best-markdown-editors-linux/)![](https://itsfoss.com/content/images/wordpress/2016/10/Best-Markdown-Editors-for-Linux.jpg)


![](https://itsfoss.com/content/images/wordpress/2016/10/Best-Markdown-Editors-for-Linux.jpg)

## Basic Markdown Syntax

To get the new Markdown user up to speed quickly, I will limit this to cover the syntax I use most often. These, I believe will be the most helpful – you can be productive now while you learn more about what Markdown can do for you later on.

### Write Headings

I normally use `#`

characters to denote headings. There are six levels:

```
# Level 1 Heading
## Level 2 Heading
### Level 3 Heading
#### Level 4 Heading
##### Level 5 Heading
###### Level 6 Heading
```


There is another heading style that uses lines underneath the text. I rarely use this type of heading since I am limited to only two. A double line, which is made with the `=`

character, makes a `H1`

heading. A single line, made with the `-`

character, makes a `H2`

heading:

```
Level 1 Heading
===============
Level 2 Heading
---------------
```


![Markdown headings and subheadings](https://itsfoss.com/content/images/wordpress/2021/04/1_md_headings_vscodium.png)

### Paragraphs

Paragraphs are separated by a blank line (make sure that there is a blank line between paragraphs). Do not indent the first line at all. Indenting with a `<Tab>`

or `<spaces>`

has a different purpose in Markdown.

A paragraph is a block of text and should not be indented with spaces or tabs. It can have one line or many lines. To end a paragraph and start a new one, the `<Enter>`

key is hit twice; paragraphs are separated by a blank line.

![Paragraph example in Markdown](https://itsfoss.com/content/images/wordpress/2021/04/2_md_paragraphs_example_vscodium.png)

### Line Breaks

Remember that with paragraphs, a blank line has to separate them and this is done by pressing twice on the `<Enter>`

key. Markdown is strict about it.

Markdown does not support “hard-wrapped” or “fixed-line-length” paragraphs. That is, hitting the `<Enter>`

key once will not force text to a new line. It may appear so in the editing window, but the HTML won’t show it.

Yet, there will be times when you may need to break up paragraphs with some way to break up a line. Markdown does have a way to do this but it may seem a little strange at first: **a line break is made by ending a line with two or more spaces and then hitting the <Enter> key once.**

![Line breaks in Markdown syntax](https://itsfoss.com/content/images/wordpress/2021/04/3_md_line_break_fail_vscodium.png)

Here is a working example of a short verse. Each line has two spaces at the end. The last line, since it’s the end of the verse, doesn’t have the extra spaces. Since it’s the end of the verse (paragraph), I hit the `<Enter>`

key twice:

Baa, baa black sheep,

Have you any wool?.

Yes, sir. Yes, sir.

Three bags full.

Adding two spaces at the end of a line, to create a line break, can take some getting used to.

![Successful line break in Markdown](https://itsfoss.com/content/images/wordpress/2021/04/4_md_line_break_success_vscodium.png)

### Horizontal Rules

Horizontal rules are great for splitting up text into sections.

Use three or more dashes `-`

, underscores `_`

, or asterisks `*`

for horizontal rules, like so:

```
`---`
`***`
`___`
```


You can even put spaces between the characters:

``- - -``


I do not use horizontal rules very often in articles or papers, but they come in handy for me in journal entries, system logs, and class notes.

![Markdown horizontal rules example](https://itsfoss.com/content/images/wordpress/2021/04/5_md_horizontal_rules_vscodium.png)

### Emphasis on text with bold and italics

When you want a word or phrase to stand out and be noticed, you can either make it bold or italicized. Italics and bold text can be made on one of two ways. The first is by surrounding the text with asterisks `*`

, while the second is to use underscores `_`

.

To italicize a word or phrase, surround the text with one underscore or asterisk. To make a word or phrase bold, surround it with two underscores or asterisks:

```
This is *italics* made with asterisks.
This is _italics_ made with underscores.
This is **bold** made with asterisks.
This is __bold__ made with underscores.
```


Remember to use the same character. An asterisk on one side of a word or phrase, and an underscore on the side, will not work. The same character has to be on both sides of the word or phrase.

![Adding bold and italics in Markdown](https://itsfoss.com/content/images/wordpress/2021/04/6_md_emphasis_vscodium.png)

### Block quotes

Block quotes are used for direct quotes. If you were writing a blog entry and you wanted to repeat something that Benjamin Franklin said, you could use a block quote.

A right angle bracket is used to specify a block quote:

```
> This is a block quote.
>> Use two right angle brackets if you want a block quote that is further indented.
```


![Markdown block quote example](https://itsfoss.com/content/images/wordpress/2021/04/7_md_blockquotes_vscodium.png)

### Adding links in Markdown

Links are just plain cool. There are three ways to create links on basic Markdown, but I will only cover two here: Regular links and automatic links.

The third type of link, called reference links, are supported in basic Markdown and more advanced flavors. I want to get to started quickly. You can look up reference links when you are ready for that.

Regular links let you link to various websites. The name of the site, or a phrase you want to use, is placed in square brackets `[]`

. The actual link is inside parentheses `()`

.

`Visit [It's FOSS](https://itsfoss.com) today!`


Automatic links are made with angle brackets `<>`

surrounding the link. The link is an actual address (either a Web or email address). The link is spelled out and, when it is converted to HTML, the spelled out link becomes a working link.

```
<http://automatic-link-to-url.com/>
<
```[[email protected]](/cdn-cgi/l/email-protection)>


This is useful for when you want to spell out the address in your text:

![Adding links in markdown](https://itsfoss.com/content/images/wordpress/2021/04/8_md_links_vscodium.png)

### Adding images in Markdown

Links to images are almost identical to links to Web sites. The small difference between site links and images, is that image links begin with a bang (exclamation point) `!`


The name of the image, or a descriptive phrase of the image, is placed in square brackets `[]`

. The actual link is inside parentheses `()`

.

You can embed images like so:

`![alternate text](./images/image.jpg)`


Here’s an example image link. It is a sample link, with no image, but it is a decent sample of how an actual link might look like:

`![a picture of bill](./images/my_photo_of_me.jpg)`


![Adding images in Markdown](https://itsfoss.com/content/images/wordpress/2021/04/9_md_images_vscodium.png)

### Lists

Lists are made for many reasons. They can be used as ‘things to do’ items, topic elements in an outline, parts lists in an assembly project, and so on. There are two main types of lists: unordered and ordered.

Unordered lists are not numbered; these are the ‘bullet items’ we see in many documents. Ordered lists are numbered.

To create an ordered (numbered) list, just begin each line with a number, like so:

```
1. Item one.
2. Item two.
3. Item three.
```


Unordered lists are not numbered, but use either an asterisk `*`

, a plus sign `+`

, or a minus sign `-`

at the beginning of each item on the list. I prefer to use either an asterisk or minus sign, but you get to choose:

```
* Item one.
+ Item two.
- Item three.
```


Sub-items can be added to both ordered and unordered lists by indenting, like so:

```
1. Item 1
1. Sub-item 1
2. Sub-item 2
2. Item 2
3. Item 3
```


![Ordered and unordered lists in Markdown](https://itsfoss.com/content/images/wordpress/2021/04/10_md_lists_vscodium.png)

[Marktext is an Excellent Editor Even for Those Who Don’t Know MarkdownAnother Markdown editor? Have we not seen all kinds of Markdown editors already? I understand that feeling. If you are a Markdown lover, from Joplin to Zettlr, you have tried most of them. And if you are not a Markdown fan, you probably don’t care about these editors. Markdown](https://itsfoss.com/marktext-editor/)![](https://itsfoss.com/content/images/wordpress/2022/08/marktext-markdown-editor.png)


![](https://itsfoss.com/content/images/wordpress/2022/08/marktext-markdown-editor.png)

## Markdown syntax cheat sheet

For your reference, here is a short listing of Markdown syntax that has been covered in this small introduction.

If you decide to adopt it as a writing tool, you’ll find that Markdown has the means to simplify writing even more.

![markdown syntax cheatsheet](https://itsfoss.com/content/images/wordpress/2021/04/markdown-syntax-cheatsheet.png)

## Conclusion

Markdown can do more than what I have described here. A huge percentage of my writing can be accomplished with the Markdown syntax I have covered here – and these are the items I use most often even in more complex projects.

If all of this seems too simple, it really is that easy. Markdown was built to simply the writing task, but you don’t have to take my word for it. Try it out! There is no need to install a Markdown editor; you can do this online. There are several [good online Markdown editors](https://itsfoss.com/online-markdown-editors/). Here are three that I prefer:

John Gruber’s [Dingus](https://daringfireball.net/projects/markdown/dingus), [Editor.md](http://editor.md.ipandao.com/en.html), and [Dillinger](https://dillinger.io/). Editor.md and Dillinger will let you see your Markdown rendered as HTML in real time. Dingus doesn’t preview in real time, but there is a Markdown syntax cheat sheet on the page for reference.

![Editing Markdown in browser](https://itsfoss.com/content/images/wordpress/2021/04/editor-md_page_in_browser-1.png)

Try out some of the examples in this article on either of these online editors. Try out some of your own ideas, too. This will let you get used to Markdown before possibly committing to learn more about it.

And if you want to support It’s FOSS, you can buy our Markdown guide in PDF and EPUB format. It covers what you just learned with a bit more explanation and examples.