It’s about everything - from H-One to H-Six. After all, every website needs a system for displaying headlines. Our headlines provide you with a so-called anchor link. If you click on the # symbol, we will save the link to your clipboard. So that you can share our great post on the messenger service you trust (hopefully Signal).

A Heading with a clickable Hashtag
A Heading with a clickable Hashtag

How we have done this? Read on.

Hugo and Headlines #

Hugo offers a so-called »render hook« for headlines. This means you can change the behavior of how headlines are rendered from Markdown files. To do this, create the file called render-heading.html under the path layouts/_default/_markup:

Directory-Structure for the Headline-Anchor-Link

Render-Hook #

According to Hugos Documentation this file gets the following input variables from Hugo:

  • .Level - The hierarchical level of the headline: So 1 for H1, 2 for H2 etc.
  • .Text - This is the headline’s text
  • .Anchor - The anchor link of the headline itself

Rendering the Headline #

This snippet is all you need to render a headline:

<h{{ .Level }}>
    {{ .Text | safeHTML }}
</h{{ .Level }}>

However, we don’t just want to write the headline text, we also want to automatically display a hashtag at the end of the headline. To do this, we expand the snippet like this:

<h{{ .Level }}>
    <div class="lowlight"></div>
    {{ .Text | safeHTML }} #            
    </div>
</h {{ .Level }}>

We write a diamond symbol after the heading text. We also place a div with the lowlight class around the heading text. Not much has happened yet. We therefore add a link to the snippet:

<h{{ .Level }}>
  <div class="lowlight"></div>
  {{ .Text | safeHTML }}
    <a class="heading-anchor" href="#{{.Anchor}}" aria-label="#" alt="#">
        {{ "#" }}            
    </a>
  </div>
</h {{ .Level }}>

So far. So good. The # symbol is now clickable. We gave the link element the class heading-anchor. The reason for this is so that we can easily select all anchor elements using Javascript.

Outro #

With these snippets you should be able to display a clickable anchor link behind your headlines. Your headlines can then be clicked. But you are still responsible for them to kick.