本文记录的是如何使用markdown命名锚(names anchors),当使用命名锚时,我们可以创建文章目录,直接跳至该命名锚(比如页面中某个小节)的链接,这样使用者就无需不停地滚动页面来寻找他们需要的信息了。

其实使用markdown不能直接达到定义命名锚的目的,借助了html的功能。在定义标题时不使用markdown语法(#个数对应相应级别的HTML样式标题),而使用h1/h2等,且使用id属性命名锚,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<h2 id="overview">Overview</h2>

<h3 id="philosophy">Philosophy</h3>

Markdown is intended to be as easy-to-read and easy-to-write as is feasible.

<h3 id="html">Inline HTML</h3>

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

<h3 id="autoescape">Automatic Escaping for Special Characters</h3>

In HTML, there are two characters that demand special treatment: `<` and `&`.

<h2 id="block">Block Elements</h2>

<h3 id="p">Paragraphs and Line Breaks</h3>

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines.

<h3 id="header">Headers</h3>
Markdown supports two styles of headers, [Setext] [1] and [atx][2].

<h3 id="blockquote">Blockquotes</h3>
Markdown uses email-style `>` characters for blockquoting. If you're familiar with quoting passages of text in an email message, then you know how to create a blockquote in Markdown.

<h3 id="list">Lists</h3>
Markdown supports ordered (numbered) and unordered (bulleted) lists.

<h3 id="precode">Code Blocks</h3>
Pre-formatted code blocks are used for writing about programming or markup source code. Rather than forming normal paragraphs, the lines of a code block are interpreted literally.

<h3 id="hr">Horizontal Rules</h3>
You can produce a horizontal rule tag (`<hr />`) by placing three or more hyphens, asterisks, or underscores on a line by themselves.

这样在文章的开头就可以使用无序列表的嵌套来定义文章的章节目录,如下目录的定义:

1
2
3
4
5
6
7
8
9
10
11
*   [Overview](#overview)
* [Philosophy](#philosophy)
* [Inline HTML](#html)
* [Automatic Escaping for Special Characters](#autoescape)
* [Block Elements](#block)
* [Paragraphs and Line Breaks](#p)
* [Headers](#header)
* [Blockquotes](#blockquote)
* [Lists](#list)
* [Code Blocks](#precode)
* [Horizontal Rules](#hr)

最终效果如图:

Overview

Philosophy

Markdown is intended to be as easy-to-read and easy-to-write as is feasible.

Inline HTML

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

Automatic Escaping for Special Characters

In HTML, there are two characters that demand special treatment: < and &.

Block Elements

Paragraphs and Line Breaks

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines.

Markdown supports two styles of headers, Setext and atx.

Blockquotes

Markdown uses email-style > characters for blockquoting. If you're familiar with quoting passages of text in an email message, then you know how to create a blockquote in Markdown.

Lists

Markdown supports ordered (numbered) and unordered (bulleted) lists.

Code Blocks

Pre-formatted code blocks are used for writing about programming or markup source code. Rather than forming normal paragraphs, the lines of a code block are interpreted literally.

Horizontal Rules

You can produce a horizontal rule tag (<hr />) by placing three or more hyphens, asterisks, or underscores on a line by themselves.

参考:

Markdown: Syntax