From 72615b6d49ab8b102e92e2e487ab420f41ba9223 Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Sat, 8 Mar 2025 23:30:00 +0530 Subject: [PATCH] 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
for image captions * Add comments and manage whitespace * Remove pointer-event prevention from entry-cover image --- assets/css/common/post-entry.css | 1 - layouts/partials/cover.html | 84 ++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/assets/css/common/post-entry.css b/assets/css/common/post-entry.css index c3cba370..8ff32251 100644 --- a/assets/css/common/post-entry.css +++ b/assets/css/common/post-entry.css @@ -97,7 +97,6 @@ .entry-cover img { border-radius: var(--radius); - pointer-events: none; width: 100%; height: auto; } diff --git a/layouts/partials/cover.html b/layouts/partials/cover.html index e92b5557..9c1f9eaa 100644 --- a/layouts/partials/cover.html +++ b/layouts/partials/cover.html @@ -1,42 +1,64 @@ {{- with .cxt}} {{/* Apply proper context from dict */}} {{- if (and .Params.cover.image (not $.isHidden)) }} -{{- $alt := (.Params.cover.alt | default .Params.cover.caption | plainify) }} -{{- $loading := cond $.IsSingle "eager" "lazy" }}
- {{- $responsiveImages := (.Params.cover.responsiveImages | default site.Params.cover.responsiveImages) | default true }} + {{- $loading := cond $.IsSingle "eager" "lazy" }} {{- $addLink := (and site.Params.cover.linkFullImages $.IsSingle) }} - {{- $pageBundleCover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }} + {{- $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)}} - {{- if $cover -}}{{/* i.e it is present in page bundle */}} - {{- if $addLink }}{{ end -}} - {{- $sizes := (slice "360" "480" "720" "1080" "1500") }} - {{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}} - {{- if hugo.IsExtended -}} - {{- $processableFormats = $processableFormats | append "webp" -}} - {{- end -}} - {{- $prod := (hugo.IsProduction | or (eq site.Params.env "production")) }} - {{- if (and (in $processableFormats $cover.MediaType.SubType) ($responsiveImages) (eq $prod true)) }} - {{ $alt }} - {{- else }}{{/* Unprocessable image or responsive images disabled */}} - {{ $alt }} - {{- end }} - {{- else }}{{/* For absolute urls and external links, no img processing here */}} - {{- if $addLink }}{{ end -}} - {{ $alt }} + {{- /* 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 */}} + + {{- $sizes := (slice "360" "480" "720" "1080" "1500") }} + {{- $processableFormats := (slice "jpg" "jpeg" "png" "tif" "bmp" "gif") -}} + {{- if hugo.IsExtended -}} + {{- $processableFormats = $processableFormats | append "webp" -}} + {{- end -}} + + {{- $imgdl := (.Params.cover.image) | absURL }} + {{- if $cover -}} + {{- $imgdl = $cover.Permalink }} + {{- end -}} + + {{- if $addLink }} + {{- end }} - {{- if $addLink }}{{ end -}} - {{/* Display Caption */}} + + {{- if $cover -}} + {{/* i.e it is present in page bundle */}} + {{- if (and (in $processableFormats $cover.MediaType.SubType) ($responsiveImages) (eq $prod true)) }} + {{ $alt }} + {{- else }}{{/* Unprocessable image or responsive images disabled */}} + {{ $alt }} + {{- end }} + {{- else }} + {{- /* For absolute urls and external links, no img processing here */}} + {{ $alt }} + {{- end }} + + {{- if $addLink }} + + {{- end -}} + + {{- /* Display Caption */}} {{- if $.IsSingle }} - {{ with .Params.cover.caption }}

{{ . | markdownify }}

{{- end }} + {{ with .Params.cover.caption -}} +
{{ . | markdownify }}
+ {{- end }} {{- end }}
{{- end }}{{/* End image */}}