Are headings rendered through `render-heading.html` post-processed?

My render-heading.html looks like this:

{{ $anchor := .Anchor | anchorize -}}
<h{{ .Level }} id="{{ $anchor }}">{{ .Text | safeHTML }}<a class="headerlink" href="#{{ $anchor }}" title="Link to this heading">#</a></h{{ .Level }}>

I expect the id and the href to match.

However, here is what I see as output:

<h1 id="pythonとnumpyの-インストールガイド">PythonとNumPyの インストールガイド<a class="headerlink" href="#python%e3%81%a8numpy%e3%81%ae-%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab%e3%82%ac%e3%82%a4%e3%83%89" title="Link to this heading">#</a></h1>

I’ve opened issue 11678 but was redirected here.

This is Go’s html/template package doing its thing, escaping values within a certain context.

Pass the attribute name and value (together) through the safeHTMLAttr function.

{{ $anchor := .Anchor | anchorize -}}
<h{{ .Level }} id="{{ $anchor }}">{{ .Text | safeHTML }}<a class="headerlink" {{ printf "href=%q" $anchor | safeHTMLAttr }} title="Link to this heading">#</a></h{{ .Level }}>

Thank you for the explanation, @jmooring. I will adapt the code accordingly.

1 Like

I am unable to reproduce the reported behavior. Try it:

git clone --single-branch -b hugo-forum-topic-47018 https://github.com/jmooring/hugo-testing hugo-forum-topic-47018
cd hugo-forum-topic-47018
hugo server

I get:

  <h2 id="pythonとnumpyの-インストールガイド">PythonとNumPyの インストールガイド<a class="headerlink" href="pythonとnumpyの-インストールガイド" title="Link to this heading">#</a></h2>

I can confirm that it checks out. Sorry, too quick on the keyboard there!

For others following along, the anchor needs a # prefix in the href:

{{ $id := .Anchor | anchorize -}}
{{- $anchor := printf "#%s" $id -}}
<h{{ .Level }} id="{{ $id }}">{{ .Text | safeHTML }}<a class="headerlink" {{ printf "href=%q" $anchor | safeHTMLAttr }} title="Link to this heading">#</a></h{{ .Level }}>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.