Markdown 语法 – 基础教程

Markdown’s syntax is intended for one purpose: to be used as a format for writing for the web.

John Gruber

约翰-格鲁伯(John Gruber)是Markdown的作者,他设计 Markdown 格式化语法的目的是使其尽可能具有可读性。Markdown 是一种简单的标记语言,可以格式化纯文本。它是一种为网络编写内容的方法。Markdown 语言简单易学;它的额外字符极少,因此您可以快速书写并减少错误。

基本 Markdown 语法

本文介绍 Markdown 的主要元素、细节和示例。所有 Markdown 语法的完整列表已在 core Markdown 规范GitLab Flavored Markdown 变体中声明。Aspose.HTML 开箱即支持所有这些功能。

页眉 - Headers

Markdown 支持两种标题样式: Setextatx:要创建 atx 样式的标题,请在行首添加符号 (#)。符号的个数应与标题的级别相对应。 注意: 数字符号 (#) 和标题名称之间一定要有空格。

或者,要创建一个 setext 样式的标题,可在文本下面一行添加任意数量的 == 字符(标题级别 1)或 -- 字符(标题级别 2)。

Markdown SyntaxHTMLRendered
# This is a Heading level 1<h1>This is a Heading level 1</h1>

This is a Heading level 1

## This is a Heading level 2<h2>This is a Heading level 2</h2>

This is a Heading level 2

### This is a Heading level 3<h3>This is a Heading level 3</h3>

This is a Heading level 3

###### This is a Heading level 6<h6>This is a Heading level 6</h6>
This is a Heading level 6
This is a Heading level 1
===================
<h1>This is a Heading level 1</h1>

This is a Heading level 1

This is a Heading level 2
- - - - - - - - - - - - - - - - - - -
<h2>This is a Heading level 2</h2>

This is a Heading level 2

强调 - Emphasis

Markdown 将星号 * 和下划线 (_) 视为强调符号。要使文本同时加粗和斜体,请在单词或短语前后添加三个星号或下划线。要使单词中间加粗并斜体以示强调,可在字母周围添加三个星号,但不留空格。

Markdown SyntaxHTMLRendered
*single asterisks*<em>single asterisks</em>single asterisks
_single underscores_<em>single underscores</em>single underscores
**double asterisks**<strong>double asterisks</strong>double asterisks
__ double underscores__<strong>double underscores</strong>double underscores
***Bold and Italic***<strong><em>Bold and Italic</em></strong>Bold and Italic
___Bold and Italic___<strong><em>Bold and Italic</em></strong>Bold and Italic
Make text bold***and***italic at the same timeMake text bold<strong><em>and</em></strong>italic at the same timeMake text boldanditalic at the same time

小标题 - Blockquotes

Markdown 语言使用电子邮件风格的 > 字符来表示 blockquote。因此,在创建 blockquote 时,应在硬包段落的第一行前加上符号 >。如果一个块引号包含多个段落,则应在段落之间的空行上添加>。此外,块引号还可以包含其他 Markdown 元素,包括标题、强调、列表或代码片段:让我们来看一些使用示例:

Markdown SyntaxRendered
> Markdown is intended to be as easy-to-read and easy-to-write as is feasible.
alt text
> ## This is a header.
>
> 1. This is the first list item.
> 2. This is the second list item.
>
> Here’s some example code:
>
> using var document = new HTMLDocument();
alt text

Markdown 代码块

代码 - Code

要表示文章文本中的代码跨度,请用反斜杠引号 (`) 将其括起来:

Markdown SyntaxHTMLRendered
Load the HTML file using `HTMLDocument` classLoad the HTML file using <code>HTMLDocument</code> classLoad the HTML file using HTMLDocumentclass
`Use code in your Markdown file.`<code>Use code in your Markdown file.</code>Use code in your Markdown file.

代码块 - Code Blocks

要在 Markdown 中创建代码块,代码块的每一行至少要缩进 4 个空格或 1 个制表符。在本例中,前导空格用句号 (⋅) 表示:

Markdown SyntaxHTMLRendered
This is a normal paragraph.<p>This is a normal paragraph.</p>

This is a normal paragraph.

⋅⋅⋅⋅This is a Markdown code block.<pre>This is a Markdown code block.</pre>
This is a Markdown code block.

列表 - Lists

无序列表

Markdown 支持有序(编号)和无序(列表)列表。无序列表可交替使用星号、加号和连字符作为列表标记。以 *+- 开头。要创建嵌套列表,请缩进一个或多个项目:

Markdown Syntax
HTML
Rendered
- First item
- Second item
- Third item
- Fourth item
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
</ul>
  • First item
  • Second item
  • Third item
  • Fourth item
* First item
* Second item
* Blue
* Black
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
</ul>
  • First itemd
  • Second item
  • Third item
  • Fourth item
+ First item
+ Second item
+ Third item
+ Fourth item
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
</ul>
  • First itemd
  • Second item
  • Third item
  • Fourth item

有序列表

要创建有序列表,请添加带有数字和句点的行项目。列表应从数字 1 开始。这样,用于标记列表的其他数字将不会影响 Markdown 生成的 HTML 输出。

Markdown Syntax
HTML
Rendered
1. Red
1. Green
1. Blue
1. Black
<ol>
<li>Red</li>
<li>Green</li>
<li>Blue</li>
<li>Black</li>
</ol>
  1. Red
  2. Green
  3. Blue
  4. Black
1. Red
2. Green
3. Blue
4. Black
<ol>
<li>Red</li>
<li>Green</li>
<li>Blue</li>
<li>Black</li>
</ol>
  1. Red
  2. Green
  3. Blue
  4. Black
1. Red
5. Green
3. Blue
8. Black
<ol>
<li>Red</li>
<li>Green</li>
<li>Blue</li>
<li>Black</li>
</ol>
  1. Red
  2. Green
  3. Blue
  4. Black

图片 - Images

内联图像 Markdown 语法如下:

![Alt text for image](/path/to/img.png)
Example:
![Text "Ananuri Fortress Complex in Georgia"](georgia-castle.png)

您应该添加一个感叹号(!),然后在方括号中添加 alt 文本,并在括号中添加图片的 URL 或路径。其中,“图片的备选文本 “是对图片的简要描述,"/path/to/img.png “是图片的相对路径。如果图像无法渲染,备用文本将非常有用。示例 georgia-castle.png 的渲染输出如下:

文本 “格鲁吉亚阿纳努里要塞建筑群”

扩展 Markdown 语法

要创建链接,请在方括号中写入链接文本,然后在括号中写入 URL。链接语法如下

使用 [Markdown Converter](https://products.aspose.app/html/conversion/md) 将 Markdown 文件转换为各种流行格式。

呈现的链接文本如下所示:

使用 Markdown Converter 将 Markdown 文件转换为各种流行格式。

URL 和电子邮件地址

要快速将 URL 或电子邮件地址转化为链接,请将其写入角括号中:

1<https://docs.aspose.com/html/net/converting-between-formats/>
2<html@example.com>

URL 和电子邮件地址的呈现链接如下所示:

https://docs.aspose.com/html/net/converting-between-formats/

html@example.com

图像作为链接

要为图片添加链接,请将图片的 Markdown 代码用括号括起来,然后在括号中添加链接。将图片作为链接的 Markdown 语法如下:

1 [![文本 "MD 到 PDF 转换器"](/html/images/banner/md-to-pdf.png)](https://products.aspose.app/html/zh/conversion/md-to-pdf)

作为链接的渲染图像看起来是这样的:

文本 “MD 到 PDF 转换器”

Markdown 表格语法

表格通常是呈现某些数据的最佳方式,但在 Markdown 中格式化表格却是最棘手的问题之一。 Markdown 中的表格由两部分组成:表头和表格中的数据行。表格是用管道(|)和连字符(-)创建的。使用三个或更多连字符(- - -)创建每列的标题,并使用管道(|)分隔每列。 冒号(:)用于对齐单元格内容。连字符左边的冒号会导致列左对齐等。

下面是一个 Markdown 表格语法示例:

1| Header 1       | Header 2           | Header 3       |
2| :------------- | :----------------: | -------------: |
3| Cell Content 1 | Cell Content 2     | Cell Content 3 |
4| Text           | Some long data here| Data           |

The rendered table looks like this:

Header 1Header 2Header 3
Cell Content 1Cell Content 2Cell Content 3
TextSome long data hereData

通过 GitLab Flavored Markdown,您可以创建简单易读的表格。您可以轻松地为表格添加 Markdown 语法,如粗体、斜体、代码块、HTML 标记等。

Aspose.HTML for .NET 库为 C# 平台提供了 Markdown 解析 API。您可以通过编程创建、编辑、保存、合并、将 MD 文件转换为其他格式,并在 Markdown 文件中添加链接、列表、代码块、图像和其他元素。

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.