我正在编写一个大型Markdown文档,并希望在开始时放置一个目录,将提供到文档中各个位置的链接。我该怎么做呢?

我试着用:

[a link](# MyTitle)

其中MyTitle是文档中的一个标题,但这不起作用。


当前回答

关于<a name="">技巧的更多旋转:

<a id="a-link"></a> Title
------

#### <a id="a-link"></a> Title (when you wanna control the h{N} with #'s)

其他回答

通用解决方案

根据markdown的实现,这个问题似乎有不同的答案。 事实上,官方Markdown文档对这个话题只字未提。 在这种情况下,如果您想要一个可移植的解决方案,您可以使用HTML。

在任何标题之前,或在同一标题行中,使用一些HTML标记定义一个ID。 例如:<a id="Chapter1"></a> . 您将在代码中看到这一点,但在呈现的文档中看不到。

完整的例子:

请在这里查看完整的示例(在线和可编辑)。

## Content

* [Chapter 1](#Chapter1)
* [Chapter 2](#Chapter2)

<div id="Chapter1"></div>
## Chapter 1

Some text here.  
Some text here.
Some text here.

## Chapter 2 <span id="Chapter2"><span>

Some text here.  
Some text here.
Some text here.

为了测试这个例子,您必须在内容列表和第一章之间增加一些额外的空间,或者降低窗口的高度。 另外,不要在id的名称中使用空格。

使用kramdown,看起来效果不错:

[I want this to link to foo](#foo)
....
....
{: id="foo"}
### Foo are you?

我看到有人提到过

[foo][#foo]
....
#Foo

工作效率很高,但对于除标题外的元素或具有多个单词的其他标题,前者可能是一个很好的选择。

pandoc手册解释了如何使用头文件的标识符链接到头文件。我没有检查其他解析器对此的支持,但据报道,它不能在github上工作。

标识符可以手动指定:

## my heading text {#mht}

Some normal text here,
including a [link to the header](#mht).

或者您可以使用自动生成的标识符(在本例中是#my-heading-text)。在pandoc手册中都有详细的解释。

注意:这只适用于转换为HTML, LaTex, ConTeXt, Textile或AsciiDoc。

因为在评论中提到了MultiMarkdown作为一个选项。

在MultiMarkdown中,内部链接的语法很简单。

对于文档中的任何标题,只需以这种格式给出标题名称[heading][]以创建内部链接。

阅读更多:MultiMarkdown-5交叉引用。

Cross-References An oft-requested feature was the ability to have Markdown automatically handle within-document links as easily as it handled external links. To this aim, I added the ability to interpret [Some Text][] as a cross-link, if a header named “Some Text” exists. As an example, [Metadata][] will take you to # Metadata (or any of ## Metadata, ### Metadata, #### Metadata, ##### Metadata, ###### Metadata). Alternatively, you can include an optional label of your choosing to help disambiguate cases where multiple headers have the same title: ### Overview [MultiMarkdownOverview] ## This allows you to use [MultiMarkdownOverview] to refer to this section specifically, and not another section named Overview. This works with atx- or settext-style headers. If you have already defined an anchor using the same id that is used by a header, then the defined anchor takes precedence. In addition to headers within the document, you can provide labels for images and tables which can then be used for cross-references as well.

在我的情况下,我正在寻找一个没有Pandoc的TOC解决方案。每个TOC条目都包含一个指向标题的链接,格式为[显示名称](#-url- formatting - Name -of-header)

对于2个缩进级别的简单情况,

1. [Installation](#1-installation)  
1.1. [Minimum System Requirements](#11-minimum-system-requirements)  
1.2. [Prerequisites](#12-prerequisites)  

结果:

安装 1.1. 系统最低要求 1.2. 先决条件

对于包含3个或更多缩进级别的一般多级编号列表,列表无法在第3级或更高级别进一步缩进(例如1.3.2.)。相反,我能找到的最好的解决方案是使用>>>来格式化嵌套的blockquotes。

## Table of Contents  
>1. [Installation](#1-installation)  
>>1.1. [Minimum System Requirements](#11-minimum-system-requirements)  
>>1.2. [Prerequisites](#12-prerequisites)  
>>>1.2.1. [Preparation of Database Server](#121-preparation-of-database-server)  
>>>1.2.2. [Preparation of Other Servers](#122-preparation-of-other-servers)  
>>  
>>1.3. [Installing – Single Server](#13-installing-single-server)  
>>1.4. [Installing – Multi Server](#14-installing-multi-server)  
>>>1.4.1. [Database Server](#141-database-server)  
>>>...  

结果在GitHub上很好地渲染TOC。不能渲染它在这里没有SO的linter抱怨未格式化的代码。

注意1.2.2之后的空白条目。 如果没有空白条目,下面的行仍然停留在第3个blockquote缩进级别。

相比之下,项目列表只使用空格或制表符作为缩进标记

## Table of Contents  
- [Installation](#1-installation)  
  - [Minimum System Requirements](#11-minimum-system-requirements)  
  - [Prerequisites](#12-prerequisites)  
    - [Preparation of Database Server](#121-preparation-of-database-server)  
    - [Preparation of Other Servers](#122-preparation-of-other-servers)  
  - [Installing – Single Server](#13-installing-single-server)  
  - [Installing – Multi Server](#-installing-multi-server)  
    - [Database Server](#141-database-server)  
    - ...  

结果:

目录

安装 系统最低要求 先决条件 数据库服务器准备 其他服务器准备 安装—单机 安装-多服务器 数据库服务器 ...

所有上述缩进列表将成功链接到以下标题在GitHub markdown(标题无法链接在so风味markdown出于某种原因)-

# 1. Installation  
## 1.1. Minimum System Requirements  
## 1.2. Prerequisites  
### 1.2.1. Preparation of Database Server  
### 1.2.2. Preparation of Other Servers  
## 1.3. Installing – Single Server  
## 1.4. Installing – Multi Server   
### 1.4.1. Database Server