Files
HugoBlox-kit/layouts/shortcodes/figure.html
T
George Cushen 361707f4fd feat: lazy load figures and gallery images
The proportion of image data in the typical payload of a website can be significant. Lazy loading is a solution that lowers initial page payload and load time, without removing content. Non-critical resources are instead loaded at the moment that they are needed.

Also, load MathJax async in head if required, rather than at end of body.

Close #1106
2019-12-18 14:33:22 +00:00

37 lines
1.5 KiB
HTML

{{/* Enable image to be loaded from local page dir or media library at `static/img/`. */}}
{{ $image_src := .Get "src" }}
{{ if .Get "library" }}
{{ $image_src = printf "img/%s" $image_src | relURL }}
{{ end }}
{{/* Disallow user from opening image in the lightbox? */}}
{{ $lightbox := eq (.Get "lightbox" | default "true") "true" }}
{{/* Get lightbox group for showing multiple images in a lightbox. */}}
{{ $group := .Get "lightbox-group" | default "" }}
{{/* Get caption. Support legacy `title` option. */}}
{{ $caption := .Get "title" | default (.Get "caption") | default "" }}
<figure{{ with .Get "class" }} class="{{.}}"{{ end }}>
{{ if $lightbox }}
<a data-fancybox="{{$group}}" href="{{$image_src}}" {{ with $caption }}data-caption="{{ .|markdownify|emojify }}"{{ end }}>
{{ else if .Get "link"}}
<a href="{{ .Get "link" }}"{{ with .Get "target" }} target="{{.}}"{{ end }}{{ with .Get "rel" }} rel="{{.}}"{{ end }}>
{{ end -}}
<img data-src="{{$image_src}}" class="lazyload" alt="{{ with .Get "alt" }}{{.}}{{end}}" {{ with .Get "width" }}width="{{.}}" {{end}}{{ with .Get "height" }}height="{{.}}" {{end}}>
{{- if or $lightbox (.Get "link") }}</a>{{ end }}
{{ if $caption }}
{{/* Localize the figure numbering (if enabled). */}}
{{ $figure := split (i18n "figure" | default "Figure %d:") "%d" }}
<figcaption{{ if eq (.Get "numbered") "true" }} data-pre="{{ index $figure 0 }}" data-post="{{ index $figure 1 }}" class="numbered"{{ end }}>
{{ $caption | markdownify | emojify }}
</figcaption>
{{ end }}
</figure>