我正在编写一个大型 Markdown 文档,并希望在开头放置一个目录,该目录将提供指向文档中各个位置的链接。我怎样才能做到这一点?
我尝试使用:
[a link](# MyTitle)
其中 MyTitle
是文档中的标题,但这不起作用。
Github 会自动从您的标头中解析锚标记。因此,您可以执行以下操作:
[Custom foo description](#foo)
# Foo
在上述情况下,Foo
标头生成了一个名为 foo
的锚标记
注意:所有标题大小只有一个 #
,#
和锚名称之间没有空格,锚标签名称必须小写,如果是多字则用破折号分隔。
[click on this link](#my-multi-word-header)
### My Multi Word Header
更新
也可以与 pandoc
一起使用。
这可能是过时的线程,但要在 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
(#dependencies-title)
中的 # 告诉 Github markdown 这是一个内部链接。后面的文本可以是任何标题大小。这是我做的一个示例测试...https://github.com/aogilvie/markdownLinkTest
通过实验,我找到了使用 <div…/>
的解决方案,但一个明显的解决方案是将您自己的锚点放置在页面中您喜欢的任何位置,因此:
<a name="abcde">
之前和
</a>
在您要“链接”到的行之后。然后是一个降价链接,如:
[link text](#abcde)
文档中的任何地方都会将您带到那里。
<div…/>
解决方案插入一个“虚拟”除法只是为了添加 id
属性,这可能会破坏页面结构,但 <a name="abcde"/>
解决方案应该是无害的。
(PS:可以把anchor放在你想链接的那一行,如下:
## <a name="head1">Heading One</a>
但这取决于 Markdown 如何处理这个问题。例如,我注意到 Stack Overflow 答案格式化程序对此很满意!)
## headers
。
<div/>
的文本之前,下面的几行都会受到影响。相反,我必须将要链接的文本包装在完整的 div
标记子句中,并且必须使用真正的 HTML 从头开始重新指定行为。嘘。
是的,markdown 会这样做,但您需要指定名称锚 <a name='xyx'>
。
一个完整的例子,
这将创建链接
[tasks](#tasks)
在文档的其他地方,您创建命名的锚点(无论它叫什么)。
<a name="tasks">
my tasks
</a>
请注意,您也可以将其包裹在标题周围。
<a name="tasks">
### Agile tasks (created by developer)
</a>
在 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>
git clone
-ed 到最低或最外层的 tmbundle 目录中,~/Library/Application\ Support/TextMate/Bundles
以简化集成。
-1
添加到 id 的第一个重复,将 -2
添加到第二个,等等。
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 时有效。
通用解决方案
这个问题似乎根据markdown实现有不同的答案。事实上,官方的 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 名称中使用空格。
## Chapter 1
需要在其上方有一条空线。 (2)。链接失效了...
<span id="Chapter1"><span>
<div...
和部分 ##
之间使用空行
如果您对要导航到的标题中的符号感兴趣,请记住一些其他事项...
# What this is about
------
#### Table of Contents
- [About](#what-this-is-about)
- [⚡ Sunopsis](#9889-tldr)
- [:gear: Grinders](#it-grinds-my-gears)
- [Attribution]
------
## ⚡ TLDR
Words for those short on time or attention.
___
## It Grinds my :gear:s
Here _`:gear:`_ is not something like ⚙ or ⛭
___
## ⛤ Attribution
Probably to much time at a keyboard
[Attribution]: #9956-attribution
...标题字符串中的 #
、;
、&
和 :
之类的内容通常被忽略/条带化而不是转义,并且还可以使用 citation 样式链接到使快速使用更容易。
备注 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"
...其中 vanilla 卫生会导致...的id
id="methodnameoptions-codepromisecode"
...这意味着从模板编写或编译 MarkDown 文件要么需要针对一种 slugifeing 方式,要么需要为各种巧妙的方式添加配置和脚本逻辑,例如清理标题的文本。
Markdown 规范中没有这样的指令。对不起。
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
[series expansion formula of the Boettcher function](#series-expansion-formula-of-the-boettcher-function)
应该可以解决问题。
除了以上回答,
在 YAML 标头中设置选项 number_sections: true
时:
number_sections: TRUE
RMarkdown 将为您的部分自动编号。
要引用这些自动编号的部分,只需将以下内容放入您的 R Markdown 文件中:
[My Section]
其中 My Section
是部分的名称
无论部分级别如何,这似乎都有效:
# My section
## My section
### My section
使用 kramdown,看起来效果很好:
[I want this to link to foo](#foo)
....
....
{: id="foo"}
### Foo are you?
我看到有人提到过
[foo][#foo]
....
#Foo
有效地工作,但前者可能是除了标题或包含多个单词的标题之外的元素的不错选择。
由于在评论中提到了 MultiMarkdown 作为一个选项。
在 MultiMarkdown 中,内部链接的语法很简单。
对于文档中的任何标题,只需以 [heading][]
格式提供标题名称即可创建内部链接。
在此处阅读更多信息:MultiMarkdown-5 Cross-references。
交叉引用 一个经常被要求的功能是能够让 Markdown 自动处理文档内链接,就像它处理外部链接一样容易。为此,如果存在名为“Some Text”的标题,我添加了将 [Some Text][] 解释为交叉链接的功能。例如,[元数据][] 将带您到 # 元数据(或 ## 元数据、### 元数据、#### 元数据、##### 元数据、###### 元数据中的任何一个)。或者,您可以包含您选择的可选标签,以帮助消除多个标题具有相同标题的情况:### Overview [MultiMarkdownOverview] ## 这允许您使用 [MultiMarkdownOverview] 来专门引用此部分,而不是另一个名为概述的部分。这适用于 atx 或 settext 样式的标题。如果您已经使用与标头相同的 id 定义了锚点,则定义的锚点优先。除了文档中的标题之外,您还可以为图像和表格提供标签,然后也可以将其用于交叉引用。
<a>
应该适用于任何 Markdown 文档。此外,您可以在文档正文中的任何位置放置锚点,而不仅仅是在标题、图像或表格中。
更多关于 <a name="">
技巧的旋转:
<a id="a-link"></a> Title
------
#### <a id="a-link"></a> Title (when you wanna control the h{N} with #'s)
只需遵循 [text](#link)
语法并遵循以下准则:
按原样写下字母和数字
用破折号替换空格 -
删除其余字符
因此,例如,如果您有以下部分:
# 1. Python
# 2. c++
# 3. c++11
# 4. asp.net-core
您可以使用以下方法添加参考:
[1. Python](#1-python)
[2. c++](#2-c)
[3. c++11](#3-c11)
[4. asp.net-core](#4-aspnet-core)
请注意 asp.net-core
如何变为 aspnet-core
,1. python
如何变为 1-python
,等等。
[just](#like-this-one)
中用连字符替换空格。## Foo
创建一个链接,请尝试 [this is my link to Foo](#foo) ...即:single 哈希,之间没有空格哈希和 lowercase-kebab-case-name-of-header