render-image.html 3.1 KB

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