Skip to content

Markdown 速查表

用于格式化文本的轻量级标记语言。

01

入门基础

标题与段落

使用 # 表示标题(1-6 级)。备选语法用 === 表示 H1,--- 表示 H2。段落之间需要空行分隔。Markdown 的设计初衷是作为纯文本也具有良好的可读性。

markdown
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6

Alternative Heading 1
=====================

Alternative Heading 2
---------------------

This is a paragraph. Just write
plain text. Leave a blank line
between paragraphs.

换行

行尾两个空格或反斜杠可创建硬换行(<br>)。没有这些标记时,单个换行符是软换行,会合并为一个段落。始终用空行分隔段落。

markdown
First line  
Second line (two trailing spaces above)

Or use a backslash at end of line\
to force a line break.

Soft wrap without break:
just continue on the next line
and it joins into one.

注释

Markdown 没有官方注释语法,但 HTML 注释(<!-- -->)在大多数渲染器中可用,且在输出中隐藏。[//]: # 技巧使用空链接引用来添加备注。注释在源码中仍然可见。

markdown
<!-- HTML comments work in most Markdown -->

[//]: # (This is a comment)

[//]: # "Another comment style"

<!-- 
  Multi-line comment
  not rendered in output
-->

空白与缩进

四个前导空格会将文本变为代码块——注意避免意外缩进。缩进对嵌套列表很重要(每级 2-4 个空格)。制表符通常被视为 4 个空格。保持空格使用一致以避免不同解析器的渲染问题。

markdown
Paragraph one.

    This is an indented code block
    because of 4 spaces.

> Quoted text needs > prefix.

   Three spaces is just text
   (less than 4).

水平分隔线

三个或更多的连字符、星号或下划线创建水平分隔线(<hr>)。连字符最常见。字符间允许空格。为清晰起见,在水平线前后留空行。避免将 ---(分隔线)与 #(标题)混淆。

markdown
---

***

___

   * * *

Text above

---

Text below
02

文本格式化

粗体与强调

双星号或双下划线产生粗体(<strong>)。推荐使用星号,因为下划线在单词中间不起作用(snake_case)。可以在粗体中嵌套其他强调。HTML <strong> 标签作为备选方案。

markdown
**bold text**
__also bold__

**bold with *italic* inside**

<strong>HTML bold also works</strong>

**bold**

斜体与强调

单星号或单下划线创建斜体(<em>)。星号在单词中间有效;下划线将单词内部的下划线视为字面量(所以 no_italic_here 不会斜体)。使用 <em> 作为备选。斜体用于强调,不仅仅是样式。

markdown
*italic text*
_also italic_

*italic*

<em>HTML italic</em>

normal *italic* normal

粗体+斜体组合

三星号/下划线同时产生粗体和斜体。也可以嵌套使用:粗体包含斜体或反之。渲染顺序不影响结果,但应保持一致以提高可读性。

markdown
***bold and italic***
___also both___

**_bold and italic_**
*__bold and italic__*

combine **bold** and *italic*

删除线

双波浪号(~~)创建删除线,是 GitHub Flavored Markdown 的一部分,现已广泛支持。HTML <del> 和 <s> 标签是等效的备选方案。标准(原始)Markdown 不包含删除线。

markdown
~~strikethrough~~

~~crossed out text~~

<del>HTML delete</del>
<s>HTML strikethrough</s>

~~done~~ and ~~pending~~

标记/高亮

双等号(==)高亮是某些解析器支持的扩展(不在 GFM 核心中)。HTML <mark> 标签通用支持。少量使用高亮来吸引对关键术语的注意——过度使用会降低效果。

markdown
==highlighted text==

<mark>HTML mark</mark>

This is ==important== text.

Compare **bold**, *italic*, ==mark==.

下标与上标

波浪号(下标)和脱字符(上标)语法(~ ~ 和 ^ ^)是不在标准 Markdown 或 GFM 中的扩展——由 Pandoc、R Markdown 等支持。为通用支持,使用 HTML <sub> 和 <sup> 或 Unicode 字符。

markdown
H~2~O is water

E = mc^2^

x^2^ + y^2^ = r^2^

1st^st^ January

CO~2~ emissions
03

列表

无序列表

使用 -、* 或 + 作为无序列表项——选择一种并保持一致(- 最常见)。缩进 2-4 个空格创建嵌套项。在同一列表中混用标记符可能在某些解析器中导致问题。每行一项。

markdown
- item one
- item two
- item three

* asterisk works too
+ plus sign also works

  - sub-item (indent 2 spaces)
  - another sub-item

有序列表

有序列表使用数字后跟句点。实际数字不需要连续——Markdown 会重新编号。要从特定数字开始,先使用该数字(10. 从 10 开始)。对所有项使用 1. 可自动编号。

markdown
1. first
2. second
3. third

1. auto-numbered
1. still increments
1. third item

10. starts at 10
11. next

嵌套列表

嵌套列表项缩进 2-4 个空格(混合类型时 4 个最安全)。可以在不同层级混合有序和无序列表。深层嵌套(3+ 级)会降低可读性——考虑重构。保持缩进一致。

markdown
1. top level
   - nested unordered
   - second nested
2. back to top
   1. nested ordered
   2. another
3. third top

- fruits
  - apple
    - granny smith
  - banana

任务列表

任务列表语法(- [x] 和 - [ ])是 GitHub Flavored Markdown 扩展。复选框在 GitHub/GitLab 上渲染为可交互。支持有序和无序列表。非常适合 README、问题跟踪器和进度跟踪。

markdown
- [x] completed task
- [ ] incomplete task
- [ ] another todo

1. [x] done
2. [ ] not done

- [x] Write the docs
- [ ] Review PR
- [ ] Deploy

松散列表与紧凑列表

如果列表项之间用空行分隔,列表是'松散'的(每项包裹在 <p> 中)。没有空行则是'紧凑'的(无 <p> 包裹)。这影响 HTML 输出的间距。有意添加空行来控制段落包裹。

markdown
- tight item
- tight item
- tight item

- loose item

- loose item

- loose item

带多段落的列表

要在列表项中包含多个段落或块元素,将续行缩进 4 个空格(或与列表标记内容对齐)。空行分隔子块。这对复杂列表结构至关重要,但容易出错。

markdown
- First item

  Second paragraph of first item.

- Second item

  > A blockquote inside a list item.
  >
  > Must be indented to align.

- Third item
05

图片

基本图片

图片语法类似链接,但加 ! 前缀。alt 文本是可访问性必需的——它为屏幕阅读器描述图片,并在图片加载失败时显示。路径可以是相对、绝对或 URL。标题可选。

markdown
![alt text](image.png)

![alt text](https://example.com/img.png)

![alt text](/path/to/image.png "Optional title")

带 alt 文本的图片

alt 文本应有意义且简洁——描述图片的用途,而不仅仅是'图片'。对于装饰性图片,空 alt(alt='')是可接受的,告知屏幕阅读器跳过。不要使用'...的图片'——屏幕阅读器已经会播报。

markdown
![A diagram showing the system architecture](architecture.png)

![Photo: mountain landscape at sunset](mountains.jpg "Sunset over the Alps")

![](decorative-icon.png)

链接图片

通过将图片语法嵌套在链接语法中使图片可点击:[![alt](img)](url)。常用于链接到全尺寸图的缩略图、链接到 CI 状态的徽章、链接到首页的 logo。alt 文本应描述目标,而不仅是图片。

markdown
[![alt text](thumbnail.png)](https://example.com/full)

[![logo](logo.png "Visit home")](/)

[![badge](https://img.shields.io/build.svg)](https://ci.example.com)

引用图片

引用图片使用与链接相同的引用风格,加 ! 前缀。定义一次图片引用即可重复使用。当同一图片出现多次或为保持图片密集段落的可读性时很有用。

markdown
![alt text][img-ref]

[img-ref]: https://example.com/image.png "Title"

![alt][logo]

[logo]: /assets/logo.png

图片尺寸与图注

标准 Markdown 没有图片尺寸语法——使用 HTML <img> 的 width/height 属性。百分比宽度实现响应式。<figure> + <figcaption> 组合提供语义化图注(HTML5)。某些扩展 Markdown(Pandoc)支持 ![alt](img){width=300}。

markdown
<img src="image.png" width="300" height="200" alt="sized image">

<img src="banner.png" width="100%" alt="responsive">

<figure>
  <img src="diagram.png" alt="flowchart">
  <figcaption>Figure 1: System flow</figcaption>
</figure>
06

行内代码与转义

行内代码

用单反引号包裹行内代码。要在代码中显示反引号,使用双反引号作为分隔符(或更多)。行内代码以等宽字体渲染,不会被解析为 Markdown。适合文件名、命令和代码标识符。

markdown
Use the `printf()` function.

Inline code: `const x = 42;`

Code with backticks: ``use `code` here``

A `<div>` element.

代码跨度中转义反引号

要在代码跨度中包含反引号,使用比内部出现数量更多的反引号作为分隔符:双反引号包裹单反引号。在代码跨度外,用反斜杠转义反引号。闭合围栏必须与起始围栏长度匹配。

markdown
To show a backtick inside code,
use more backticks as delimiters:

`` ` ``        one backtick
`` a`b ``      backtick inside text

Use \ to escape outside code:
A literal backtick: \`

代码跨度与空白

代码跨度精确保留内部空白。常用技巧:用单个前导/尾随空格包裹内容来显示原本会被修剪的前导/尾随空格。每侧一个空格会自动去除。

markdown
`  spaced  `  (spaces preserved)

` code ` with one space pad

`function f() { return; }`

`  multi   word   code  `

反斜杠转义

反斜杠转义特殊 Markdown 字符:反引号、星号、下划线、花括号、方括号、圆括号、井号、加号、减号、点、感叹号以及反斜杠本身。转义使字符按字面量渲染。只转义原本会被解释为格式化的字符。

markdown
\*not italic\*
\_not italic\_
\#not a heading
\[not a link](url)
\`not code\`
\\backslash itself
\!not an image

需要转义的字符

这些是 Markdown 中可转义的字符。并非所有都需要在所有地方转义——# 只在行首有意义,- 在列表上下文中,等等。不确定时,转义。花括号和圆括号通常只在特定上下文中需要转义(链接、模板字符串)。

markdown
\\     backslash
\`     backtick
\*     asterisk
\_     underscore
\{ \}  curly braces
\[ \]  square brackets
\( \)  parentheses
\#     hash
\+     plus
\-     minus
\.     dot
\!     exclamation
07

代码块

缩进代码块

缩进 4 个空格(或一个制表符)创建代码块。缩进代码块没有语法高亮和语言标签。不能在列表项内使用(被视为续行)。新内容优先使用围栏代码块——缩进块是遗留方式。

markdown
Normal paragraph.

    // indented code (4 spaces)
    function hello() {
      console.log("hi");
    }

Back to normal text.

围栏代码块

三反引号或三波浪号创建围栏代码块。围栏优于缩进块——它们明确且支持语言提示。当代码本身包含三反引号时,波浪号(~~~)很有用。始终用相同字符数闭合围栏。

markdown
```
plain code block
multiple lines
```

~~~
also a fenced block
using tildes
~~~

语法高亮

在起始围栏后添加语言标识符以获得语法高亮(如 js、python)。大多数渲染器使用 highlight.js 或 Prism。常用别名:js、ts、py、rb、sh、json、html、css。如果语言未知,省略它以使用纯等宽字体。

markdown
```javascript
const greet = (name) => {
  console.log(`Hello, ${name}!`);
};
```

```python
def greet(name):
    print(f"Hello, {name}!")
```

```bash
echo "Hello, $USER"
```

嵌套代码围栏

要在代码块中显示反引号,使用不同的围栏(用波浪号包裹反引号内容,或使用比内部出现数量更多的反引号)。四个反引号围栏可包含三反引号内容。闭合围栏必须与起始围栏长度匹配。

markdown
Use four backticks to wrap triple backticks:

````
```js
console.log("inside");
```
````

Or use tildes to wrap backtick content:

~~~
```js
code with backticks
```
~~~

列表中的代码

列表项内的围栏代码块必须缩进以与列表文本对齐(- 列表通常 3 个空格,1. 列表 4 个)。列表内的缩进代码块需要 8 个空格(列表 4 + 代码 4)。正确对齐是 Markdown 中较棘手的任务之一。

markdown
1. Item with code:

   ```
   code block indented to align
   ```

2. Next item with inline `code`.

- Or indent code 4 spaces beyond
      the list marker (8 total).
08

引用块

基本引用块

> 字符开始引用块。可以给每行加前缀,也可以只给第一行——大多数解析器会合并连续行。引用块渲染为缩进、带样式的引用(<blockquote>)。用于引用、提示和突出摘录。

markdown
> This is a blockquote.
> It can span multiple lines.

> Or just prefix the first line
and continuation lines without >
will still be part of the quote.

多行引用块

使用空白 > 行来分隔引用块内的段落(否则会合并)。空行上的 > 保持引用打开。段落之间没有空行时,文本流为一个段落。始终以普通行结束。

markdown
> First paragraph of quote.
>
> Second paragraph of quote.
>
> > Nested quote starts here.
> > Continues on next line.

End of quote.

嵌套引用块

多个 > 字符创建嵌套引用块(>> 两级,>>> 三级)。添加空格提高可读性。深层嵌套(3+ 级)变得难以阅读和样式化——建议扁平化。每级在 HTML 输出中添加缩进。

markdown
> Outer quote
>
> > Inner nested quote
> > still nested
>
> Back to outer

> Outer
>> Deeper
>>> Deepest level

包含其他元素的引用块

引用块可以包含大多数 Markdown 元素:标题、列表、代码、强调甚至表格。每行加 > 前缀(和空格)使其保持在引用内。这使引用块在提示框方面很强大,但复杂内容需要仔细对齐 >。

markdown
> ## Heading in quote
>
> - list item one
> - list item two
>
> *italic* and **bold** work too.
>
> ```
> code block inside quote
> ```

带引用来源的引用块

Markdown 没有正式的引用语法。常见约定:在新行用破折号前缀作者,或使用 HTML <blockquote> 配合 <footer> 和 <cite> 实现语义化引用。对于学术引用,考虑使用脚注标注来源。

markdown
> Be the change you wish to see
> in the world.

> — Mahatma Gandhi

> "Simplicity is the soul of efficiency."
> — Austin Freeman

<blockquote>
  <p>Quote text.</p>
  <footer>— <cite>Author</cite></footer>
</blockquote>
09

表格

基本表格

表格使用管道符(|)分隔列,连字符分隔表头。表头行是必需的。行首尾的管道符可选但提高可读性。列宽不重要——单元格自动调整。表格是 GitHub Flavored Markdown 扩展。

markdown
| Name  | Age | City     |
|-------|-----|----------|
| Alice | 30  | NYC      |
| Bob   | 25  | London   |
| Carol | 35  | Paris    |

单元格对齐

分隔行中的冒号控制对齐::--- 左对齐,:---: 居中,---: 右对齐。没有冒号时默认左对齐。对齐应用于整列并在 HTML 输出中设置 text-align CSS。适用于数字列(右对齐)和表头(居中)。

markdown
| Left   | Center | Right  |
|:-------|:------:|-------:|
| left   | center | right  |
| L      | C      | R      |

| Default | alignment |
|---------|-----------|
| no mark | left      |

带格式的表格

单元格可以包含行内 Markdown:粗体、斜体、代码、链接、删除线、emoji。块元素(标题、列表、代码块)在单元格内不起作用——需要时使用 HTML。保持单元格内容简短以提高可读性。用反斜杠转义管道符来显示字面管道。

markdown
| Feature    | Status | Notes              |
|------------|:------:|--------------------|
| **Bold**   | done   | *finished*         |
| `code`     | wip    | [docs](docs.md)    |
| ~~old~~    | removed| gone in v2         |
| [link][1]  | done   | see [ref][1]       |

复杂表格

表格可扩展到多行多列,但每个单元格保持简单。对于带逗号的代码跨度,用反引号包裹。用反斜杠加管道符转义字面管道符。复杂数据(多行单元格、嵌套表格)需要 HTML <table>。如果表格太宽,考虑使用列表。

markdown
| Name  | Role    | Skills              | Available |
|-------|---------|---------------------|:---------:|
| Alice | Lead    | `js`, `ts`, `go`   | yes       |
| Bob   | Junior  | `py`, `sql`         | no        |
| Carol | Senior  | `rust`, `c++`       | yes       |

Escaped pipe: \| literal

表格技巧与限制

Markdown 表格有限制:不能合并单元格、不能多行内容、不能有块元素、不能嵌套表格。这些情况下使用原始 HTML。保持表格小而可扫描。如果表格需要脚注或长文本,拆分它。TablesGenerator.com 等工具帮助快速构建。

markdown
| A | B |
|---|---|
| 1 | 2 |

Tip: Use \| to escape pipes.

Limitation: no multi-line cells,
no blockquotes, no lists in cells.

For complex tables, use HTML:
<table><tr><td>cell</td></tr></table>
10

脚注

基本脚注

脚注在文本中使用 [^id] 标记,在其他位置使用 [^id]: 定义。id 可以是数字或单词。渲染器创建可点击的上标数字和底部的脚注部分。由 GFM、Pandoc 和大多数现代解析器支持。

markdown
Here is a sentence with a footnote.[^1]

[^1]: This is the footnote text.

Another sentence.[^note]

[^note]: Footnotes can use named labels.

多次引用

一个脚注可以被多次引用——每个 [^id] 链接到同一个定义。脚注定义的续行需要缩进。第一个引用通常获得上标数字;后续引用链接到同一个注释。

markdown
Use the term[^term] twice[^term] in the text.

[^term]: A single definition referenced
    multiple times.

First[^1] and second[^1] reference.

[^1]: One definition, two links back.

行内脚注

行内脚注(^[text])将注释内容直接嵌入引用处,由 Pandoc 和某些扩展支持(非 GFM 核心)。它们将所有内容放在一处,但可能打断阅读流。短注释使用行内;长注释更适合用引用式。

markdown
Here is an inline footnote^[The note text
right here in the body.] in a sentence.

Pandoc also supports: ^[inline notes].

And another[^1].

[^1]: Traditional reference style.

脚注定位

脚注定义可以放在文档的任何位置——渲染器会收集并在末尾显示它们。续行缩进(4 个空格)。脚注在引用块和列表项中有效。源码中的位置不影响输出位置。

markdown
Body text with a note.[^1]

More paragraphs here.

[^1]: Definitions can appear anywhere.
    They float to the bottom in output.

> Even inside blockquotes.[^2]

[^2]: The renderer collects all notes
    and renders them at the end.

带代码和格式的脚注

脚注定义支持行内 Markdown(粗体、斜体、代码)和多段落内容(续行缩进 4 个空格)。脚注内的缩进代码块需要 8 个空格。保持脚注简洁——长技术内容更适合放在正文或附录中。

markdown
See the API[^api] for details.

[^api]: Use `fetch('/api/data')` to call.
    Supports **bold** and *italic*.
    Multi-paragraph: indent each line.

    Code blocks need indentation:
        indented code inside footnote
11

定义列表

基本定义列表

定义列表在一行放术语,下一行用冒号(:)后跟定义。由 PHP Markdown Extra、Pandoc 和某些扩展支持(非 GFM 核心)。渲染为 <dl><dt><dd>。适用于术语表和术语-定义对。

markdown
Term
: Definition of the term.

Markdown
: A lightweight markup language.

HTML
: HyperText Markup Language.

多个术语

多个连续术语(无冒号)可以共享一个定义。每个术语成为 <dt>,定义成为 <dd>。当同一概念有多个名称或别名时很有用。术语分开放在不同行。

markdown
CSS
Cascading Style Sheets
: A style sheet language.

JS
JavaScript
: A programming language.

Both terms
: Share one definition.

多个描述

一个术语的多个连续定义(每行以冒号开头)创建多个 <dd> 元素。适用于列出一个术语的几个不同含义或方面。每个冒号行是一个独立的定义条目。

markdown
Markdown
: A markup language.
: A tool for writing for the web.

Apple
: A fruit.
: A technology company.

嵌套定义列表

嵌套定义列表需要在父定义内缩进子列表(2-4 个空格)。解析器支持各异——并非所有渲染器都能处理嵌套。对于复杂的层次术语结构,考虑使用 HTML <dl> 以确保可靠性。在目标渲染器中测试。

markdown
Term
: Definition one.

  Nested term
  : Nested definition.

: Definition two for top term.

带块元素的定义列表

定义列表条目可以包含多个段落、引用块和代码块——续行内容缩进 4 个空格(超出冒号部分)。内部代码块需要 8 个空格。这很强大但脆弱;复杂内容在 HTML <dl> 中更安全。

markdown
Markdown
:   A markup language.

    With multiple paragraphs in the
    definition (indent 4 spaces).

    > Blockquote inside definition.

:   Second definition with code:

        indented code (8 spaces)
12

目录

自动目录

大多数解析器默认不自动生成目录。某些(带 toc 插件的 marked、MDX)支持 [TOC] 或 {:toc} 占位符。Doctoc 和 markdown-toc 等工具从标题生成目录。对于静态站点,TOC 组件在构建或运行时读取标题锚点。

markdown
## Table of Contents

1. [Introduction](#introduction)
2. [Installation](#installation)
3. [Usage](#usage)
4. [FAQ](#faq)

Some parsers auto-generate a TOC
with `[TOC]` or `{:toc}` markers.

手动目录

手动目录是指向标题锚点的嵌套链接列表。锚点 id 源自标题文本(小写、空格转连字符、移除标点)。缩进以反映标题层级。标题变更时手动更新,或使用生成器。

markdown
# My Document

## Contents
- [Intro](#intro)
- [Setup](#setup)
  - [Prerequisites](#prerequisites)
  - [Install](#install)
- [Usage](#usage)

## Intro
...

锚点生成规则

锚点生成因解析器而异。常见规则:小写、空格转连字符、去除标点、保留字母数字。GitHub 保留 emoji 和某些 unicode。数字和点通常被移除。不确定时,检查渲染后的 HTML 找到实际 id。

markdown
## Hello World        -> #hello-world
## Hello, World!      -> #hello-world
## C++ & Rust         -> #c--rust
## Uber Heading       -> #uber-heading
## 1. Getting Started  -> #1-getting-started
## API v2.0           -> #api-v20

自定义锚点 ID

为获得可靠的锚点,添加显式 ID。{#id} 属性语法适用于 Pandoc、marked 和某些扩展。HTML <a id="..."> 技巧到处适用——放在标题前或内部。自定义 ID 在标题文本变更后仍然有效。

markdown
## Heading {#custom-id}

<a id="my-anchor"></a>
## Heading

## Heading<a id="also"></a>

[link to custom](#custom-id)

目录导航与返回链接

返回顶部链接改善长文档的导航。<details>/<summary> HTML 创建可折叠目录——适合侧边栏或长 README。与锚点链接结合实现平滑导航。在 CSS 中添加 scroll-margin-top 来偏移固定标题。

markdown
## Section

Content here.

[Back to top](#table-of-contents)

---

<details>
<summary>Table of Contents</summary>

- [Section 1](#section-1)
- [Section 2](#section-2)
</details>
13

Emoji 与特殊字符

Emoji 简码

Emoji 简码(:name:)由 GitHub/GitLab 支持(使用 EmojiOne/Twemoji 集)。有数千个命名的 emoji。简码方便但不可移植——在非 GFM 解析器中可能按字面量渲染。使用原始 Unicode emoji 以获得最大兼容性。

markdown
:smile: :heart: :thumbsup: :rocket:

GFM supports shortcodes:
:sparkles: new feature :tada:

:checkered_flag: launch!
:bug: fixed issue #42

常用 Emoji

大多数现代编辑器和渲染器直接处理原始 Unicode emoji——只需粘贴。简码是 GitHub 特有的。emoji 可以传达语气和状态(check/cross 表示结果,bug 表示 bug)。在正式文档中少量使用;在 README 和变更日志中很好用。

markdown
:smile: smile    :heart: heart    :thumbsup: thumbsup
:rocket: rocket   :star: star     :warning: warning
:check: check    :x: cross       :bulb: bulb
:fire: fire      :tada: tada      :bug: bug
:book: book      :wrench: wrench  :zap: zap

HTML 实体

HTML 实体在 Markdown 中有效,因为它会传递到 HTML。使用 &amp; 表示字面 & 符号(尤其在 URL/属性中),&lt; &gt; 表示尖括号,&copy; &reg; &trade; 表示符号。数字形式(&#169;)和十六进制(&#x00A9;)覆盖任何 Unicode 字符。

markdown
&amp;  &lt;  &gt;  &quot;  &apos;

&copy;  &reg;  &trade;

&mdash; &ndash; &hellip;

&#169;  &#x00A9;

Use &amp; to show a literal &.

特殊字符

Markdown 默认不将普通标点转换为排版字符(智能引号)——使用 HTML 实体或粘贴 Unicode。破折号和弯引号改善排版。许多编辑器会自动纠正这些。对于箭头和符号,实体最清晰。

markdown
-- em dash (or &mdash;)
-- en dash (or &ndash;)
... ellipsis (or &hellip;)
' ' curly quotes (&lsquo; &rsquo;)
" " curly quotes (&ldquo; &rdquo;)
-> arrow (or &rarr;)
(tm) (r) (c)

组合 Emoji 与 Markdown

emoji 与所有 Markdown 元素组合:标题、列表、粗体、链接、引用块。它们为变更日志、README 和问题模板添加视觉提示。保持 emoji 使用一致(例如保持变更日志约定)。在正式/学术写作中避免使用 emoji。

markdown
## New Features

- :sparkles: Added dark mode
- :bug: Fixed login bug
- :memo: Updated docs

**Status:** :white_check_mark: Ready

> :warning: **Warning:** Deprecated!

[:book: Read the docs](/docs)
14

Mermaid 图表

Mermaid 流程图

Mermaid 是一种基于文本的图表语言,由 GitHub、GitLab 和许多静态站点生成器渲染。将 Mermaid 代码包裹在 mermaid 围栏块中。flowchart TD(自上而下)或 LR(从左到右)定义方向。节点使用 []、{}、() 表示不同形状。

markdown
```mermaid
flowchart TD
    A[Start] --> B{Is it?}
    B -->|Yes| C[Do it]
    B -->|No| D[Do not]
    C --> E[End]
    D --> E
```

Mermaid 时序图

时序图显示参与者之间随时间的交互。->> 是实心箭头(请求),-->> 是虚线箭头(响应)。participant 声明参与者。适用于记录 API 流程、协议和消息传递。在 GitHub 上自动渲染。

markdown
```mermaid
sequenceDiagram
    participant A as Alice
    participant B as Bob
    A->>B: Hello Bob!
    B-->>A: Hi Alice!
    A->>B: How are you?
    B-->>A: Good, thanks!
```

Mermaid 类图

类图建模面向对象结构。+ 表示公开,- 私有,# 受保护。<|-- 表示继承。Mermaid 支持关联、组合和接口。非常适合直接在 Markdown 中记录代码架构。

markdown
```mermaid
classDiagram
    class Animal {
        +String name
        +int age
        +makeSound() void
    }
    class Dog {
        +fetch() void
    }
    Animal <|-- Dog
```

Mermaid 状态图

状态图显示状态转换。[*] 标记开始/结束状态。箭头用事件标注转换(state --> state: event)。适用于记录协议、UI 状态和对象生命周期。stateDiagram-v2 是现代语法。

markdown
```mermaid
stateDiagram-v2
    [*] --> Idle
    Idle --> Processing: start
    Processing --> Done: complete
    Processing --> Error: fail
    Done --> [*]
    Error --> [*]
```

Mermaid 甘特图

甘特图可视化项目进度。dateFormat 设置日期解析。任务使用 id、开始、持续时间(如 7d)。'after a1' 链接任务。section 分组相关任务。适用于 README 和项目计划——在 GitHub 上无需外部工具自动渲染。

markdown
```mermaid
gantt
    title Project Schedule
    dateFormat YYYY-MM-DD
    section Design
    Spec        :a1, 2024-01-01, 7d
    Mockups     :after a1, 5d
    section Build
    Develop     :2024-01-13, 14d
    Test        :7d
```

Mermaid 饼图

饼图以百分比显示比例(值是相对的)。标题可选。标签用引号,值在冒号后。Mermaid 还支持 git 图、ER 图、旅程图和思维导图——都可以在受支持平台的 Markdown 中渲染。

markdown
```mermaid
pie title Browser Market Share
    "Chrome" : 65
    "Safari" : 18
    "Edge" : 5
    "Firefox" : 3
    "Other" : 9
```
15

GitHub Flavored Markdown

任务列表(GFM)

GitHub Flavored Markdown(GFM)任务列表在 GitHub 上渲染为可交互复选框。[x] 已勾选,[ ] 未勾选。它们在 issue、PR、评论和 README 中有效。进度条出现在 issue 列表中。该语法现已广泛支持,不限于 GitHub。

markdown
- [x] Done task
- [ ] Todo task
- [ ] Another todo

## Project Status
- [x] Design
- [x] Implement
- [ ] Test
- [ ] Deploy

删除线(GFM)

GFM 在标准 Markdown 中添加了删除线(~~text~~)。它可以跨行并与其他格式组合。渲染为 <del>。最初是 GFM 扩展,现在已纳入 CommonMark 规范补充。适用于显示编辑和弃用。

markdown
~~deleted text~~

old ~~price~~ new

~~this line is wrong~~

Combine: **bold ~~struck~~ text**

~~multi-line
strikethrough~~

表格(GFM)

表格是 GFM 功能(非原始 Markdown)。需要带表头行和分隔符的管道语法。GFM 表格支持对齐、行内格式和转义管道符。对于复杂表格(合并、嵌套),回退到 HTML。

markdown
| Feature    | Status |
|------------|:------:|
| Tables     | yes    |
| Tasks      | yes    |
| Auto-links | yes    |

Tables are core to GFM, not in
the original Markdown spec.

GitHub 提示框

GitHub 提示框(2023+)在引用块第一行使用 [!TYPE] 标记:NOTE、TIP、IMPORTANT、WARNING、CAUTION。它们在 GitHub 上以不同颜色和图标渲染。适合 README 和文档中的提示框。该语法是 GitHub 特有的。

markdown
> [!NOTE]
> Useful information that users
> should know.

> [!WARNING]
> Urgent info about risks.

> [!IMPORTANT]
> Key information.

> [!TIP]
> Helpful advice.

不允许的原始 HTML

GFM 为安全而过滤原始 HTML——<script>、<style>、<title>,有时 <iframe> 和事件处理器被移除。这防止用户生成内容中的 XSS。安全 HTML 如 <details>、<kbd>、<sup> 会通过。混合 HTML 时了解渲染器的白名单。

markdown
GFM sanitizes some HTML tags:

<script>alert('xss')</script>
<style>body { color: red; }</style>
<title>Page</title>

These are stripped for security.

自动链接(GFM)

GFM 自动链接纯 URL(https://...)和 www. 域名,无需尖括号。为可移植性,尖括号(<url>)在所有变体中有效。自动链接可能令人意外——当想与相邻其他文本一起字面显示 URL 时使用尖括号。

markdown
Visit https://example.com now.

www.google.com auto-links too.

<https://example.com> always works.

In GFM, bare URLs and www. links
become clickable automatically.
16

扩展语法

高亮

==text== 高亮语法是扩展(不在 GFM 或 CommonMark 中)——由某些解析器如带插件的 Markdown-it 支持。HTML <mark> 标签通用支持。使用高亮来强调关键术语;过度使用会降低效果。

markdown
==highlighted text==

<mark>HTML highlight</mark>

Some parsers (not GFM core) support
==this syntax== for highlighting.

Use <mark> for universal support.

下标与上标

下标(~text~)和上标(^text^)是 Pandoc/markdown-it 扩展,非标准。为通用渲染,使用 HTML <sub>/<sup> 或 Unicode。适用于用 Markdown 编写的科学和数学内容。

markdown
H~2~O (subscript)

x^2^ (superscript)

E = mc^2^

H~2~SO~4~

Pandoc/R Markdown support ~ and ^.

属性语法

{#id .class key=val} 属性语法(Pandoc、某些 markdown-it 插件)为元素附加 HTML 属性。适用于样式钩子、自定义锚点和链接目标。不可移植——在不支持的解析器中回退为字面文本。少量使用。

markdown
## Heading {#id .class}

![alt](img.png){width=50%}

[link](url){target=_blank}

> Quote {.callout}

自定义容器

自定义容器(:::type ... :::)由 VuePress、Docusaurus、VitePress 等文档工具支持。它们渲染为带样式的提示框。type 映射到 CSS 类。适合主题化文档,但不可移植到纯 Markdown 渲染器。

markdown
:::note
This is a note container.
:::

:::warning
This is a warning.
:::

::: details Open me
Hidden content.
:::

数学与 LaTeX(KaTeX)

数学支持取决于渲染器:GitHub 支持 $...$ 行内和 $$...$$ 块(使用 MathJax/KaTeX)。并非所有 Markdown 解析器都支持数学。为最大兼容性,将数学渲染为图片或直接使用 MathJax。适用于科学和技术文档。

markdown
Inline: $E = mc^2$ in a sentence.

Block:

$$
\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
$$

Some parsers support \( \) and
\[ \] delimiters too.
17

工具与编辑器

VS Code

VS Code 内置 Markdown 预览和丰富的扩展生态。Markdown All in One 添加快捷键(粗体、目录、预览)。markdownlint 强制风格。Preview Enhanced 支持图表、数学和导出。配置自动换行以舒适编辑。

markdown
# VS Code Markdown extensions:
- Markdown All in One
- Markdown Preview Enhanced
- markdownlint
- Pandoc

# Built-in preview: Ctrl+Shift+V
# Side-by-side: Ctrl+K V

# Settings:
"[markdown]": {
  "editor.wordWrap": "on",
  "editor.quickSuggestions": true
}

Typora

Typora 是一款流行的所见即所得 Markdown 编辑器——它在你输入时实时渲染,消除了分屏工作流。它支持 GFM、表格、代码围栏、数学和 Mermaid。通过内置工具导出为多种格式。付费但精致;适合写作导向的工作流。

markdown
Typora: a WYSIWYG Markdown editor.

- Live rendering as you type
- No split pane (instant preview)
- Supports tables, diagrams, math
- Export to PDF, HTML, Word, LaTeX
- Themes and custom CSS

Shortcuts:
- Ctrl+/ toggle source code mode
- Ctrl+B bold, Ctrl+I italic

Obsidian

Obsidian 将笔记存储为本地 Markdown 文件,具有强大功能:wiki 链接、反向链接、图谱视图和标签。它通过提示框、嵌入和 Dataview 扩展 Markdown。非常适合 Zettelkasten 和个人知识管理。个人使用免费。

markdown
Obsidian: a Markdown knowledge base.

- Plain-text .md files (local-first)
- Bidirectional [[wiki links]]
- #tags and nested tags
- Backlinks and graph view
- Plugins for almost everything
- Callouts: > [!note] Title

[[note|alias]] for renamed links.

Markdown Lint

markdownlint 在 Markdown 文件间强制一致的风格。在 .markdownlint.json 中配置规则。常见禁用:MD013(行长度)用于散文,MD033(行内 HTML)当需要 HTML 时。MD040 提醒你为代码围栏标注语言。与 CI 和编辑器集成。

markdown
# .markdownlint.json (config)

{
  "MD013": false,
  "MD024": { "siblings_only": true },
  "MD033": false,
  "default": true
}

# Rules (examples):
# MD013 line length
# MD024 no duplicate headings
# MD033 no inline HTML
# MD040 fenced code needs language

Pandoc

Pandoc 是文档转换的'瑞士军刀'——Markdown 与 HTML、PDF、Word、LaTeX、EPUB、幻灯片互转。它支持 Markdown 扩展(引用、数学、定义列表、原始属性)。-s 标志创建独立文档。学术和出版工作流不可或缺。

markdown
# Convert Markdown to many formats:

pandoc input.md -o output.html
pandoc input.md -o output.pdf
pandoc input.md -o output.docx
pandoc input.md -o slides.pdf

# Markdown to a self-contained HTML:
pandoc input.md -s -o output.html

# With a table of contents:
pandoc input.md --toc -o output.html

# Support extensions:
pandoc -f markdown+smart input.md

在线编辑器

在线编辑器适合快速编辑和分享。StackEdit 和 Dillinger 是功能齐全的独立编辑器。HackMD/HedgeDoc 添加实时协作。GitHub 的 Web 编辑器有预览标签页。都支持标准 Markdown;GFM 功能在 GitHub 编辑器上效果最好。

markdown
Popular online Markdown editors:

- StackEdit (stackedit.io)
  Full-featured, syncs to cloud.

- Dillinger (dillinger.io)
  Clean, exports to many formats.

- HackMD / HedgeDoc
  Real-time collaborative editing.

- GitHub web editor
  Preview tab for .md files.

- Markdown Live Preview
  Lightweight, instant preview.
18

最佳实践

可读性优先

Markdown 的核心原则是作为纯文本的可读性。优先使用最可读的语法:# 标题、- 列表、**粗体**。避免过度嵌套和过多格式。如果原始源码在文本编辑器中读起来令人愉悦,你就在正确使用 Markdown。

markdown
# Good: readable as plain text
## Section
Use **bold** for emphasis, not
__bold__ (asterisks are clearer).

# Bad: hard to read raw
##Section or ***overkill***

Tip: Preview raw Markdown often.
If it reads well as text, it is good.

行长度与换行

两种有效方式:硬换行(每行一句)提供干净的 git diff 和易于编辑;软换行(每段一行)避免重排工作。每个项目选择一种并保持一致。相应地配置编辑器的换行宽度(通常 80-120 字符)。

markdown
# Hard wrap (one sentence per line):
This is sentence one.
This is sentence two.

# Soft wrap (paragraph as one line):
This is a long paragraph that
continues on without hard breaks,
relying on editor wrapping.

# Many projects prefer one sentence
# per line for clean git diffs.

一致性

Markdown 对相同结果提供多种语法(粗体 ** vs __,列表 - vs * vs +)。每个项目选择一种约定并用 markdownlint 强制执行。一致性减少读者和贡献者的认知负荷,并使自动化处理更容易。

markdown
# Pick one style and stick to it:

- Use - for lists (not * or +)
- Use ** for bold (not __)
- Use * for italic (not _)
- Use ``` for fences (not ~~~)
- Use # for headings (not ===/--)

Consistent style is more
maintainable and professional.

链接最佳实践

对长 URL 使用引用链接以保持散文可读。对短的本地引用使用行内链接。描述性链接文本改善可访问性和 SEO——避免'点击这里'。标题作为工具提示添加上下文。将引用定义分组在章节或文档末尾。

markdown
# Reference links for long URLs:
See the [docs][docs] for details.

[docs]: https://example.com/very/long/url

# Inline for short links:
Edit [index.js](index.js).

# Title for context:
[API](api.md "REST API reference")

Keep link text descriptive,
not 'click here' or 'link'.

可访问性

可访问性在 Markdown 中很重要:图片的有意义 alt 文本(装饰性图片用空 alt),描述性链接文本(不是'点击这里'),正确的标题层级(不要跳级),以及在 Markdown 不足处使用语义 HTML(kbd、abbr、cite)。屏幕阅读器依赖这些。

markdown
# Always add alt text to images:
![Diagram of the system](diagram.png)

# Empty alt for decorative images:
![](spacer.png)

# Descriptive link text:
[Read the installation guide](install.md)

# Avoid: [click here](url)
# Use heading hierarchy in order.

# Use semantic HTML when needed:
<kbd>Ctrl</kbd> + <kbd>C</kbd>

常见错误

常见陷阱:块元素(列表、引用、代码)周围缺少空行,嵌套列表缩进不一致,未闭合的代码围栏,以及混用制表符/空格。发布前始终预览。运行 markdownlint 自动捕获问题。在目标渲染器中测试。

markdown
# Missing blank line before lists:
Text
- item  # may merge with text!

# Fix:
Text

- item

# Unclosed code fence:
```
code
# (missing closing fence)

# Inconsistent list indentation:
- a
  - b (2 spaces)
   - c (3 spaces — may break)

这篇内容对您有帮助吗?

学习路径

从零开始学习

通过结构化课程从头学习这个语言。