Skip to content

Instantly share code, notes, and snippets.

@victoriadrake
Created September 6, 2020 20:54
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 victoriadrake/1f0d2253712dbc726a28ebd14ebb3999 to your computer and use it in GitHub Desktop.
Save victoriadrake/1f0d2253712dbc726a28ebd14ebb3999 to your computer and use it in GitHub Desktop.
Render full image URLs in your Hugo site with a markup render hook

Render full image URLs in your Hugo site

This is a markup render hook for Hugo sites to change the behavior of image links in your built site. It should live at layouts/_default/_markup/render-image.html.

If the image link begins with http, the link will be rendered as-is.

If not, the page permalink will prefix the image URL to produce the full path to the image in the rendered site page.

{{ $link := string .Destination }}
{{ if hasPrefix $link "http" }}
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />
{{ else }}
<img src="{{ .Page.Permalink }}{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title}} title="{{ . }}"{{ end }} />
{{ end }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment