figure.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. {{/* Figure Shortcode for Wowchemy. */}}
  2. {{/* Load image from page dir falling back to media library at `assets/images/` and then to remote URI. */}}
  3. {{ $destination := .Get "src" }}
  4. {{ $caption := .Get "title" | default (.Get "caption") | default "" }}
  5. {{ $id := .Get "id" | default $caption }}
  6. {{ $alt := .Get "alt" | default ($caption | plainify) }}
  7. {{- $img := (.Page.Resources.ByType "image").GetMatch $destination -}}
  8. {{- if and (not $img) .Page.File -}}
  9. {{ $path := $destination }}
  10. {{- $img = resources.Get (path.Join "images" $path) -}}
  11. {{- end -}}
  12. <figure {{ with .Get "class" }}class="{{.}}"{{ end }} {{ with $id }}id="figure-{{ anchorize . }}"{{ end }}>
  13. {{- if $img -}}
  14. {{ $isSVG := eq $img.MediaType.SubType "svg" }}
  15. {{ if $isSVG }}
  16. {{ $img.Content | safeHTML }}
  17. {{ else }}
  18. {{- $img_lg := $img.Fit "1200x1200" -}}
  19. {{- $img_md := $img_lg.Fit "760x760" -}}{{/* Match `.docs-article-container` max-width */}}
  20. {{- $img_sm := $img_md.Fit "400x400" -}}
  21. <img alt="{{ $alt }}" srcset="
  22. {{ $img_sm.RelPermalink }} 400w,
  23. {{ $img_md.RelPermalink }} 760w,
  24. {{ $img_lg.RelPermalink }} 1200w"
  25. src="{{ $img_sm.RelPermalink }}"
  26. width="{{ (.Get "width") | default $img_md.Width }}"
  27. height="{{ (.Get "height") | default $img_md.Height }}"
  28. loading="lazy"
  29. data-zoomable />
  30. {{ end }}
  31. {{- else -}}
  32. <img src="{{ $destination | safeURL }}" alt="{{ $alt }}" loading="lazy" data-zoomable
  33. {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}} />
  34. {{- end -}}
  35. {{ if $caption }}
  36. {{/* Localize the figure numbering (if enabled). */}}
  37. {{ $figure := split (i18n "figure" | default "Figure %d:") "%d" }}
  38. <figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{- trim (index $figure 0) " " -}}&nbsp;" data-post="{{ index $figure 1 }}&nbsp;" class="numbered"{{ end }}>
  39. {{ $caption | markdownify | emojify }}
  40. </figcaption>
  41. {{ end }}
  42. </figure>