Skip to content

Instantly share code, notes, and snippets.

@jan-martinek
Created July 17, 2021 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-martinek/24850f77d0c8954943946ded70d9c87f to your computer and use it in GitHub Desktop.
Save jan-martinek/24850f77d0c8954943946ded70d9c87f to your computer and use it in GitHub Desktop.
Workaround for Hugo: sorting pages by Czech alphabet (first 40 alpha chars)
{{ $page := .Page }}
{{ range .pages }}
{{ $alpha := "aábcčdďeéěfgh#iíjklmnňoópqrřsštťuúůvwxyýzž" }}
{{ $title := substr (.Title | lower | replaceRE "ch" "#" | replaceRE (print "[^" $alpha "]") "") 0 40 }}
{{ $sortStr := split $title "" }}
{{ $len := len $sortStr }}
{{ $score := math.Pow 100 42 }}
{{ range $index, $char := $sortStr }}
{{/* calc character rank */}}
{{ $num := len (index (findRE (print "^.*" $char) $alpha) 0) }}
{{/* calc score */}}
{{ $score = add $score (mul $num (math.Pow 100 (sub 40 $index))) }}
{{ end }}
{{ $sortKey := print (lang.NumFmt 0 $score "- .") $title }}
{{ $page.Scratch.SetInMap "pages" $sortKey (dict "Title" .Title "Permalink" .Permalink "Params" .Params) }}
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment