{{/* Generate rules for `aliases` page param. */ -}}

{{ $p := . -}}
{{ range $alias := $p.Aliases -}}
  {{ if not (hasPrefix $alias "/") -}}
    {{ $alias = partial "relative-redirects-alias"
        (dict
          "alias" $alias
          "p" (cond (hasPrefix $alias "./") $p $p.Parent)
        ) -}}
    {{ if hasPrefix $alias "./" -}}
      {{/* warnf ">> Found alias starting with ./ (%s)" $alias */ -}}
    {{ end -}}
  {{ end -}}
  {{ $alias | printf "%-35s" }} {{ $p.RelPermalink }}
  {{/* warnf ">> Alias: %s from %s" $alias $p */ -}}
{{ end -}}

{{- define "partials/relative-redirects-alias" -}}
  {{ $result := "" }}
  {{ if hasPrefix .alias "../" }}
    {{/* warnf ">> Found alias staring with ../ (%s)" .alias */ -}}
    {{ $result = (partial "relative-redirects-alias"
          (dict
            "alias" (strings.TrimPrefix "../" .alias)
            "p" .p.Parent ))
    }}
  {{ else }}
    {{ $result = path.Join .p.RelPermalink .alias }}
  {{ end }}
  {{ return $result }}
{{ end -}}
