Skip to content

Commit 72615b6

Browse files
Feat/improve cover image gen (#1628)
* Refactor cover.html * Refactor 2 * Refactor 3 * Refactor 4 * Refactor 5 * add comments and refactor more * Refactor cover.html to use <figcaption> for image captions * Add comments and manage whitespace * Remove pointer-event prevention from entry-cover image
1 parent e2e1011 commit 72615b6

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

assets/css/common/post-entry.css

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797

9898
.entry-cover img {
9999
border-radius: var(--radius);
100-
pointer-events: none;
101100
width: 100%;
102101
height: auto;
103102
}

layouts/partials/cover.html

+50-28
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,64 @@
11
{{- with .cxt}} {{/* Apply proper context from dict */}}
22
{{- if (and .Params.cover.image (not $.isHidden)) }}
3-
{{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }}
4-
{{- $loading := cond $.IsSingle "eager" "lazy" }}
53
<figure class="entry-cover">
6-
{{- $responsiveImages := (.Params.cover.responsiveImages | default site.Params.cover.responsiveImages) | default true }}
4+
{{- $loading := cond $.IsSingle "eager" "lazy" }}
75
{{- $addLink := (and site.Params.cover.linkFullImages $.IsSingle) }}
8-
{{- $pageBundleCover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
6+
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
7+
{{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }}
8+
{{- $responsiveImages := (.Params.cover.responsiveImages | default site.Params.cover.responsiveImages) | default true }}
9+
10+
{{- $pageBundleCover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
911
{{- $globalResourcesCover := (resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
1012
{{- $cover := (or $pageBundleCover $globalResourcesCover)}}
11-
{{- if $cover -}}{{/* i.e it is present in page bundle */}}
12-
{{- if $addLink }}<a href="{{ (path.Join .RelPermalink .Params.cover.image) | absURL }}" target="_blank"
13-
rel="noopener noreferrer">{{ end -}}
14-
{{- $sizes := (slice "360" "480" "720" "1080" "1500") }}
15-
{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
16-
{{- if hugo.IsExtended -}}
17-
{{- $processableFormats = $processableFormats | append "webp" -}}
18-
{{- end -}}
19-
{{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }}
13+
{{- /* We are not using the .Param.cover.relative to decide the location of image */}}
14+
{{- /* If we have the image in pageBundle or globalResources we can process the image */}}
15+
16+
{{- $sizes := (slice "360" "480" "720" "1080" "1500") }}
17+
{{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}}
18+
{{- if hugo.IsExtended -}}
19+
{{- $processableFormats = $processableFormats | append "webp" -}}
20+
{{- end -}}
21+
22+
{{- $imgdl := (.Params.cover.image) | absURL }}
23+
{{- if $cover -}}
24+
{{- $imgdl = $cover.Permalink }}
25+
{{- end -}}
26+
27+
{{- if $addLink }}
28+
<a href="{{ $imgdl }}" target="_blank" rel="noopener noreferrer">
29+
{{- end }}
30+
31+
{{- if $cover -}}
32+
{{/* i.e it is present in page bundle */}}
2033
{{- if (and (in $processableFormats $cover.MediaType.SubType) ($responsiveImages) (eq $prod true)) }}
21-
<img loading="{{$loading}}" srcset="{{- range $size := $sizes -}}
22-
{{- if (ge $cover.Width $size) -}}
23-
{{ printf "%s %s" (($cover.Resize (printf "%sx" $size)).Permalink) (printf "%sw ," $size) -}}
24-
{{ end }}
25-
{{- end -}}{{$cover.Permalink }} {{printf "%dw" ($cover.Width)}}"
26-
sizes="(min-width: 768px) 720px, 100vw" src="{{ $cover.Permalink }}" alt="{{ $alt }}"
27-
width="{{ $cover.Width }}" height="{{ $cover.Height }}">
34+
<img loading="{{$loading}}"
35+
srcset='{{- range $size := $sizes -}}
36+
{{- if (ge $cover.Width $size) }}
37+
{{- printf "%s %s" (($cover.Resize (printf "%sx" $size)).Permalink) (printf "%sw," $size) }}
38+
{{- end }}
39+
{{- end }}
40+
{{- printf "%s %dw" ($cover.Permalink) ($cover.Width) }}'
41+
src="{{ $cover.Permalink }}"
42+
sizes="(min-width: 768px) 720px, 100vw"
43+
width="{{ $cover.Width }}" height="{{ $cover.Height }}"
44+
alt="{{ $alt }}">
2845
{{- else }}{{/* Unprocessable image or responsive images disabled */}}
29-
<img loading="{{$loading}}" src="{{ (path.Join .RelPermalink .Params.cover.image) | absURL }}" alt="{{ $alt }}">
46+
<img loading="{{ $loading }}" src="{{ $imgdl }}" alt="{{ $alt }}">
3047
{{- end }}
31-
{{- else }}{{/* For absolute urls and external links, no img processing here */}}
32-
{{- if $addLink }}<a href="{{ (.Params.cover.image) | absURL }}" target="_blank"
33-
rel="noopener noreferrer">{{ end -}}
34-
<img loading="{{$loading}}" src="{{ (.Params.cover.image) | absURL }}" alt="{{ $alt }}">
48+
{{- else }}
49+
{{- /* For absolute urls and external links, no img processing here */}}
50+
<img loading="{{ $loading }}" src="{{ $imgdl }}" alt="{{ $alt }}">
3551
{{- end }}
36-
{{- if $addLink }}</a>{{ end -}}
37-
{{/* Display Caption */}}
52+
53+
{{- if $addLink }}
54+
</a>
55+
{{- end -}}
56+
57+
{{- /* Display Caption */}}
3858
{{- if $.IsSingle }}
39-
{{ with .Params.cover.caption }}<p>{{ . | markdownify }}</p>{{- end }}
59+
{{ with .Params.cover.caption -}}
60+
<figcaption>{{ . | markdownify }}</figcaption>
61+
{{- end }}
4062
{{- end }}
4163
</figure>
4264
{{- end }}{{/* End image */}}

0 commit comments

Comments
 (0)