hugo-PaperMod/layouts/partials/cover.html

66 lines
3.0 KiB
HTML
Raw Normal View History

{{- with .cxt}} {{/* Apply proper context from dict */}}
{{- if (and .Params.cover.image (not $.isHidden)) }}
<figure class="entry-cover">
2024-11-09 09:51:33 -05:00
{{- $loading := cond $.IsSingle "eager" "lazy" }}
2023-12-16 02:39:19 -05:00
{{- $addLink := (and site.Params.cover.linkFullImages $.IsSingle) }}
2024-11-09 09:59:08 -05:00
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
{{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }}
{{- $responsiveImages := (.Params.cover.responsiveImages | default site.Params.cover.responsiveImages) | default true }}
{{- $pageBundleCover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
{{- $globalResourcesCover := (resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
{{- $cover := (or $pageBundleCover $globalResourcesCover)}}
2024-11-09 10:35:08 -05:00
{{- /* We are not using the .Param.cover.relative to decide the location of image */}}
{{- /* If we have the image in pageBundle or globalResources we can process the image */}}
2024-11-09 09:55:03 -05:00
2024-11-09 09:59:08 -05:00
{{- $sizes := (slice "360" "480" "720" "1080" "1500") }}
{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
{{- if hugo.IsExtended -}}
{{- $processableFormats = $processableFormats | append "webp" -}}
{{- end -}}
2024-11-09 10:17:29 -05:00
{{- $imgdl := (.Params.cover.image) | absURL }}
{{- if $cover -}}
{{- $imgdl = $cover.Permalink }}
{{- end -}}
2024-11-09 09:55:03 -05:00
{{- if $addLink }}
2024-11-09 10:17:29 -05:00
<a href="{{ $imgdl }}" target="_blank" rel="noopener noreferrer">
2024-11-09 09:55:03 -05:00
{{- end }}
2024-11-09 10:22:28 -05:00
{{- if $cover -}}
{{/* i.e it is present in page bundle */}}
{{- if (and (in $processableFormats $cover.MediaType.SubType) ($responsiveImages) (eq $prod true)) }}
2024-11-09 10:18:49 -05:00
<img loading="{{$loading}}"
srcset='{{- range $size := $sizes -}}
{{- if (ge $cover.Width $size) }}
{{- printf "%s %s" (($cover.Resize (printf "%sx" $size)).Permalink) (printf "%sw," $size) }}
{{- end }}
{{- end }}
{{- printf "%s %dw" ($cover.Permalink) ($cover.Width) }}'
src="{{ $cover.Permalink }}"
sizes="(min-width: 768px) 720px, 100vw"
width="{{ $cover.Width }}" height="{{ $cover.Height }}"
alt="{{ $alt }}">
{{- else }}{{/* Unprocessable image or responsive images disabled */}}
2024-11-09 10:18:49 -05:00
<img loading="{{ $loading }}" src="{{ $imgdl }}" alt="{{ $alt }}">
{{- end }}
2024-11-09 10:22:28 -05:00
{{- else }}
2024-11-09 10:35:08 -05:00
{{- /* For absolute urls and external links, no img processing here */}}
2024-11-09 10:17:29 -05:00
<img loading="{{ $loading }}" src="{{ $imgdl }}" alt="{{ $alt }}">
{{- end }}
2024-11-09 09:55:03 -05:00
{{- if $addLink }}
</a>
2024-11-09 10:35:08 -05:00
{{- end -}}
2024-11-09 09:55:03 -05:00
2024-11-09 10:35:08 -05:00
{{- /* Display Caption */}}
2023-12-16 02:39:19 -05:00
{{- if $.IsSingle }}
2024-11-09 10:35:08 -05:00
{{ with .Params.cover.caption -}}
<figcaption>{{ . | markdownify }}</figcaption>
2024-11-09 09:55:03 -05:00
{{- end }}
{{- end }}
</figure>
{{- end }}{{/* End image */}}
2021-03-30 08:37:36 -04:00
{{- end -}}{{/* End context */ -}}