{{ define "main" }}
  {{- $prev_page := false }}
  {{- /* Try to get the previous sibling */}}
  {{- with .Parent.Pages.Next . }}

    {{- /* Check if it's a section, if yes, get it's last page */}}
    {{- if (and .IsSection (gt .Pages.Len 0 )) }}
      {{- $prev_page = index .Pages.Reverse 0 }}
    {{ else }}
      {{- $prev_page = . }}
    {{ end }}

  {{ else }}

    {{- /* No previous sibling, use the parent itself */}}
    {{- $prev_page = .Parent }}

  {{ end }}

  {{- $next_page := false }}

  {{- if gt .Pages.Len 0 }}
    {{- $next_page = index .Pages 0 }}
  {{- else }}
    {{- with .Parent.Pages.Prev . }}
        {{- $next_page = . }}
      {{ end }}
  {{- end }}

  {{- $reading_time := 0}}
  {{- range .RegularPagesRecursive }}
    {{- $reading_time = add $reading_time .ReadingTime}}
  {{- end}}
  <h1>{{ .Title }}</h1>
  <p class="subtitle">Total reading time: about {{ $reading_time }} min</p>
  <hr>
  <nav class="page-nav">
    {{ with $prev_page }}<a class="prev" title="Previous page" href="{{.RelPermalink}}">&lt; {{ .Title }}</a>{{ end }}
    {{ with $next_page }}<a class="next" title="Next page" href="{{.RelPermalink}}">{{ .Title }} &gt;</a>{{ end }}
  </nav>
  <hr>
  {{ .Content }}
  <hr>
  {{ partial "pagesTOC.html" (dict "page" . "header_level" 2) }}
  <hr>
  <nav class="page-nav">
    {{ with $prev_page }}<a class="prev" title="Previous page" href="{{.RelPermalink}}">&lt; {{ .Title }}</a>{{ end }}
    {{ with $next_page }}<a class="next" title="Next page" href="{{.RelPermalink}}">{{ .Title }} &gt;</a>{{ end }}
  </nav>
{{ end }}
