Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Line Breaks in Shortcode #hugo

Here's how to enter an image caption once and output it twice as an alt attribute and as an HTML element with line breaks.

Specify your line breaks in your img shortcode in every post.

Then in the Shortcode template call plainify to strip the HTML from the image's alt attribute. And finally use safeHTML to render the line breaks inside an element

Example Shortcode template:

< img src="{{ $.Page.Site.BaseURL }}{{ .Get "src" }}" alt="{{ with .Get "caption"}}{{ . | plainify }}{{ end }}"/>

<h4>{{ with .Get "caption"}}{{ . | safeHTML }}{{ end }}</h4>

Example Shortcode usage in .md files:

{{< img src="/images/mi3_800x398.jpg" caption="Untitled,<br> oil on canvas,<br> 142.5 x 70 cm, 2010" >}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment