panr / hugo-theme-hello-friend

Pretty basic theme for Hugo that covers all of the essentials. All you have to do is start typing!
https://github.com/panr/hugo-theme-hello-friend/
MIT License
1.06k stars 1.22k forks source link

[Feature request] Adding anchors to headings #272

Closed KenHV closed 2 years ago

KenHV commented 2 years ago

Hugo allows adding anchors to headings in posts. They're convenient for navigation inside posts and for sharing a particular section.

I implemented this by changing line no. 52 in layouts/_default/single.html to

 {{ .Content | replaceRE "(<h[2-6] id=\"(.+)\".*>)(.*)(</h[2-6]>)" "${1}<a href=\"#${2}\" class=\"h-anchor\">#</a>${3}${4}" | safeHTML }}

which I kanged from https://discourse.gohugo.io/t/adding-anchor-next-to-headers/1726/22. The anchor can be stylized by using the "h-anchor" CSS tag. I also found this which could be of use: https://github.com/schnerring/hugo-theme-gruvbox/commit/e7355714bf3834630c08c5eb74e955a02714ba8f.

I would've made a PR but I'm sure there are cleaner ways of implementing this, like in the commit I linked. Would you please consider this? Thanks in advance.

panr commented 2 years ago

I'm going to add this very soon. I've got this implemented on my other theme as well 👍

KenHV commented 2 years ago

Thanks!