Cross-Reference (Named Anchor) in Markdown

Cross-reference (named anchor) in markdown

Take me to [pookie](#pookie)

should be the correct markdown syntax to jump to the anchor point named pookie.

To insert an anchor point of that name use HTML:

<a name="pookie"></a>

Markdown doesn't seem to mind where you put the anchor point. A useful place to put it is in a header. For example:

### <a name="tith"></a>This is the Heading

works very well. (I'd demonstrate here but SO's renderer strips out the anchor.)

Note on self-closing tags and id= versus name=

An earlier version of this post suggested using <a id='tith' />, using the self-closing syntax for XHTML, and using the id attribute instead of name.

XHTML allows for any tag to be 'empty' and 'self-closed'. That is, <tag /> is short-hand for <tag></tag>, a matched pair of tags with an empty body. Most browsers will accept XHTML, but some do not. To avoid cross-browser problems, close the tag explicitly using <tag></tag>, as recommended above.

Finally, the attribute name= was deprecated in XHTML, so I originally used id=, which everyone recognises. However, HTML5 now creates a global variable in JavaScript when using id=, and this may not necessarily be what you want. So, using name= is now likely to be more friendly.

(Thanks to Slipp Douglas for explaining XHTML to me, and nailer for pointing out the HTML5 side-effect — see the comments and nailer's answer for more detail. name= appears to work everywhere, though it is deprecated in XHTML.)

How to link to a named anchor in Multimarkdown?

Taken from the Multimarkdown Users Guide (thanks to @MultiMarkdown on Twitter for pointing it out)

[Some Text][]will link to a header named “Some Text”


### Some Text ###

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.

How to link to part of the same document in Markdown?

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


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
<div id="TOC">
<a href="#introduction">Introduction</a>
<a href="#first-attempts">First Attempts</a>
<div id="introduction">
<a href="#TOC">Introduction</a>
Many have posed the question of true happiness. In this blog post we propose to solve it.
<div id="first-attempts">
<a href="#TOC">First Attempts</a>
The earliest attempts at attaining true happiness of course aimed at pleasure. Soon, though, the downside of pleasure was revealed.

Markdown Anchor Link With Same Name But Different Sections

There is no markdown specification that I know of that supports specifying a section-subsection-... format. As I understand it, they're converted to something like <a name=header>header</a> links, and there's no info on what the parent header is.

A workaround that I use is that when a header name is repeated, it gets a -1 appended to it so you can access with #header-1. The same pattern is applied for the next copy (#header-2), and the next (#header-3) and so forth.

Here is a sample markdown (working on VS Code 1.38.1):

# App

[module 1 background](#background)

[module 2 background](#background-1)

[module 3 background](#background-2)

## Module 1

### background

## Module 2

### SubModule 2-1

#### SubSubModule 2-1-1

##### background

## Module 3

### background

The problem with this workaround is that you'll have to keep track of the order of the duplicate names, which gets quite tedious if you have a lot. The good thing is it's easier to create a link to a duplicate name embedded in a series of headers (#background-1 is easier than ##module2###submodule-2-1####subsubmodule-2-1-1#####background).

I don't know exactly how or why this is, but VS Code is using the CommonMark specification and the markdown-it library to parse it, as mentioned in Markdown editing doc.

How to set an anchor in Markdown File

As stated in Microsofts basic Markdown guidance, anchors are generated for every heading.

Just place your answer below a heading.


## Answer 22

The answer is 42

Markdown link within document to heading with a period

The syntax is incorrect :

  • Only one pad is referenced instead of two.
  • the .sh takes it as a file

An solution

Try you the following :


## ``

What we have done is to disambiguate what is obtained as a file, converting it into a name when putting it in quotation marks.

Updated 22012019110907

Before an interesting investigation about why the previously proposed did not work, I have achieved a (temporary) solution that works.

The solution is to obtain the link through the readme symbol:

Adding link

Copy the link and generate it in our link:


## ``

For example :


## ``

Try me

This or in code snippet :