可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am writing a large Markdown document and would like to place a table of contents of sorts at the beginning that will provide links to various locations in the document. How can I do this?
I tried using
[a link](# MyTitle)
where MyTitle
is a title within the document and this didn't work.
回答1:
In pandoc, if you use the option --toc
in producing html, a table of contents will be produced with links to the sections, and back to the table of contents from the section headings. It is similar with the other formats pandoc writes, like LaTeX, rtf, rst, etc. So with the command
pandoc --toc happiness.txt -o happiness.html
this bit of markdown:
% 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.
will yield this as the body of the 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>
回答2:
Github automatically parses anchor tags out of your headers. So you can do the following:
[Foo](#foo)
# Foo
In the above case, the Foo
header has generated an anchor tag with the name foo
Note: just one #
for all heading sizes, no space between #
and anchor name, anchor tag names must be lowercase, and delimited by dashes if multi-word.
[click on this link](#my-multi-word-header)
### My Multi Word Header
Update
Works out of the box with pandoc
too.
回答3:
Experimenting, I found a solution using <div…/>
but an obvious solution is to place your own anchor point in the page wherever you like, thus:
<a name="abcde">
before and
</a>
after the line you want to 'link' to. Then a markdown link like:
[link text](#abcde)
anywhere in the document takes you there.
The <div…/>
solution inserts a "dummy" division just to add the id
property, and this is potentially disruptive to the page structure, but the <a name="abcde"/>
solution ought to be quite innocuous.
(PS: It might be OK to put the anchor in the line you wish to link to, as follows:
## <a name="head1">Heading One</a>
but this depends on how Markdown treats this. I note, for example, the Stack Overflow answer formatter is happy with this!)
回答4:
This may be out-of-date thread but to create inner document links in markdown in Github use...
(NOTE: lowercase #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.
A good question was made so I have edited my answer;
An inner link can be made to any title size using - #
, ##
, ###
, ####
I created a quick example below...
https://github.com/aogilvie/markdownLinkTest
回答5:
yes, markdown does do this but you need to specify the name anchor <a name='xyx'>
.
a full example,
this creates the link
[tasks](#tasks)
later in the document, you create the named anchor (whatever it is called).
<a name="tasks">
my tasks
</a>
note that you could also wrap it around the header too.
<a name="tasks">
### Agile tasks (created by developer)
</a>
回答6:
The pandoc manual explains how to link to your headers, using their identifier. I did not check support of this by other parsers, but it was reported that it does not work on github.
The identifier can be specified manually:
## my heading text {#mht}
Some normal text here,
including a [link to the header](#mht).
or you can use the auto-generated identifier. Both is explained in detail in the pandoc manual.
NOTE: This only works when converting to HTML, LaTex, ConTeXt, Textile or AsciiDoc.
回答7:
There is no such directive in the Markdown spec. Sorry.
回答8:
Gitlab uses GitLab Flavored Markdown (GFM)
Here "all Markdown-rendered headers automatically get IDs"
One can use mouse to :
## series expansion formula of the Boettcher function
which gives a link :
https://gitlab.com/adammajewski/parameter_external_angle/blob/master/README.md#series-expansion-formula-of-the-boettcher-function
Prefix can be removed so the link here is simply
file#header
which here means:
README.md#series-expansion-formula-of-the-boettcher-function
Now it can be used as :
[series expansion formula of the Boettcher function](README.md#series-expansion-formula-of-the-boettcher-function)
One can also do it manually: replace spaces with hyphen sign.
Live example is here
回答9:
Using kramdown, it seems like this works well:
[I want this to link to foo](#foo)
....
....
{: id="foo"}
### Foo are you?
I see it's been mentioned that
[foo][#foo]
....
#Foo
works efficiently, but the former might be a good alternative for elements besides headers or else headers with multiple words.
回答10:
Since MultiMarkdown was mentioned as an option in comments.
In MultiMarkdown the syntax for an internal link is simple.
For any heading in the document simply give the heading name in this format [heading][]
to create an internal link.
Read more here: MultiMarkdown-5 Cross-references.
Cross-References
An oft-requested feature was the ability to have Markdown automatically handle within-document links as easily as it handled external links. To this aim, I added the ability to interpret [Some Text][] as a cross-link, if a header named “Some Text” exists.
As an example, [Metadata][] will take you to # Metadata (or any of ## Metadata, ### Metadata, #### Metadata, ##### Metadata, ###### Metadata).
Alternatively, you can include an optional label of your choosing to help disambiguate cases where multiple headers have the same title:
### Overview [MultiMarkdownOverview] ##
This allows you to use [MultiMarkdownOverview] to refer to this section specifically, and not another section named Overview. This works with atx- or settext-style headers.
If you have already defined an anchor using the same id that is used by a header, then the defined anchor takes precedence.
In addition to headers within the document, you can provide labels for images and tables which can then be used for cross-references as well.
回答11:
Some more spins on the <a name="">
trick:
<a id="a-link"></a> Title
------
#### <a id="a-link"></a> Title (when you wanna control the h{N} with #'s)