Heading levels in PDF


I’m generating PDFs from specific chapters in a Hugo documentation project. I have a layout that combines the content of the chapter in a single file, however, I have trouble with the way headings are handled.

I have the below section in the layout, which includes specifying the heading levels. I didn’t write this, so I’m not entirely sure what each bit does.

I don’t think it’s working correctly. In the generated file, all sections become h1 regardless of where the file is in the directory hierarchy.

In addition, there might be some topics that are of “page” kind and some that are “section” on the same level in the hierarchy, but they’ll be different heading levels based on this template.

Can anyone recommend a better way of doing this?

Thank you very much!

{{- $currentNode := . }}
{{ template "heading_walk" dict "sect" . "currentnode" $currentNode }}
{{- define "heading_walk" }}
  {{- $currentNode := .currentnode }}
  {{- with .sect}}
    {{- if .IsSection }}
      {{- $numberOfPages := (add (len .Pages) (len .Sections)) }}
      {{- $header_level:=(split .Page.File.Dir "/" | len )}}
<h{{$header_level}}>{{ .Title }}</h{{$header_level}}>
{{ template "print_content" . }}
        {{- if ne $numberOfPages 0 }}
            {{- .Scratch.Set "pages" .Pages }}
            {{- if .Sections}}
            {{- .Scratch.Set "pages" (.Pages | union .Sections) }}
            {{- end}}
            {{- $pages := (.Scratch.Get "pages") }}
            {{- range $pages.ByWeight }}
              {{ template "heading_walk" dict "sect" . "currentnode" $currentNode }}
            {{- end}}
        {{- end}}
    {{- else}}
{{- $header_level:=(split .Page.File.Dir "/" | len | add 1 )}}
{{ template "print_content" . }}
    {{- end}}
 {{- end }}
{{- end}}

Yes. Spend some time to understand what each bit does.

Thanks! I did spend multiple days on trying to understand it and fix it, but I couldn’t work out why the splitting of the directory path and and counting the length bit is not working as I’d expect. Which is why I posted here. Sorry I didn’t phrase my question correctly.