|
| 1 | +{{/* Filename and path of an image */}} |
| 2 | +{{ $filename := .filename }} |
| 3 | + |
| 4 | +{{/* Image alt text */}} |
| 5 | +{{ $altText := default "" .altText }} |
| 6 | + |
| 7 | +{{/* Optional class name for the outer image wrapper */}} |
| 8 | +{{ $classname := default "" .classname }} |
| 9 | + |
| 10 | +{{/* Image sizes, this will go to the actual `img` element */}} |
| 11 | +{{ $sizes := default "100vw" .sizes }} |
| 12 | + |
| 13 | +{{/* |
| 14 | + Image widths, these are the different widths the image |
| 15 | + will be copied down to for different devices. |
| 16 | +*/}} |
| 17 | +{{ $widths := default (slice 1920 1440 1024 640 480 375) .widths }} |
| 18 | + |
| 19 | +{{/* Disable link to original full-size image */}} |
| 20 | +{{ $nolink := or .nolink false }} |
| 21 | + |
| 22 | +{{/* |
| 23 | + If true, look for the image in the `assets` directory |
| 24 | + otherwise look for the image relative to the current file. |
| 25 | +*/}} |
| 26 | +{{ $isAsset := or .isAsset false }} |
| 27 | + |
| 28 | +{{/* Optional maximum width of the image to be displayed */}} |
| 29 | +{{ $maxwidth := default (index $widths 0) .maxwidth }} |
| 30 | +{{ $maxwidth = int $maxwidth }} |
| 31 | + |
| 32 | +{{/* Create a local scratch variable */}} |
| 33 | +{{ $asset := newScratch }} |
| 34 | + |
| 35 | +{{/* Find the image file */}} |
| 36 | +{{ if $isAsset }} |
| 37 | + {{ $asset.Set "resource" (resources.Get $filename | fingerprint) }} |
| 38 | +{{ else }} |
| 39 | + {{ with site.GetPage "page" (printf "%s" .page) }} |
| 40 | + {{ with .Resources.GetMatch $filename }} |
| 41 | + {{ $asset.Set "resource" (. | fingerprint) }} |
| 42 | + {{ end }} |
| 43 | + {{ end }} |
| 44 | +{{ end }} |
| 45 | + |
| 46 | +{{/* Create the image element */}} |
| 47 | +{{ $img := $asset.Get "resource" }} |
| 48 | +{{ $imageId := printf "img-%s" (md5 $img.RelPermalink) }} |
| 49 | +{{ with $img }} |
| 50 | + {{ $minImageSize := index (last 1 $widths) 0 }} |
| 51 | + |
| 52 | + {{/* |
| 53 | + The outer element will be an anchor tag by default, |
| 54 | + or a div if the nolink option is set. |
| 55 | + */}} |
| 56 | + {{ $wrapper := newScratch }} |
| 57 | + {{ $wrapper.Set "open" (printf ` |
| 58 | + <a |
| 59 | + class="responsive-image %s" |
| 60 | + href="%s" |
| 61 | + target="_blank" |
| 62 | + rel="noopener noreferrer" |
| 63 | + aria-label="Open Image: %s" |
| 64 | + >` $classname $img.RelPermalink $altText | safeHTML) |
| 65 | + }} |
| 66 | + {{ $wrapper.Set "close" (`</a>` | safeHTML) }} |
| 67 | + {{ if eq $altText "" }} |
| 68 | + {{ $wrapper.Set "open" (printf ` |
| 69 | + <a |
| 70 | + class="responsive-image %s" |
| 71 | + href="%s" |
| 72 | + target="_blank" |
| 73 | + rel="noopener noreferrer" |
| 74 | + aria-hidden="true" |
| 75 | + >` $classname $img.RelPermalink | safeHTML) |
| 76 | + }} |
| 77 | + {{ end }} |
| 78 | + {{ if $nolink }} |
| 79 | + {{ $wrapper.Set "open" (printf ` |
| 80 | + <div |
| 81 | + class="responsive-image %s" |
| 82 | + >` $classname | safeHTML) |
| 83 | + }} |
| 84 | + {{ $wrapper.Set "close" (`</div>` | safeHTML) }} |
| 85 | + {{ end }} |
| 86 | + |
| 87 | + {{/* If the image is an SVG, just render it as is */}} |
| 88 | + {{ if eq $img.MediaType.SubType "svg" }} |
| 89 | + {{ $wrapper.Get "open" }} <style> |
| 90 | + #{{ $imageId }} { |
| 91 | + max-width: {{ $maxwidth }}; |
| 92 | + width: 100%; |
| 93 | + } |
| 94 | + </style> |
| 95 | + <img |
| 96 | + id="{{ $imageId }}" |
| 97 | + src="{{ $img.RelPermalink }}" |
| 98 | + alt="{{ $altText }}" |
| 99 | + loading="lazy" |
| 100 | + {{ if eq $altText "" }} |
| 101 | + aria-hidden="true" |
| 102 | + {{ end }} |
| 103 | + /> |
| 104 | + {{ $wrapper.Get "close" }} |
| 105 | + {{ else }} |
| 106 | + {{/* Create a low res copy for initial page load */}} |
| 107 | + {{ $blurred := $img.Resize "10x q10" }} |
| 108 | + |
| 109 | + {{/* Apply the outer wrapper element */}} |
| 110 | + {{ $wrapper.Get "open" }} |
| 111 | + |
| 112 | + {{/* |
| 113 | + If the image is a GIF, render it without sizes |
| 114 | + because changing the size will break the animation. |
| 115 | + */}} |
| 116 | + {{ if eq $img.MediaType.SubType "gif" }} |
| 117 | + <style> |
| 118 | + #{{ $imageId }} { |
| 119 | + aspect-ratio: {{ $img.Width }} / {{ $img.Height }}; |
| 120 | + max-width: {{ math.Min $img.Width $maxwidth }}px; |
| 121 | + object-fit: cover; |
| 122 | + object-position: center; |
| 123 | + width: 100%; |
| 124 | + } |
| 125 | + </style> |
| 126 | + <img |
| 127 | + id="{{ $imageId }}" |
| 128 | + src="{{ ($blurred.Resize (printf "%sx" (string $img.Width))).RelPermalink }}" |
| 129 | + data-lazy-src="{{ $img.RelPermalink }}" |
| 130 | + alt="{{ $altText }}" |
| 131 | + loading="lazy" |
| 132 | + {{ if eq $altText "" }} |
| 133 | + aria-hidden="true" |
| 134 | + {{ end }} |
| 135 | + /> |
| 136 | + {{ else }} |
| 137 | + {{/* |
| 138 | + Finally, render a picture element with sources for |
| 139 | + webp and jpeg formats. |
| 140 | + */}} |
| 141 | + <style> |
| 142 | + #{{ $imageId }} { |
| 143 | + aspect-ratio: {{ $img.Width }} / {{ $img.Height }}; |
| 144 | + max-width: {{ math.Min $maxwidth $img.Width }}px; |
| 145 | + width: 100%; |
| 146 | + object-fit: cover; |
| 147 | + object-position: center; |
| 148 | + } |
| 149 | + </style> |
| 150 | + <picture> |
| 151 | + <source |
| 152 | + type="image/webp" |
| 153 | + srcset=" |
| 154 | + {{ range $index, $width := $widths }} |
| 155 | + {{ $lineEnd := cond (eq (add $index 1) (len $widths)) "" "," }} |
| 156 | + {{ ($blurred.Resize (printf "%dx webp" $width)).RelPermalink }} |
| 157 | + {{ printf "%dw" $width }}{{ $lineEnd }} |
| 158 | + {{ end }} |
| 159 | + " |
| 160 | + data-lazy-srcset=" |
| 161 | + {{ range $index, $width := $widths }} |
| 162 | + {{ $lineEnd := cond (eq (add $index 1) (len $widths)) "" "," }} |
| 163 | + {{ ($img.Resize (printf "%dx webp" $width)).RelPermalink }} |
| 164 | + {{ printf "%dw" $width }}{{ $lineEnd }} |
| 165 | + {{ end }} |
| 166 | + " |
| 167 | + /> |
| 168 | + <img |
| 169 | + id="{{ $imageId }}" |
| 170 | + srcset=" |
| 171 | + {{ range $index, $width := $widths }} |
| 172 | + {{ $lineEnd := cond (eq (add $index 1) (len $widths)) "" "," }} |
| 173 | + {{ ($blurred.Resize (printf "%dx" $width)).RelPermalink }} |
| 174 | + {{ printf "%dw" $width }}{{ $lineEnd }} |
| 175 | + {{ end }} |
| 176 | + " |
| 177 | + data-lazy-srcset=" |
| 178 | + {{ range $index, $width := $widths }} |
| 179 | + {{ $lineEnd := cond (eq (add $index 1) (len $widths)) "" "," }} |
| 180 | + {{ ($img.Resize (printf "%dx" $width)).RelPermalink }} |
| 181 | + {{ printf "%dw" $width }}{{ $lineEnd }} |
| 182 | + {{ end }} |
| 183 | + " |
| 184 | + src="{{ ($blurred.Resize (printf "%dx" $minImageSize)).RelPermalink }}" |
| 185 | + data-lazy-src="{{ ($img.Resize (printf "%dx" $minImageSize)).RelPermalink }}" |
| 186 | + sizes="{{ $sizes }}" |
| 187 | + alt="{{ $altText }}" |
| 188 | + {{ if eq $altText "" }} |
| 189 | + aria-hidden="true" |
| 190 | + {{ end }} |
| 191 | + /> |
| 192 | + </picture> |
| 193 | + {{ end }} |
| 194 | + {{ $wrapper.Get "close" }} |
| 195 | + {{ end }} |
| 196 | +{{ else }} |
| 197 | + <p>could not find image</p> |
| 198 | +{{ end }} |
0 commit comments