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

问题描述 投票:396回答:11

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

我试过用

[a link](# MyTitle)

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

markdown multimarkdown
11个回答
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>

1
投票

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

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

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

在这里阅读更多:MultiMarkdown-5 Cross-references

交叉引用

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

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

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

###概述[MultiMarkdownOverview] ##

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

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

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


0
投票

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

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

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

0
投票

还有一些额外的事情需要记住,如果你想看到标题中的符号,你想导航到...

# 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:语法。如果在那里使用'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!"

645
投票

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

[Custom foo description](#foo)

# Foo

在上面的例子中,Foo头已经生成了一个名为foo的锚标签

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

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

### My Multi Word Header

更新

pandoc一起开箱即用。


96
投票

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

<a name="abcde">

之前和

</a>

在你想要“链接”的行之后。然后是降价链接,如:

[link text](#abcde)

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

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

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

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

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


66
投票

这可能是过时的线程,但在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


17
投票

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

一个完整的例子,

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

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

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

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

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

11
投票

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

可以手动指定标识符:

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

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

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


7
投票

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


4
投票

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


1
投票

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

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

我看到它被提到了

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

有效地工作,但前者可能是除了标题之外的元素或具有多个单词的标题的良好替代。

© www.soinside.com 2019 - 2024. All rights reserved.