Transform ox-hugo anchors to links
This approach comes from @kaushalmodi, the author of ox-hugo. I export my content from Org to Hugo-compatible Markdown using this package.
After exporting, headings are followed by anchors in Markdown:
## Heading {#heading}
### Another heading {#another-heading}
Create a partial layouts/partials/headline-hash.html
:
{{ . | replaceRE "(<h[2-9] id=\"([^\"]+)\".+)(</h[2-9]+>)" `${1} <a class="headline-hash" href="#${2}">#</a> ${3}` | safeHTML }}
Apply the partial on .Content
:
{{ partial "headline-hash.html" .Content }}
The anchors become links:
<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