Hugo ordinal abbreviations

As of Hugo v0.49.2, numeric ordinal abbreviations, such as 1st or 2nd, are not supported: source.

Here is a small snippet you can add in your template to add these abbreviations. These are fake ligatures, emulated with the HTML superscript and small tags to elevate them above the baseline and reduce their size.

This example takes a page’s date and extracts the day. The abbreviation is added depending on the standard calendar days 1-31. You can use this in an equivalent of the _default/li.html template. These abbreviations are for English.

{{ with (.Date | dateFormat "2") -}}
{{- if (eq "1" .) -}}{{ . }}<sup><small>st</small></sup>
{{- else if (eq "2" .) -}}{{ . }}<sup><small>nd</small></sup>
{{- else if (eq "3" .) -}}{{ . }}<sup><small>rd</small></sup>
{{- else if (in "21 31" .) -}}{{ . }}<sup><small>st</small></sup>
{{- else if (in "22" .) -}}{{ . }}<sup><small>nd</small></sup>
{{- else if (in "23" .) -}}{{ . }}<sup><small>rd</small></sup>
{{- else -}}{{ . }}<sup><small>th</small></sup>
{{- end -}}
{{- end -}}

Here’s also a short code, which I saved as ordabbr.html taking parameter “day”, for use in Markdown. Sample usage is:

{{< ordabbr day="2" >}}

Shortcode:

{{ with (.Get "day") -}}
{{- if (eq "1" .) -}}{{ . }}<sup><small>st</small></sup>
{{- else if (eq "2" .) -}}{{ . }}<sup><small>nd</small></sup>
{{- else if (eq "3" .) -}}{{ . }}<sup><small>rd</small></sup>
{{- else if (in "21 31" .) -}}{{ . }}<sup><small>st</small></sup>
{{- else if (in "22" .) -}}{{ . }}<sup><small>nd</small></sup>
{{- else if (in "23" .) -}}{{ . }}<sup><small>rd</small></sup>
{{- else -}}{{ . }}<sup><small>th</small></sup>
{{- end -}}
{{- end -}}

The latest version of this code is on my GitHub.

Update 2018-10-29: The code was different between the template and shortcode usages. Unfortunately, there as a bug in the template version that did not exist in the shortcode. Therefore, the template code has been updated to match.