Skip to content

Commit 15d90b9

Browse files
committed
feat: Intercept the md image render and replace with a version that loads a scaled webp
1 parent ad48265 commit 15d90b9

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{ $alt := .Text | default "" }}
2+
{{ $title := .Title | default $alt }}
3+
{{ $src := .Destination | safeURL }}
4+
{{ with .Page.Resources.GetMatch $src }}
5+
<figure>
6+
{{ partial "_images/picture.html" (dict "src" . "alt" $alt "title" $title) }}
7+
{{ with $alt }}<figcaption>
8+
<p style="text-transform: uppercase; font-size: x-small;">{{- . -}}</p>
9+
</figcaption>{{ end }}
10+
</figure>
11+
{{ end }}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{ $originalImage := .src }}
2+
{{ $class := .class }}
3+
{{ $lazy := .lazy | default true }}
4+
{{ $caption := .alt | default "" }}
5+
6+
{{ $w := float $originalImage.Width }}
7+
{{ $h := float $originalImage.Height }}
8+
{{ $aspectRatio := div $w $h }}
9+
10+
{{ $media_query_widths := slice 1400 1200 992 768 576 480 360}}
11+
12+
<picture>
13+
{{ range $media_query_widths }}
14+
{{ $nw := . }}
15+
{{ $nh := div $nw $aspectRatio | int }}
16+
{{ $img_webp := $originalImage.Resize (printf "%dx%d webp q100" $nw $nh) }}
17+
<source media="(min-width:{{$nw}}px)" srcset="{{ $img_webp.Permalink }}" type="image/webp">
18+
{{end}}
19+
<img {{with $lazy}} loading="lazy" class="img-fluid" data-src="{{$originalImage.RelPermalink}}" {{else}}
20+
src="{{$originalImage.RelPermalink}}" {{end}} alt="{{ $caption }}">
21+
</picture>

0 commit comments

Comments
 (0)