figure.html 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. {{ $is_remote := strings.HasPrefix $destination "http" }}
  7. {{ $caption := .Get "caption" | default (.Get "title") | default "" }}{{/* Support legacy `title` option. */}}
  8. {{ $zoom := eq (.Get "lightbox" | default "true") "true" }}
  9. {{ $id := anchorize (.Get "id" | default ($caption | plainify)) }}
  10. {{ $alt := .Get "alt" | default ($caption | plainify) }}
  11. {{ $img_class := "" }}
  12. {{ if eq (.Get "theme" | lower) "light" }}{{ $img_class = printf "%s img-light" $img_class }}{{end}}
  13. {{ if eq (.Get "theme" | lower) "dark" }}{{ $img_class = printf "%s img-dark" $img_class }}{{end}}
  14. {{/* Workaround Hugo v0.81 error on Windows when `resources.Get (path.Join "media" <URL>)` */}}
  15. {{- $img := "" -}}
  16. {{- if not $is_remote -}}
  17. {{- $img = (.Page.Resources.ByType "image").GetMatch $destination -}}
  18. {{- if not $img -}}
  19. {{- $img = resources.Get (path.Join "media" $destination) -}}
  20. {{- end -}}
  21. {{- end -}}
  22. <figure {{ with .Get "class" }}class="{{.}}"{{end}} {{ with $id }}id="figure-{{ . }}"{{ end }}>
  23. <div class="d-flex justify-content-center">
  24. <div class="w-100" {{ with .Get "max_width" }}style="max-width: {{.}}"{{end}}>
  25. {{- if $img -}}
  26. {{- $isSVG := eq $img.MediaType.SubType "svg" -}}
  27. {{- $isGIF := eq $img.MediaType.SubType "gif" -}}
  28. {{- if $isSVG | or $isGIF -}}
  29. <img alt="{{ $alt }}"
  30. src="{{ $img.RelPermalink }}"
  31. loading="lazy"
  32. {{- if $zoom }} data-zoomable{{end}}
  33. {{- with .Get "width" }} width="{{.}}"{{end}}
  34. {{- with .Get "height" }} height="{{.}}"{{end}}
  35. {{- with $img_class }} class="{{.}}"{{end}} />
  36. {{- else }}
  37. {{- $img_lg := $img.Fit "1200x1200 webp" -}}
  38. {{- $img_md := $img_lg.Fit "760x760 webp" -}}{{/* Match `.docs-article-container` max-width */}}
  39. {{- $img_sm := $img_md.Fit "400x400 webp" -}}
  40. {{- $width := (.Get "width") | default $img_md.Width -}}
  41. {{- $height := (.Get "height") | default $img_md.Height -}}
  42. <img alt="{{ $alt }}" srcset="
  43. {{ $img_sm.RelPermalink }} 400w,
  44. {{ $img_md.RelPermalink }} 760w,
  45. {{ $img_lg.RelPermalink }} 1200w"
  46. src="{{ $img_sm.RelPermalink }}"
  47. width="{{ $width }}"
  48. height="{{ $height }}"
  49. loading="lazy"
  50. {{- if $zoom }} data-zoomable{{end}}
  51. {{- with $img_class }} class="{{.}}"{{end}} />
  52. {{- end }}
  53. {{- else -}}
  54. <img src="{{ $destination | safeURL }}" alt="{{ $alt }}" loading="lazy" {{ if $zoom }}data-zoomable{{end}}
  55. {{- with .Get "width" }} width="{{.}}"{{end}} {{- with .Get "height" }} height="{{.}}"{{end}}
  56. {{- with $img_class }} class="{{.}}"{{end}} />
  57. {{- end -}}
  58. </div>
  59. </div>
  60. {{- if $caption -}}
  61. {{/* Localize the figure numbering (if enabled). */}}
  62. {{- $figure := split (i18n "figure" | default "Figure %d:") "%d" -}}
  63. <figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{- trim (index $figure 0) " " -}}&nbsp;" data-post="{{ index $figure 1 }}&nbsp;" class="numbered"{{ end }}>
  64. {{ $caption | markdownify | emojify }}
  65. </figcaption>
  66. {{- end -}}
  67. </figure>