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

我试着用:

[a link](# MyTitle)

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


当前回答

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

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

我看到有人提到过

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

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

其他回答

在Markdown规范中没有这样的指令,抱歉。

在pandoc中,如果在生成html时使用——toc选项,将生成一个包含到章节的链接的目录,并从章节标题返回到目录。它类似于pandoc编写的其他格式,如LaTeX、rtf、rst等。对于命令

pandoc --toc happiness.txt -o happiness.html

这里有一点降价:

% True Happiness

Introduction
------------

Many have posed the question of true happiness.  In this blog post we propose to
solve it.

First Attempts
--------------

The earliest attempts at attaining true happiness of course aimed at pleasure. 
Soon, though, the downside of pleasure was revealed.

将产生这个作为html的主体:

<h1 class="title">
    True Happiness
</h1>
<div id="TOC">
    <ul>
        <li>
            <a href="#introduction">Introduction</a>
        </li>
        <li>
            <a href="#first-attempts">First Attempts</a>
        </li>
    </ul>
</div>
<div id="introduction">
    <h2>
        <a href="#TOC">Introduction</a>
    </h2>
    <p>
        Many have posed the question of true happiness. In this blog post we propose to solve it.
    </p>
</div>
<div id="first-attempts">
    <h2>
        <a href="#TOC">First Attempts</a>
    </h2>
    <p>
        The earliest attempts at attaining true happiness of course aimed at pleasure. Soon, though, the downside of pleasure was revealed.
    </p>
</div>

Github会自动从你的标题中解析锚标记。所以你可以这样做:

[Custom foo description](#foo)

# Foo

在上面的例子中,Foo头生成了一个名为Foo的锚标记

注意:所有标题大小只有一个#,#和锚点名称之间没有空格,锚点标签名称必须小写,如果有多个单词,则用破折号分隔。

[click on this link](#my-multi-word-header)

### My Multi Word Header

更新

潘多克也不例外。

是的,markdown确实做到了这一点,但你需要指定名称anchor <a name='xyx'>。

一个完整的例子,

这就创建了链接 (任务)(#任务)

在文档的其他地方,您创建了命名锚(不管它叫什么)。

<a name="tasks">
   my tasks
</a>

注意,你也可以把它环绕在标题上。

<a name="tasks">
### Agile tasks (created by developer)
</a>

试验,我发现了一个解决方案使用<div…/>,但一个明显的解决方案是在页面中放置自己的锚点,无论你喜欢,因此:

<a name="abcde">

之前和

</a>

在你想要“链接”到的行之后。然后是一个降价链接:

[link text](#abcde)

文档中的任何地方都可以。

<div…/>解决方案插入一个“假”除法只是为了添加id属性,这可能会破坏页面结构,但<a name="abcde"/>解决方案应该是非常无害的。

(PS:把锚点放在你想链接的行中也可以,如下所示:

## <a name="head1">Heading One</a>

但这取决于Markdown如何处理。我注意到,例如,Stack Overflow答案格式化器很高兴这样!)