mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-06-19 04:23:30 +08:00
support srcset for webp for cover/content images
This commit is contained in:
parent
4272da7ee4
commit
1f2702d769
@ -61,7 +61,7 @@ class StackGallery {
|
|||||||
/// because it can not detect whether image is being wrapped by a link or not
|
/// because it can not detect whether image is being wrapped by a link or not
|
||||||
/// and it lead to a invalid HTML construction (<a><figure><img></figure></a>)
|
/// and it lead to a invalid HTML construction (<a><figure><img></figure></a>)
|
||||||
|
|
||||||
const images = container.querySelectorAll('img.gallery-image');
|
const images = container.querySelectorAll('picture.gallery-image');
|
||||||
for (const img of Array.from(images)) {
|
for (const img of Array.from(images)) {
|
||||||
/// Images are wrapped with figure tag if the paragraph has only images without texts
|
/// Images are wrapped with figure tag if the paragraph has only images without texts
|
||||||
/// This is done to allow inline images within paragraphs
|
/// This is done to allow inline images within paragraphs
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
{{/* SVG and external images won't work with gallery layout, because their width and height attributes are unknown */}}
|
{{/* SVG and external images won't work with gallery layout, because their width and height attributes are unknown */}}
|
||||||
{{- $galleryImage := false -}}
|
{{- $galleryImage := false -}}
|
||||||
|
|
||||||
|
<picture
|
||||||
|
|
||||||
{{- if $image -}}
|
{{- if $image -}}
|
||||||
{{- $notSVG := ne (path.Ext .Destination) ".svg" -}}
|
{{- $notSVG := ne (path.Ext .Destination) ".svg" -}}
|
||||||
{{- $Permalink = $image.RelPermalink -}}
|
{{- $Permalink = $image.RelPermalink -}}
|
||||||
@ -16,8 +18,33 @@
|
|||||||
{{- $Width = $image.Width -}}
|
{{- $Width = $image.Width -}}
|
||||||
{{- $Height = $image.Height -}}
|
{{- $Height = $image.Height -}}
|
||||||
{{- $galleryImage = true -}}
|
{{- $galleryImage = true -}}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
{{ if $galleryImage }}
|
||||||
|
class="gallery-image"
|
||||||
|
data-flex-grow="{{ div (mul $image.Width 100) $image.Height }}"
|
||||||
|
data-flex-basis="{{ div (mul $image.Width 240) $image.Height }}px"
|
||||||
|
{{ with $alt }}
|
||||||
|
alt="{{ . }}"
|
||||||
|
{{ end }}
|
||||||
|
{{ end }}
|
||||||
|
{{- end -}}
|
||||||
|
|
||||||
|
>
|
||||||
|
|
||||||
|
{{- if $image -}}
|
||||||
|
{{- $notSVG := ne (path.Ext .Destination) ".svg" -}}
|
||||||
|
{{- $Permalink = $image.RelPermalink -}}
|
||||||
|
{{- if $notSVG -}}
|
||||||
{{- if (default true .Page.Site.Params.imageProcessing.content.enabled) -}}
|
{{- if (default true .Page.Site.Params.imageProcessing.content.enabled) -}}
|
||||||
|
{{- $small := $image.Resize `webp 480x` -}}
|
||||||
|
{{- $big := $image.Resize `webp 1024x` -}}
|
||||||
|
{{- $Srcset = printf `%s 480w, %s 1024w` $small.RelPermalink $big.RelPermalink -}}
|
||||||
|
|
||||||
|
<source type="image/webp"
|
||||||
|
srcset="{{ $Srcset }}"
|
||||||
|
>
|
||||||
|
|
||||||
{{- $small := $image.Resize `480x` -}}
|
{{- $small := $image.Resize `480x` -}}
|
||||||
{{- $big := $image.Resize `1024x` -}}
|
{{- $big := $image.Resize `1024x` -}}
|
||||||
{{- $Srcset = printf `%s 480w, %s 1024w` $small.RelPermalink $big.RelPermalink -}}
|
{{- $Srcset = printf `%s 480w, %s 1024w` $small.RelPermalink $big.RelPermalink -}}
|
||||||
@ -25,17 +52,13 @@
|
|||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
||||||
|
|
||||||
<img src="{{ $Permalink }}"
|
<img src="{{ $Permalink }}"
|
||||||
{{ with $Width }}width="{{ . }}"{{ end }}
|
{{ with $Width }}width="{{ . }}"{{ end }}
|
||||||
{{ with $Height }}height="{{ . }}"{{ end }}
|
{{ with $Height }}height="{{ . }}"{{ end }}
|
||||||
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
|
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
{{ with $alt }}
|
decoding="async"
|
||||||
alt="{{ . }}"
|
>
|
||||||
{{ end }}
|
|
||||||
{{ if $galleryImage }}
|
</picture>
|
||||||
class="gallery-image"
|
|
||||||
data-flex-grow="{{ div (mul $image.Width 100) $image.Height }}"
|
|
||||||
data-flex-basis="{{ div (mul $image.Width 240) $image.Height }}px"
|
|
||||||
{{ end }}
|
|
||||||
>
|
|
@ -9,7 +9,12 @@
|
|||||||
{{- $Height := $image.resource.Height -}}
|
{{- $Height := $image.resource.Height -}}
|
||||||
{{- $Srcset := "" -}}
|
{{- $Srcset := "" -}}
|
||||||
|
|
||||||
|
<picture>
|
||||||
{{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
{{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
||||||
|
{{- $thumbnail := $image.resource.Resize "webp 800x" -}}
|
||||||
|
{{- $thumbnailRetina := $image.resource.Resize "webp 1600x" -}}
|
||||||
|
{{- $Srcset = printf "%s 800w, %s 1600w" $thumbnail.RelPermalink $thumbnailRetina.RelPermalink -}}
|
||||||
|
<source type="image/webp" srcset="{{ $Srcset }}" />
|
||||||
{{- $thumbnail := $image.resource.Resize "800x" -}}
|
{{- $thumbnail := $image.resource.Resize "800x" -}}
|
||||||
{{- $thumbnailRetina := $image.resource.Resize "1600x" -}}
|
{{- $thumbnailRetina := $image.resource.Resize "1600x" -}}
|
||||||
{{- $Srcset = printf "%s 800w, %s 1600w" $thumbnail.RelPermalink $thumbnailRetina.RelPermalink -}}
|
{{- $Srcset = printf "%s 800w, %s 1600w" $thumbnail.RelPermalink $thumbnailRetina.RelPermalink -}}
|
||||||
@ -22,8 +27,9 @@
|
|||||||
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
|
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
|
||||||
width="{{ $Width }}"
|
width="{{ $Width }}"
|
||||||
height="{{ $Height }}"
|
height="{{ $Height }}"
|
||||||
loading="lazy"
|
decoding="async"
|
||||||
alt="Featured image of post {{ .Title }}" />
|
alt="Featured image of post {{ .Title }}" />
|
||||||
|
</picture>
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" />
|
<img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" />
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
Loading…
Reference in New Issue
Block a user