figure.html 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!-- https://github.com/gohugoio/hugo/issues/4406 -->
  2. <!-- https://github.com/gohugoio/hugo/issues/4562 -->
  3. {{ $image := .Get "src" }}
  4. <figure{{ with .Get "class" }} class="{{ . }}"{{ end }}>
  5. {{ if .Get "link"}}<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>{{ end }}
  6. {{ if (findRE "(^|:)//" $image) -}} <!-- If image link is a URL with protocol or two leading slashes, use just that unmodified. -->
  7. <img src="{{ $image }}"
  8. {{- else if (findRE "^/" $image) -}} <!-- If image link has one leading slash -->
  9. {{- /* Cannot use absURL below because it doesn't work as expected if baseURL has a subdir. */ -}}
  10. {{- $baseurl_no_trailing_slash := $.Site.BaseURL | replaceRE "/$" "" -}}
  11. <img src="{{ (printf "%s%s" $baseurl_no_trailing_slash $image) }}"
  12. {{- else -}}
  13. {{- /* Below variable will always have a trailing slash, even with uglyURLs enabled. */ -}}
  14. {{- $permalink_pretty := $.Page.Permalink | replaceRE "\\.html$" "/" -}}
  15. <img src="{{ (printf "%s%s" $permalink_pretty $image) }}"
  16. {{- end -}}
  17. {{- if (or (.Get "alt") (.Get "caption")) -}}
  18. {{- with .Get "alt"}}
  19. {{- printf " alt=\"%s\"" . | safeHTMLAttr -}}
  20. {{- else -}}
  21. {{- printf " alt=\"%s\"" (.Get "caption" | markdownify | plainify) | safeHTMLAttr -}}
  22. {{- end -}}
  23. {{- end -}}
  24. {{- with .Get "width" }} width="{{ . }}"{{ end -}}
  25. {{- with .Get "height" }} height="{{ . }}"{{ end -}}/> <!-- Closing img tag -->
  26. {{- if .Get "link" }}</a>{{ end -}}
  27. {{- if or (or (.Get "title") (.Get "caption")) (.Get "attr") -}}
  28. <figcaption>
  29. {{ with (.Get "title") }}
  30. <h4>{{ . }}</h4>
  31. {{ end }}
  32. {{ if (or (.Get "caption") (.Get "attr")) }}<p>
  33. {{ .Get "caption" | markdownify }}
  34. {{ with .Get "attrlink" }}<a href="{{ . }}"> {{ end }}
  35. {{ .Get "attr" | markdownify }}
  36. {{ if .Get "attrlink" }}</a>{{ end }}</p>
  37. {{ end }}
  38. </figcaption>
  39. {{- end -}}
  40. </figure>