Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the width and height for thumbnails #590

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions layouts/partials/post/thumbnail.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,30 @@
{{- $img := index .Params.images 0 -}}
{{- $ext := $img | path.Ext }}
{{- $url := urls.Parse $img -}}
{{- $width := false -}}
{{- $height := false }}
{{- $validExts := slice ".svg" ".avif" -}}
{{- if not $url.Scheme -}}
{{- range $formats -}}
{{- $filename := replace $img $ext (printf ".%s" .) -}}
{{- if and (not (strings.HasSuffix $ext .)) (fileExists (path.Join "static" $filename)) -}}
<source srcset="{{ $filename | absURL }}" type="image/{{ . }}" />
{{- end -}}
{{- end -}}
{{- $filename := path.Join "static" $url.Path -}}
{{- if fileExists $filename -}}
{{- if not (in $validExts $ext) -}}
{{- $config := imageConfig $filename -}}
{{- $width = $config.Width -}}
{{- $height = $config.Height -}}
{{- end -}}
{{- else -}}
{{- warnf "no such image can be found in page resources and the static folder: %s" .filename -}}
{{- end -}}
{{- end -}}
<img class="img-fluid" alt="{{ .Title }}" src="{{ $img | absURL }}" loading="lazy"/>
<img class="img-fluid" alt="{{ .Title }}" src="{{ $img | absURL }}" loading="lazy"
{{ with $width }} width="{{ . }}"{{ end }}
{{ with $height}} height="{{ . }}"{{ end }}/>
</picture>
{{- else -}}
{{- $images := .Resources.ByType "image" -}}
Expand All @@ -22,8 +37,8 @@
{{- $smallImage := $featured.Resize "180x" -}}
{{- $largeImage := $featured.Resize "500x" -}}
<picture>
<source srcset="{{ $largeImage.Permalink }}" media="(max-width: 576px)">
<img class="img-fluid" alt="{{ .Title }}" src="{{ $smallImage.Permalink }}" data-src="{{ $featured.Permalink }}" loading="lazy"/>
<source srcset="{{ $largeImage.Permalink }}" media="(max-width: 576px)" height="{{ $largeImage.Height }}" width="{{ $largeImage.Width }}">
<img class="img-fluid" height="{{ $smallImage.Height }}" width="{{ $smallImage.Width }}" alt="{{ .Title }}" src="{{ $smallImage.Permalink }}" data-src="{{ $featured.Permalink }}" loading="lazy"/>
</picture>
{{- end -}}
{{- end -}}