22

I'm trying to create a link to a section of the README file of my Bitbucket repository. The following works as expected on GitHub, but not on Bitbucket. What am I doing wrong?

## Navigation
[1. GIT To Work](#git)

... 

## 1. Git To Work - Working with git<a name="git"></a>
### What is git?
jub0bs
  • 60,866
  • 25
  • 183
  • 186
Cruzito
  • 391
  • 1
  • 3
  • 10
  • 4
    I'm voting to close this question as off-topic because it asks for user support for a specific site; see http://meta.stackoverflow.com/questions/255745/why-were-not-customer-support-for-your-favorite-company . – Marcus Müller Mar 22 '15 at 11:06
  • 3
    Closing this question seems unfair. This [one about GitHub wikis](http://stackoverflow.com/questions/26584788/change-font-in-github-flavored-markdown-for-table) remains open. – jub0bs Mar 23 '15 at 15:41

2 Answers2

31

One peculiarity of Bitbucket's Markdown "flavour" is the way it names heading identifiers. If you inspect the HTML source of your Bitbucket README page (by default accessible at https://bitbucket.org/<username>/<reponame>), you'll see that the Markdown section

## 1. Git To Work - Wording with git

translates to the following HTML heading element:

<h2 id="markdown-header-1-git-to-work-working-with-git">1. Git To Work - Working with git</h2>

Note that Bitbucket uses, as the value of the id attribute, a URL-friendly version of your heading, prefixed by markdown-header-. Therefore, you can create a link to the corresponding section, in your Markdown code, using

[Link to Git](#markdown-header-1-git-to-work-working-with-git)
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 15
    My Bitbucket doesn't generate "id" attribute for headers. It's just simply "

    YAML configuration

    ". How can I link to it? (Atlassian Bitbucket v5.8.0)
    – emeraldhieu Mar 16 '18 at 04:32
  • 4
    Our internal BitBucket does not auto generate ID's on h2's either. – Frank Stallone III Mar 31 '20 at 13:45
  • 1
    Just used this answer to reach the probably more up-to-date solution - for me I used `[Link to Git](#markdown-header-git-to-work-working-with-git)` - prefixing with `markdown-header` (and not `markdown-header-1` or any number at all). Thanks! – yair Nov 16 '20 at 12:04
  • 2
    On Bitbucket Server, there is neither a name or id generated on header tags and it will strip id attributes, so you're forced to use `name` and link to the name specified. – Maximilian Burszley Dec 15 '20 at 16:29
  • @MaximilianBurszley My answer is getting old, now. Things might have changed. Feel free to update it or, better, post your own answer. – jub0bs Dec 15 '20 at 18:32
  • 1
    That's fair. I was just adding nuance as I'm pretty sure Bitbucket Cloud works as your answer provides. – Maximilian Burszley Dec 15 '20 at 18:38
  • @MaximilianBurszley Can you please explain what you mean by "using `name`"? Maybe, as it seems to affect everyone using BitBucket Server, it would be even worth a separate answer? – Johannes Dec 05 '22 at 12:45
  • Found answer to my question: https://stackoverflow.com/a/67322161/985292 . – Johannes Dec 08 '22 at 09:06
  • These `#` URI fragments don't work on Page load. They only work after the page has fully loaded. So if you copy & paste the full URL into a browser, it won't scroll to the right spot on page load. You can prove this by adding e.g. an extra "x" the the URL, press `enter`, then remove the "x" and press `enter`. The 2nd `enter` will finally scroll to the right spot. – stwr667 Mar 14 '23 at 05:43
12

Anchor links work on my Bitbucket server but don't work in VS Code unfortunately.

[link](#section) 

## Code and Syntax highlighting  <a name="section"></a>
perry_the_python
  • 385
  • 5
  • 14