figure.html 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 alt="{{ $alt }}"
  16. src="{{ $img.RelPermalink }}"
  17. loading="lazy"
  18. data-zoomable
  19. {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}} />
  20. {{ else }}
  21. {{- $img_lg := $img.Fit "1200x1200" -}}
  22. {{- $img_md := $img_lg.Fit "760x760" -}}{{/* Match `.docs-article-container` max-width */}}
  23. {{- $img_sm := $img_md.Fit "400x400" -}}
  24. {{- $width := (.Get "width") | default $img_md.Width -}}
  25. {{- $height := (.Get "height") | default $img_md.Height -}}
  26. <img alt="{{ $alt }}" srcset="
  27. {{ $img_sm.RelPermalink }} 400w,
  28. {{ $img_md.RelPermalink }} 760w,
  29. {{ $img_lg.RelPermalink }} 1200w"
  30. src="{{ $img_sm.RelPermalink }}"
  31. width="{{ $width }}"
  32. height="{{ $height }}"
  33. loading="lazy"
  34. data-zoomable />
  35. {{ end }}
  36. {{- else -}}
  37. <img src="{{ $destination | safeURL }}" alt="{{ $alt }}" loading="lazy" data-zoomable
  38. {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}} />
  39. {{- end -}}
  40. {{ if $caption }}
  41. {{/* Localize the figure numbering (if enabled). */}}
  42. {{ $figure := split (i18n "figure" | default "Figure %d:") "%d" }}
  43. <figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{- trim (index $figure 0) " " -}}&nbsp;" data-post="{{ index $figure 1 }}&nbsp;" class="numbered"{{ end }}>
  44. {{ $caption | markdownify | emojify }}
  45. </figcaption>
  46. {{ end }}
  47. </figure>