Example on how to add copy-link buttons to headings for the convince of the user. While this is not the most important feature it is easy enough to implement so why not add it.
The (fairly lightweight)
clipboardjs library is used to copy the code to clipboard. At the time of writing 2.0.6.
For the tooltips the
tooltips CSS library is used.
This article was written for Hugo 0.71.
Usage
Hugo automatically generates the headings, so nothing else has to be done. The script will automatically wire the copy-link buttons and we’re done.
Demo
Here are some headers. Hoover over them to reveal the copy button.
Level deeper
Some text
Another level deeper
Other text
Yet deeper
Other text
The end (this is a heading)
Other text
Code
The overwritten Hugo’s markdown heading template, located at /layout/_default/_markup/render-heading.html. Because this template is used for the whole site and we only want heading links on single pages adding the copy-link button is conditional.
License: The text and content is licensed under CC BY-NC-SA 4.0.
All source code I wrote on this page is licensed under The Unlicense; do as you please, I'm not liable nor provide warranty.
Noticed an error in this post? Corrections are
spam.appreciated.