How to set an anchor in Markdown File

Viewed 3623

I want to set an anchor in a .md file. The file is in the Team Foundation Server but the tag <a name="anchor"></a> does not work. Are there any other possibilities to set an anchor in a .md file?

I already tried the following:

Link to an anchor

[Question 22](answers.md#answer22)

Setting an anchor

<a name="answer22"></a> This is an answer for Question 22

The result is that clicking on "Question 22" the file answers.md is opened successfully but isn't "hopping" to Question 22. Furthermore the tag <a name="answer22"><a/> doesn't seem to be recognized as Code in the md. File in the TFS. If I open the File answer22.md and look at the preview (in TFS you can switch between "contents", "preview", "history" etc.) the tag is not hidden and you can see it in the preview as if it is just plain text.

3 Answers

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

Just place your answer below a heading.

questions.md

[Question22]9(./answers.md#answer-22)

answers.md

## Answer 22

The answer is 42

See the Markdown Guide, Extended Syntax: Linking to Heading IDs.

This markdown:

[Heading IDs](#heading-ids) 

Should be rendered to following HTML by most supported engines:

<a href="#heading-ids">Heading IDs</a>  

Where the text of the headings serves as anchor-name in slug form (spaces converted to hyphens).

In Pandoc Markdown you can set anchors on arbitrary spans inside a paragraph using syntax [span]{#anchor}, e.g.:

[This is an answer for question 22]{#answer-22}

And then reference it as usual: [Question 22](#answer-22).

If you want to reference a whole paragraph then formally it's not possible but you can make a simple hack adding an empty span right in the beginning of the paragraph:

[]{#answer-22}
Paragraph text.
Related