figure.html 2.5 KB

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