Is it feasible to modify the built-in shortcodes in Hugo?

Hello,

Hugo comes with a lot of built-in shortcodes, but is it possible to modify the output format instead of making a new short-code?

As an example,

The date from the front matter is output by using {{< param date >}}: 2022-02-18T23:16:01+02:00 in markdown.

That’s excellent, but can I use it in conjunction with the “.Format” to get a human-readable date?

The built-in short-codes are intended to be used just as they are or I can enhance the output?

Thanks

Failed attempts

I attempted multiple times to combine the built-in shortcode parameter with format, but each time I failed. An example of a failed effort is shown below.
{{< param date.Format "Monday, Jan 2, 2006" "2015-01-21" >}}

No, it is not.

Here’s an example of modifying the embedded param shortcode to display the predefined date values as localized <time> elements.

layouts/shortcodes/param.html
{{- $name := .Get 0 }}
{{- with $name }}
  {{- with ($.Page.Param .) }}
    {{- if in (slice "date" "expirydate" "lastmod" "publishdate") (lower $name) }}
      {{- $datetimeDisplay := . | time.Format ":date_long" }}
      {{- $datetimeValue := .Format "2006-01-02T15:04:05-07:00" -}}
      <time datetime="{{ $datetimeValue }}">{{ $datetimeDisplay }}</time>
    {{- else -}}
      {{ . }}
    {{- end }}
  {{- else }}
    {{- errorf "Param %q not found: %s" $name $.Position }}
  {{- end }}
{{- else }}
  {{- errorf "Missing param key: %s" $.Position }}
{{- end -}}
3 Likes

Thank you :pray: for your prompt response and for sharing the extremely useful code example

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.