Closed KenHV closed 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
layouts/_default/single.html
{{ .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.
I'm going to add this very soon. I've got this implemented on my other theme as well 👍
Thanks!
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
towhich 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.