833

我正在编写一个大型 Markdown 文档,并希望在开头放置一个目录,该目录将提供指向文档中各个位置的链接。我怎样才能做到这一点?

我尝试使用:

[a link](# MyTitle)

文档中的标题在哪里,MyTitle但这不起作用。

4

14 回答 14

1460

Github 会自动从您的标头中解析锚标记。因此,您可以执行以下操作:

[Custom foo description](#foo)

# Foo

在上面的例子中,Fooheader 已经生成了一个带有名字的锚标签foo

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

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

### My Multi Word Header

更新

开箱即用pandoc

于 2013-05-07T19:17:20.763 回答
151

这可能是过时的线程,但要在 Github 中使用 markdown 创建内部文档链接...
(注意:小写 #title)

# Contents
 - [Specification](#specification) 
 - [Dependencies Title](#dependencies-title) 

## Specification
Example text blah. Example text blah. Example text blah. Example text blah. 
Example text blah. Example text blah. Example text blah. Example text blah. 
Example text blah. Example text blah. Example text blah. Example text blah. 
Example text blah. Example text blah. 

## Dependencies Title
Example text blah. Example text blah. Example text blah. Example text blah. 
Example text blah. Example text blah. Example text blah. Example text blah. 
Example text blah. Example text blah. Example text blah. Example text blah. 
Example text blah. Example text blah. 

提出了一个很好的问题,所以我编辑了我的答案;

可以使用 - #, ##,对任何标题大小进行内部链接####### 我在下面创建了一个快速示例... https://github.com/aogilvie/markdownLinkTest

于 2013-04-09T04:13:45.487 回答
143

通过实验,我找到了一个解决方案,<div…/>但一个明显的解决方案是将您自己的锚点放置在页面中您喜欢的任何位置,因此:

<a name="abcde">

之前

</a>

您要“链接”到的行之后。然后是一个降价链接,如:

[link text](#abcde)

文档中的任何地方都会将您带到那里。

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

(PS:可以把anchor放在你想链接的那一行,如下:

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

但这取决于 Markdown 如何处理这个问题。例如,我注意到 Stack Overflow 答案格式化程序对此很满意!)

于 2011-06-27T15:04:25.307 回答
37

是的,markdown 会这样做,但您需要指定名称 anchor <a name='xyx'>

一个完整的例子,

这将创建链接
[tasks](#tasks)

在文档的其他地方,您创建命名的锚点(无论它叫什么)。

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

请注意,您也可以将其包裹在标题周围。

<a name="tasks">
### Agile tasks (created by developer)
</a>
于 2015-06-11T19:13:41.833 回答
30

pandoc中,如果您--toc在生成 html 中使用该选项,则会生成一个目录,其中包含指向各节的链接,并从节标题返回目录。它与 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>
于 2010-05-13T01:33:15.287 回答
23

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

可以手动指定标识符:

## my heading text {#mht}

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

或者您可以使用自动生成的标识符(在这种情况下#my-heading-text)。两者都在pandoc 手册中详细解释。

注意:这在转换为HTMLLaTexConTeXtTextileAsciiDoc 时有效

于 2014-05-19T09:11:46.403 回答
15

通用解决方案

这个问题似乎根据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 名称中使用空格。

于 2020-03-03T15:40:41.230 回答
11

如果您对要导航到的标题中的符号感兴趣,请记住一些其他事项...

# What this is about


------


#### Table of Contents


- [About](#what-this-is-about)

- [&#9889; Sunopsis](#9889-tldr)

- [:gear: Grinders](#it-grinds-my-gears)

- [Attribution]


------


## &#9889; TLDR


Words for those short on time or attention.


___


## It Grinds my :gear:s


Here _`:gear:`_ is not something like &#9881; or &#9965;


___


## &#9956; Attribution


Probably to much time at a keyboard



[Attribution]: #9956-attribution

... #, ;, &, 和:标题字符串内的内容通常被忽略/条纹而不是转义,并且还可以使用引用样式链接以使快速使用更容易。

笔记

GitHub 支持:word:提交、自述文件等中的语法。如果对 using'em 感兴趣,请参阅gist(来自 rxaviers)。

对于现代浏览器几乎可以使用十进制或十六进制的其他任何地方;w3schools的备忘单很方便,特别是如果使用 CSS::before::after带有符号的伪元素更符合您的风格。

奖励积分?

以防万一有人想知道标题中的图像和其他链接如何被解析为id...

- [Imaged](#alt-textbadge__examplehttpsexamplecom-to-somewhere)


## [![Alt Text][badge__example]](https://example.com) To Somewhere


[badge__example]:
  https://img.shields.io/badge/Left-Right-success.svg?labelColor=brown&logo=stackexchange
  "Eeak a mouse!"

注意事项

MarkDown 渲染因地而异,所以像......

## methodName([options]) => <code>Promise</code>

...在 GitHub 上将有一个元素,id例如...

id="methodnameoptions--promise"

...香草卫生会导致id...

id="methodnameoptions-codepromisecode"

...意味着从模板编写或编译 MarkDown 文件要么需要针对一种slugifeing方式,要么需要为清理标题文本等各种巧妙方式添加配置和脚本逻辑。

于 2019-06-28T00:17:51.043 回答
9

Markdown 规范中没有这样的指令。对不起。

于 2010-05-12T21:25:15.463 回答
5

Gitlab 使用GitLab Flavored Markdown (GFM)

这里“所有 Markdown 渲染的标题都会自动获取 ID”

可以使用鼠标:

  • 将鼠标移到标题上
  • 将鼠标移到从标题左侧可见的悬停选择器上
  • 使用鼠标右键复制和保存链接

    例如在 README.md 文件中,我有标题:

## series expansion formula of the Boettcher function

这给出了一个链接:

https://gitlab.com/adammajewski/parameter_external_angle/blob/master/README.md#series-expansion-formula-of-the-boettcher-function

前缀可以去掉,所以这里的链接很简单

file#header

这意味着:

README.md#series-expansion-formula-of-the-boettcher-function

现在它可以用作:

[series expansion formula of the Boettcher function](README.md#series-expansion-formula-of-the-boettcher-function)

也可以手动完成:用连字符替换空格。

现场示例在这里

于 2017-11-23T17:36:10.680 回答
2

除了以上回答,

number_sections: true在 YAML 标头中设置选项时:

number_sections: TRUE

RMarkdown 将为您的部分自动编号。

要引用这些自动编号的部分,只需将以下内容放入您的 R Markdown 文件中:

[My Section]

My Section分区名称在哪里

无论部分级别如何,这似乎都有效:

# My section

## My section

### My section

于 2019-10-24T10:13:52.447 回答
1

使用 kramdown,看起来效果很好:

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

我看到有人提到过

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

有效地工作,但前者可能是除了标题或包含多个单词的标题之外的元素的不错选择。

于 2016-02-08T23:31:15.483 回答
1

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

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

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

在此处阅读更多信息:MultiMarkdown-5 交叉引用

交叉引用

一个经常被要求的功能是能够让 Markdown 自动处理文档内链接,就像它处理外部链接一样容易。为此,如果存在名为“Some Text”的标题,我添加了将 [Some Text][] 解释为交叉链接的功能。

例如,[元数据][] 将带您到 # 元数据(或 ## 元数据、### 元数据、#### 元数据、##### 元数据、###### 元数据中的任何一个)。

或者,您可以包含您选择的可选标签,以帮助消除多个标题具有相同标题的情况:

### 概述 [MultiMarkdownOverview] ##

这允许您使用 [MultiMarkdownOverview] 来专门引用此部分,而不是另一个名为 Overview 的部分。这适用于 atx 或 settext 样式的标题。

如果您已经使用与标头相同的 id 定义了锚点,则定义的锚点优先。

除了文档中的标题之外,您还可以为图像和表格提供标签,然后也可以将其用于交叉引用。

于 2016-09-04T08:34:25.323 回答
0

<a name="">这个技巧还有一些旋转:

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

#### <a id="a-link"></a> Title (when you wanna control the h{N} with #'s)
于 2018-07-31T12:39:34.203 回答