figure.html 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. {{ if $isSVG -}}
  28. <img alt="{{ $alt }}"
  29. src="{{ $img.RelPermalink }}"
  30. loading="lazy"
  31. {{- if $zoom }} data-zoomable{{end}}
  32. {{- with .Get "width" }} width="{{.}}"{{end}}
  33. {{- with .Get "height" }} height="{{.}}"{{end}}
  34. {{- with $img_class }} class="{{.}}"{{end}} />
  35. {{- else }}
  36. {{- $img_lg := $img.Fit "1200x1200" -}}
  37. {{- $img_md := $img_lg.Fit "760x760" -}}{{/* Match `.docs-article-container` max-width */}}
  38. {{- $img_sm := $img_md.Fit "400x400" -}}
  39. {{- $width := (.Get "width") | default $img_md.Width -}}
  40. {{- $height := (.Get "height") | default $img_md.Height -}}
  41. <img alt="{{ $alt }}" srcset="
  42. {{ $img_sm.RelPermalink }} 400w,
  43. {{ $img_md.RelPermalink }} 760w,
  44. {{ $img_lg.RelPermalink }} 1200w"
  45. src="{{ $img_sm.RelPermalink }}"
  46. width="{{ $width }}"
  47. height="{{ $height }}"
  48. loading="lazy"
  49. {{- if $zoom }} data-zoomable{{end}}
  50. {{- with $img_class }} class="{{.}}"{{end}} />
  51. {{- end }}
  52. {{- else -}}
  53. <img src="{{ $destination | safeURL }}" alt="{{ $alt }}" loading="lazy" {{ if $zoom }}data-zoomable{{end}}
  54. {{- with .Get "width" }} width="{{.}}"{{end}} {{- with .Get "height" }} height="{{.}}"{{end}}
  55. {{- with $img_class }} class="{{.}}"{{end}} />
  56. {{- end -}}
  57. </div>
  58. </div>
  59. {{- if $caption -}}
  60. {{/* Localize the figure numbering (if enabled). */}}
  61. {{- $figure := split (i18n "figure" | default "Figure %d:") "%d" -}}
  62. <figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{- trim (index $figure 0) " " -}}&nbsp;" data-post="{{ index $figure 1 }}&nbsp;" class="numbered"{{ end }}>
  63. {{ $caption | markdownify | emojify }}
  64. </figcaption>
  65. {{- end -}}
  66. </figure>