figure.html 2.1 KB

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