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.
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).
note that you could also wrap it around the header too.
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.
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:before and
after the line you want to 'link' to. Then a markdown link like:
anywhere in the document takes you there.
The
<div…/>
solution inserts a "dummy" division just to add theid
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:
but this depends on how Markdown treats this. I note, for example, the Stack Overflow answer formatter is happy with this!)
Using kramdown, it seems like this works well:
I see it's been mentioned that
works efficiently, but the former might be a good alternative for elements besides headers or else headers with multiple words.
Github automatically parses anchor tags out of your headers. So you can do the following:
In the above case, the
Foo
header has generated an anchor tag with the namefoo
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.Update
Works out of the box with
pandoc
too.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 commandthis bit of markdown:
will yield this as the body of the html: