Hugo anchors next to headers
All notes in this series:
- Automating hugo development with npm scripts
- normalize-scss with hugo
- Automatic image thumbnails in Hugo from static directory
- Escaping Hugo shortcodes within Hugo markdown
- Hugo tag and category pages
- Bind hugo to localhost for development
- Hugo 0.37 does not support h6 markdown heading
- Install Hugo testing distribution on Debian
- Hugo anchors next to headers
- Hugo: Migrating from Pygments to Chroma
- Hugo: Global resources
- Hugo: How to create a post series
This note documents how to add anchors to your headings in hugo, which show up only upon hover.
Example h2 heading §
Example h3 heading §
Example h4 heading §
Example h5 heading §
These headings are an example of the anchor in action. Hover over them and an anchor will show up to the right, which can be clicked on.
Code §
First, add a hugo partial named headline-hash
. This uses a regular expression to append an <a href ... />
after each <h[1-5] />
tag. Create the following file:
Then, update your layouts to use the partial on whatever .Content
it should apply to. For example, in my case the only place I needed to put it was in single.html
:
Finally, update your CSS so that the headline-hash
class is hidden by default, but shows when the mouse hovers over the heading:
Credit §
Full credit goes to kaushalmodi who shared this gist.
Future improvements §
As suggested by sephore, it may be possible to use markdown render hooks instead of replaceRE
.