如何链接到Markdown中同一文档的一部分?

时间:2010-05-12 19:24:52

标签: markdown multimarkdown

我正在撰写一份大型Markdown文档,并希望在开头放置一个类别的目录,以提供指向文档中各个位置的链接。我怎么能这样做?

我尝试使用

[a link](# MyTitle)

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

14 个答案:

答案 0 :(得分:622)

Github会自动从标题中解析锚标记。所以你可以做到以下几点:

[Custom foo description](#foo)

# Foo

在上述情况下,Foo标头生成了一个名为foo

的锚标记

注意:所有标题大小只有一个##和锚名称之间没有空格,锚标记名称必须小写,并且如果是多个,则用短划线分隔字。

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

### My Multi Word Header

更新

也开箱即用pandoc

答案 1 :(得分:93)

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

<a name="abcde">
在和

之前

</a>
之后你想要“链接”的行。然后是降价链接,如:

[link text](#abcde)

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

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

(PS:将锚点放在你想要链接的行中可能没问题,如下所示:

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

但这取决于Markdown如何对待这一点。我注意到,例如,Stack Overflow应答格式化器对此感到满意!)

答案 2 :(得分:65)

这可能是过时的线程,但是要在Github中使用降价来创建内部文档链接...
(注意:小写#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

答案 3 :(得分:36)

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>

答案 4 :(得分:17)

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

一个完整的例子,

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

稍后在文档中,您将创建命名锚(无论它被调用)。

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

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

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

答案 5 :(得分:10)

通用解决方案

根据减价实施情况,这个问题似乎有不同的答案。
实际上,Markdown官方文档对此主题保持沉默。
在这种情况下,如果您需要便携式解决方案,则可以使用HTML。

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

完整示例:

查看完整示例(在线和可编辑)here

## 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名称中使用空格。

答案 6 :(得分:9)

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

可以手动指定标识符:

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

或者您可以使用自动生成的标识符。两者都在pandoc manual中详细解释。

注意:转换为 HTML LaTex ConTeXt 仅有效>,纺织品 AsciiDoc

答案 7 :(得分:7)

Markdown规范中没有这样的指令。遗憾。

答案 8 :(得分:3)

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)

也可以手动执行:用连字符替换空格。

实例是here

答案 9 :(得分:1)

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

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

我看到它已被提及

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

有效地工作,但前者可能是除了标题之外的元素的一个很好的替代方案,或者是包含多个单词的标题。

答案 10 :(得分:1)

由于MultiMarkdown在评论中被提及作为选项。

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

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

在此处阅读更多内容:MultiMarkdown-5 Cross-references

  

交叉引用

     

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

     

例如,[元数据] []将带您到#元数据(或任何##元数据,###元数据,####元数据,#####元数据,######元数据)。

     

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

     

###概述[MultiMarkdownOverview] ##

     

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

     

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

     

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

答案 11 :(得分:1)

如果您想获得花式且标题中包含您想导航到的符号...

,请记住一些其他事项。

# 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

...标题字符串中的诸如#;&:之类的东西通常被忽略/分割而不是转义,并且也可以使用 citation 样式链接使快速使用变得容易。

  

注释

     

GitHub在提交,自述文件等中支持 :word: 语法。如果在那里感兴趣使用'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文件要么需要针对一种笨拙的方式,要么需要针对各种 clever 方式添加配置和脚本逻辑喜欢清除标题的文本。

答案 12 :(得分:0)

\d\.\s+技巧上还有一些旋转:

>>> import re
>>> re.findall(r'\d+\.\s+(.*?(?=\n\d|$))', text, flags=re.DOTALL)
['robert\n   smith', 'harry', 'john']

<a name="">

答案 13 :(得分:0)

除了上述答案,

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

number_sections: TRUE

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

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

[My Section]

My Section是节的名称

这似乎不管部分级别如何都有效:

# My section

## My section

### My section