Github Markdown同页链接

199

假设我在同一个 git hub wiki 页面上有两个点,我们称之为 place 1place 2

##Title

###Place 1

Hello, this is some text to fill in this, [here](place2), is a link to the second place.

###Place 2

Place one has the fun times of linking here, but I can also link back [here](place1).

替代方案是目录。

##Title
[ToC]
###Place 1
###Place 2

有没有办法做到这一点?注意 - 我看到了这个,所以我会认为这是相关主题。此外,那篇文章涉及文件之间的转换,而这篇文章涉及同一文件之间的转换。


1
可能是如何在Multimarkdown中创建和链接命名锚点的重复问题。 - flyx
2
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - flyx
6个回答

262

这在Github上运行:

## Title

### Place 1

Hello, this is some text to fill in this, [here](#place-2), is a link to the second place.

### Place 2

Place one has the fun times of linking here, but I can also link back [here](#place-1).

### Place's 3: other example

Place one has the fun times of linking here, but I can also link back [here](#places-3-other-example).

转换规则概要:

  • 删除标点符号
  • 删除前导空格
  • 大写字母将被转换为小写字母
  • 字母之间的空格将转换为 -

一个很好的包含大量链接和格式的示例文档是LivingSocial API设计指南


7
请注意,参考链接本身必须用小写字母编码。以上面的例子为例,如果你链接到[here](#Place-2),链接将无法正常工作。请注意,在这个例子中,标题被称为“Place 2”,链接被正确地称为[here](#place-2) - DaveL17
8
如果你有两个或更多名称相同为“Place”的标题,那么链接将被命名为“place”,“place-1”,“place-2”等。如果你同时还有一个明确的标题“Place 2”,它的链接将是“place-2-1”。 - Kevin
1
答案仍然有用,因为它适用于Gitlab Wiki。HTML方法(在Gitlab Wiki中使用锚标记)不起作用。虽然我知道问题是关于Github的。 - Nditah
似乎BitBucket不支持此功能。我使用锚点<a name="link">代替。 - рüффп
唯一的问题是,这似乎并没有被 Github 正式记录下来,因此它依赖于反向工程规则,这些规则可能会在没有通知的情况下发生变化,从而破坏所有链接。最好使用 @bcattle 的回答中提到的 <a id="anchor-id"/><a id="anchor-id">text, html or markdown</a> - Oliver

41

如果你有一堆(子)标题名称相同,也可以创建带名称的自定义锚点。要使用标题创建自定义锚点,请插入HTML标签:

<h4 id="login-optional-fields">
Optional Fields
</h4>

然后通过ID属性将其链接到:

[see above](#login-optional-fields)

同时,直接向文档添加锚标签也可以起作用:

<a id="my-anchor"></a>

2
谢谢,这个解决方案非常有效,原因只有一个。去年对GIT markdown的更改防止了使用#my heading添加标题,现在必须使用# my heading,并且在锚点中添加空格,例如(# my-heading)是无效的。 - MitchellK
这对于 .md 文件也有效。我在我的技术博客中使用 Gatsby,并希望创建一个指向页面内部某个部分的超链接。谢谢! - Ayush Mandowara
非常酷,且不依赖于 GitHub 如何将标题转换为锚点的实现细节(这可能随时会发生变化而没有通知)。它甚至可以与图片一起使用:<a id="image">![Your Image](docs/your-image.png)</a>,然后 [your image](#image) 链接到该图片!这在 GitHub 和 PyCharm IDE 的 Markdown 预览器中都可以使用。 - Oliver

7

引用自GitHub Gist - 原帖在 这里

为了创建跳转到README不同部分的锚链接(如交互式目录),首先创建一个标题:

#Real Cool Heading

该标题的锚链接是小写的标题名称,其中空格用破折号代替。您可以通过访问Github.com上的README并在悬停在标题左侧时单击出现的锚点来获取锚点名称。复制从#开始的所有内容:

#real-cool-heading

无论您想将Real Cool Heading部分链接到哪里,都可以将所需文本放在方括号中,然后跟上锚链接在括号中:

[Go to Real Cool Heading section](#real-cool-heading)

6

接受的答案对我没用,因为我的标题也是一个链接:

之前(无效):

Table of contents 

 1. [Header Name](#header-name)


### [Header Name](https://google.com)

之后(适用于我):

Table of contents 

 1. [Header Name](#header-name)


### Header Name

https://google.com

当您不想使用HTML并希望采用一些权衡解决方案时,可以查看readme。


1

很不幸,GitHub维基似乎会从您添加到维基页面的自定义HTML中剥离所有“id = ..”标签,因此页面内唯一有效的锚点是标题。


1
示例1:

##Title

###Place 1<span id="place1">HelloWorld</span>

Hello, this is some text to fill in this, [here](#place2), is a link to the second place.

###Place 2<span id="place2">HelloWorld</span>

Place one has the fun times of linking here, but I can also link back [here](#place1).

这里有一个版本,可以从地点1跳到地点2,也可以从地点2跳回地点1。

##Title

###[Place 1](#Place-2)<span id="place1">HelloWorld</span>

Hello, this is some text to fill in this, [here](#place2), is a link to the second 
place.

###Place 2(#Place-1)<span id="place2">HelloWorld</span>

Place one has the fun times of linking here, but I can also link back [here](#place1).

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接