figure.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. {{/* Enable image to be loaded from local page dir or media library at `static/img/`. */}}
  2. {{ $asset := (.Page.Resources.ByType "image").GetMatch (.Get "src") }}
  3. {{ $image_src := (.Get "src") }}
  4. {{ if $asset }}
  5. {{ $asset2 := $asset.Fit "2000x2000" }}
  6. {{ $image_src = $asset2.RelPermalink }}
  7. {{ else if .Get "library" }}
  8. {{ $image_src = printf "img/%s" $image_src | relURL }}
  9. {{ end }}
  10. {{/* Disallow user from opening image in the lightbox? */}}
  11. {{ $lightbox := eq (.Get "lightbox" | default "true") "true" }}
  12. {{/* Get lightbox group for showing multiple images in a lightbox. */}}
  13. {{ $group := .Get "lightbox-group" | default "" }}
  14. {{/* Get caption. Support legacy `title` option. */}}
  15. {{ $caption := .Get "title" | default (.Get "caption") | default "" }}
  16. <figure{{ with .Get "class" }} class="{{.}}"{{ end }} {{ with $caption }}id="figure-{{ anchorize . }}"{{ end }}>
  17. {{ if $lightbox }}
  18. <a data-fancybox="{{$group}}" href="{{$image_src}}" {{ with $caption }}data-caption="{{ .|markdownify|emojify }}"{{ end }}>
  19. {{ else if .Get "link"}}
  20. <a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{.}}"{{ end }}{{ with .Get "rel" }} rel="{{.}}"{{ end }}>
  21. {{ end -}}
  22. {{/* Lazy load only when we know image dimensions in order to preserve anchor linking. */}}
  23. {{ if $asset }}
  24. <img data-src="{{$image_src}}" class="lazyload" alt="{{ with .Get "alt" }}{{.}}{{end}}" width="{{ (.Get "width") | default $asset.Width }}" height="{{ (.Get "height") | default $asset.Height }}">
  25. {{ else if and (.Get "width") (.Get "height") }}
  26. <img data-src="{{$image_src}}" class="lazyload" alt="{{ with .Get "alt" }}{{.}}{{end}}" {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}}>
  27. {{ else }}
  28. <img src="{{$image_src}}" alt="{{ with .Get "alt" }}{{.}}{{end}}" {{ with .Get "width" }}width="{{.}}"{{end}} {{ with .Get "height" }}height="{{.}}"{{end}}>
  29. {{ end }}
  30. {{- if or $lightbox (.Get "link") }}</a>{{ end }}
  31. {{ if $caption }}
  32. {{/* Localize the figure numbering (if enabled). */}}
  33. {{ $figure := split (i18n "figure" | default "Figure %d:") "%d" }}
  34. <figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{ index $figure 0 }}" data-post="{{ index $figure 1 }}" class="numbered"{{ end }}>
  35. {{ $caption | markdownify | emojify }}
  36. </figcaption>
  37. {{ end }}
  38. </figure>