Skip to content

Instantly share code, notes, and snippets.

@Lego2012
Last active September 18, 2017 13:59
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 Lego2012/74c9d19b5706af6ce393ca15cd40cde9 to your computer and use it in GitHub Desktop.
Save Lego2012/74c9d19b5706af6ce393ca15cd40cde9 to your computer and use it in GitHub Desktop.
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:

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

<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