Transform ox-hugo anchors to links
This blog is built with Hugo, and the content is exported from Org files to Hugo-compatible Markdown via ox-hugo. Each heading is followed by an anchor after the export:
## Heading {#heading}
### Another heading {#another-heading}
To make the anchors clickable, Kaushal Modi (the author of ox-hugo) recommends replacing anchors with links (source), the steps are as follows:
First, create a partial layouts/partials/headline-hash.html
:
{{ . | replaceRE `(<h[2-9] id="([^"]+)".+)(</h[2-9]+>)` `${1} <a class="headline-hash opacity-0" href="#${2}">#</a> ${3}` | safeHTML }}
Next, apply the partial to .Content
:
{{ partial "headline-hash.html" .Content }}
The result will look like this:
<h2 id="heading">Heading <a class="headline-hash" href="#heading">#</a> </h2>
<h3 id="another-heading">Another heading <a class="headline-hash" href="#another-heading">#</a> </h3>
#hugo