Some websites, including all rendered Markdown documents on Github, have a small “anchor” next to each headline if you hover over it. They are directs links to a specific header, like in this example from Github:
I like this idea and it turns out that it requires no core changes at all. Apparently hugo is already adding an id to every header on each page, so all you need is a link to them. A ghetto, poorly styled option would simply be:
Of course you’d need to do that for all of h1 through h5 (or whatever.) But it works and requires no extra libraries or anything. I hope to make it styled like other things where the link is invisible unless you hover over it and it’s to the left of the header.
Basic idea is to group 1 from h1 and then utilize it. You can notice digit from heading tag (1 from h1) is included in the 5th group. And then it is used.
I tried your solution but it seems to fail. I get this error:
unexpected "{" in operand
I noticed that by quoting your code the code changed (DISCOURSE BUG?). I think that’s why your code doesn’t work (you copied from the quote with the wrong code). I figured out how to get your code to work:
Your code uses the number after the h and uses this as numbering. This is wrong because with your code
## This is my heading 1
## This is my heading 2
looks like
# 2. This is my heading 1
# 2. This is my heading 2
(both get the number 2 from h2)
Another solution
In your code the a-tag is included in the h-tag. (e.g. <h2><a href="bla"></a>Bla</h2>)
Another option is to place the a-tag before the h-tag (like in the code I quoted in my first post). And give the a-tag a class with the number of the h-tag and change the CSS: