figure.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. {{/* Figure Shortcode for Wowchemy. */}}
  2. {{/* Load image from page dir falling back to media library at `assets/media/` and then to remote URI. */}}
  3. {{/* Note: Uses `{{-` to unindent HTML so that Figure shortcode can be nested within a `{{%` Markdown shortcode,
  4. such as Callout, without the HTML being rendered as a Markdown code block. */}}
  5. {{ $destination := .Get "src" }}
  6. {{ $caption := .Get "caption" | default (.Get "title") | default "" }}{{/* Support legacy `title` option. */}}
  7. {{ $zoom := eq (.Get "lightbox" | default "true") "true" }}
  8. {{ $id := anchorize (.Get "id" | default ($caption | plainify)) }}
  9. {{ $alt := .Get "alt" | default ($caption | plainify) }}
  10. {{- $img := (.Page.Resources.ByType "image").GetMatch $destination -}}
  11. {{- if not $img -}}
  12. {{- $img = resources.Get (path.Join "media" $destination) -}}
  13. {{- end -}}
  14. <figure {{ with .Get "class" }}class="{{.}}"{{ end }} {{ with $id }}id="figure-{{ . }}"{{ end }}>
  15. {{- if $img -}}
  16. {{ $isSVG := eq $img.MediaType.SubType "svg" }}
  17. {{ if $isSVG -}}
  18. <img alt="{{ $alt }}"
  19. src="{{ $img.RelPermalink }}"
  20. loading="lazy"
  21. {{ if $zoom }}data-zoomable{{end}}
  22. {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}} />
  23. {{- else }}
  24. {{- $img_lg := $img.Fit "1200x1200" -}}
  25. {{- $img_md := $img_lg.Fit "760x760" -}}{{/* Match `.docs-article-container` max-width */}}
  26. {{- $img_sm := $img_md.Fit "400x400" -}}
  27. {{- $width := (.Get "width") | default $img_md.Width -}}
  28. {{- $height := (.Get "height") | default $img_md.Height -}}
  29. <img alt="{{ $alt }}" srcset="
  30. {{ $img_sm.RelPermalink }} 400w,
  31. {{ $img_md.RelPermalink }} 760w,
  32. {{ $img_lg.RelPermalink }} 1200w"
  33. src="{{ $img_sm.RelPermalink }}"
  34. width="{{ $width }}"
  35. height="{{ $height }}"
  36. loading="lazy"
  37. {{ if $zoom }}data-zoomable{{end}} />
  38. {{- end }}
  39. {{- else -}}
  40. <img src="{{ $destination | safeURL }}" alt="{{ $alt }}" loading="lazy" {{ if $zoom }}data-zoomable{{end}}
  41. {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}} />
  42. {{- end -}}
  43. {{ if $caption }}
  44. {{/* Localize the figure numbering (if enabled). */}}
  45. {{- $figure := split (i18n "figure" | default "Figure %d:") "%d" -}}
  46. <figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{- trim (index $figure 0) " " -}}&nbsp;" data-post="{{ index $figure 1 }}&nbsp;" class="numbered"{{ end }}>
  47. {{ $caption | markdownify | emojify }}
  48. </figcaption>
  49. {{- end -}}
  50. </figure>