more

Hugo ordinal abbreviations

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

Here is a small snippet you can add in your template for these st, nd abbreviations. They are emulated with HTML superscript and small tags.

This example takes a page’s date and extracts the day. The abbreviation is added depending on the standard Julian calendar days 1-31. You can use this in an equivalent manner of the _default/li.html template. These abbrebiations 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 a single 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. There was a bug in the template version that did not exist in the shortcode. The template code has been updated to match.