Hugo menu link to heading using id

Site configuration

[[menus.main]]
name = 'Home (Section 2)'
pageRef = '/'
weight = 10
[menus.main.params]
fragment = 'section-2'

[[menus.main]]
name = 'Post 1 (Section 3)'
pageRef = '/posts/post-1'
weight = 20
[menus.main.params]
fragment = 'section-3'

Then the menu code would look something like this:

{{ with site.Menus.main }}
  <nav class="nav-inline menu">
    <ul>
      {{ range . }}
        {{ $href := .URL }}
        {{ with .Params.fragment }}
          {{ $href = printf "%s#%s" $href . }}
        {{ end }}
        {{ if $.IsMenuCurrent .Menu . }}
          <li class="active"><a aria-current="page" href="{{ $href }}">{{ .Name }}</a></li>
        {{ else if $.HasMenuCurrent .Menu . }}
          <li class="ancestor"><a aria-current="true" href="{{ $href }}">{{ .Name }}</a></li>
        {{ else }}
          <li><a href="{{ $href }}">{{ .Name }}</a></li>
        {{ end }}
      {{ end }}
    </ul>
  </nav>
{{ end }}

The above retains both the fragment and query string.