refactor: image handling logic + support for external images (#1235)
* refactor: simplify image partial logic, remove generation of different image sizes * Remove `defaultImage.opengraph` configuration from `params.toml`.
This commit is contained in:
@@ -36,10 +36,6 @@ rssFullContent = true
|
|||||||
site = ""
|
site = ""
|
||||||
card = "summary_large_image"
|
card = "summary_large_image"
|
||||||
|
|
||||||
[defaultImage.opengraph]
|
|
||||||
enabled = false
|
|
||||||
local = false
|
|
||||||
|
|
||||||
[colorScheme]
|
[colorScheme]
|
||||||
toggle = true
|
toggle = true
|
||||||
default = "auto"
|
default = "auto"
|
||||||
|
|||||||
@@ -11,29 +11,11 @@
|
|||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{- $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{ if $image.exists }}
|
{{ if $image }}
|
||||||
<div class="article-image">
|
<div class="article-image">
|
||||||
{{ if $image.resource }}
|
<img src="{{ $image.Permalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" alt="{{ .Title }}"
|
||||||
{{- $Permalink := $image.resource.RelPermalink -}}
|
|
||||||
{{- $Width := $image.resource.Width -}}
|
|
||||||
{{- $Height := $image.resource.Height -}}
|
|
||||||
|
|
||||||
{{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
|
||||||
{{- $thumbnail := $image.resource.Fill "120x120" -}}
|
|
||||||
{{- $Permalink = $thumbnail.RelPermalink -}}
|
|
||||||
{{- $Width = $thumbnail.Width -}}
|
|
||||||
{{- $Height = $thumbnail.Height -}}
|
|
||||||
{{- end -}}
|
|
||||||
|
|
||||||
<img src="{{ $Permalink }}"
|
|
||||||
width="{{ $Width }}"
|
|
||||||
height="{{ $Height }}"
|
|
||||||
alt="{{ .Title }}"
|
|
||||||
loading="lazy">
|
loading="lazy">
|
||||||
{{ else }}
|
|
||||||
<img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" />
|
|
||||||
{{ end }}
|
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
{{ $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" }}
|
{{ $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) }}
|
||||||
<article class="{{ if $image.exists }}has-image{{ end }}">
|
<article class="{{ if $image }}has-image{{ end }}">
|
||||||
{{ partial "article/components/header" . }}
|
{{ partial "article/components/header" . }}
|
||||||
</article>
|
</article>
|
||||||
@@ -1,32 +1,10 @@
|
|||||||
{{ $image := partialCached "helper/image" (dict "Context" .context "Type" .Type) .context.RelPermalink .Type }}
|
{{ $image := partial "helper/image" (dict "Image" .context.Params.image "Resources" .context.Resources) }}
|
||||||
<article class="{{ if $image.exists }}has-image{{ end }}">
|
<article class="{{ if $image }}has-image{{ end }}">
|
||||||
<a href="{{ .context.RelPermalink }}">
|
<a href="{{ .context.RelPermalink }}">
|
||||||
|
{{ if $image }}
|
||||||
{{ if $image.exists }}
|
|
||||||
<div class="article-image">
|
<div class="article-image">
|
||||||
{{ if $image.resource }}
|
<img src="{{ $image.Permalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" loading="lazy"
|
||||||
{{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}}
|
alt="Featured image of post {{ .context.Title }}">
|
||||||
{{- $Permalink := $imageRaw.RelPermalink -}}
|
|
||||||
{{- $Width := $imageRaw.Width -}}
|
|
||||||
{{- $Height := $imageRaw.Height -}}
|
|
||||||
|
|
||||||
{{- if .context.Site.Params.imageProcessing.cover.enabled -}}
|
|
||||||
{{- $thumbnail := $imageRaw.Fill .size -}}
|
|
||||||
{{- $Permalink = $thumbnail.RelPermalink -}}
|
|
||||||
{{- $Width = $thumbnail.Width -}}
|
|
||||||
{{- $Height = $thumbnail.Height -}}
|
|
||||||
{{- end -}}
|
|
||||||
|
|
||||||
<img src="{{ $Permalink }}"
|
|
||||||
width="{{ $Width }}"
|
|
||||||
height="{{ $Height }}"
|
|
||||||
loading="lazy"
|
|
||||||
alt="Featured image of post {{ .context.Title }}"
|
|
||||||
{{ with .context.Slug }}data-key="{{ . }}" {{ end }}
|
|
||||||
data-hash="{{ $imageRaw.Data.Integrity }}">
|
|
||||||
{{ else }}
|
|
||||||
<img src="{{ $image.permalink }}" loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $image.permalink }}"/>
|
|
||||||
{{ end }}
|
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,10 @@
|
|||||||
<header class="article-header">
|
<header class="article-header">
|
||||||
{{- $image := partialCached "helper/image" (dict "Context" . "Type" "article") .RelPermalink "article" -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{ if $image.exists }}
|
{{ if $image }}
|
||||||
<div class="article-image">
|
<div class="article-image">
|
||||||
<a href="{{ .RelPermalink }}">
|
<a href="{{ .RelPermalink }}">
|
||||||
{{ if $image.resource }}
|
<img src="{{ $image.Permalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" loading="lazy"
|
||||||
{{- $Permalink := $image.resource.RelPermalink -}}
|
|
||||||
{{- $Width := $image.resource.Width -}}
|
|
||||||
{{- $Height := $image.resource.Height -}}
|
|
||||||
{{- $Srcset := "" -}}
|
|
||||||
|
|
||||||
{{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
|
||||||
{{- $thumbnail := $image.resource.Resize "800x" -}}
|
|
||||||
{{- $thumbnailRetina := $image.resource.Resize "1600x" -}}
|
|
||||||
{{- $Srcset = printf "%s 800w, %s 1600w" $thumbnail.RelPermalink $thumbnailRetina.RelPermalink -}}
|
|
||||||
{{- $Permalink = $thumbnail.RelPermalink -}}
|
|
||||||
{{- $Width = $thumbnail.Width -}}
|
|
||||||
{{- $Height = $thumbnail.Height -}}
|
|
||||||
{{- end -}}
|
|
||||||
|
|
||||||
<img src="{{ $Permalink }}"
|
|
||||||
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
|
|
||||||
width="{{ $Width }}"
|
|
||||||
height="{{ $Height }}"
|
|
||||||
loading="lazy"
|
|
||||||
alt="Featured image of post {{ .Title }}" />
|
alt="Featured image of post {{ .Title }}" />
|
||||||
{{ else }}
|
|
||||||
<img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" />
|
|
||||||
{{ end }}
|
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@
|
|||||||
{{- end -}}
|
{{- end -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
||||||
{{ $image := partialCached "helper/image" (dict "Context" . "Type" "opengraph") .RelPermalink "opengraph" }}
|
{{ $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) }}
|
||||||
{{- if $image.exists -}}
|
{{- if $image -}}
|
||||||
<meta property='og:image' content='{{ absURL $image.permalink }}' />
|
<meta property='og:image' content='{{ absURL $image.permalink }}' />
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
@@ -9,8 +9,8 @@
|
|||||||
<meta name="twitter:title" {{ printf "content=%q" $title | safeHTMLAttr }}>
|
<meta name="twitter:title" {{ printf "content=%q" $title | safeHTMLAttr }}>
|
||||||
<meta name="twitter:description" {{ printf "content=%q" $description | safeHTMLAttr }}>
|
<meta name="twitter:description" {{ printf "content=%q" $description | safeHTMLAttr }}>
|
||||||
|
|
||||||
{{- $image := partialCached "helper/image" (dict "Context" . "Type" "opengraph") .RelPermalink "opengraph" -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{- if $image.exists -}}
|
{{- if $image -}}
|
||||||
<meta name="twitter:card" content="{{ default `summary_large_image` .Site.Params.opengraph.twitter.card }}">
|
<meta name="twitter:card" content="{{ default `summary_large_image` .Site.Params.opengraph.twitter.card }}">
|
||||||
<meta name="twitter:image" content='{{ absURL $image.permalink }}' />
|
<meta name="twitter:image" content='{{ absURL $image.permalink }}' />
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
@@ -1,61 +1,54 @@
|
|||||||
{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }}
|
/// Params:
|
||||||
{{ $imageField := default "image" .Context.Site.Params.featuredImageField }}
|
/// Resources: Where to search for images
|
||||||
{{ $imageValue := index .Context.Params $imageField }}
|
/// Image: Image URL
|
||||||
|
|
||||||
{{ if $imageValue }}
|
/// Returns:
|
||||||
<!-- If page has `image` field set -->
|
/// Local: true if the image is local, false if it is remote
|
||||||
{{ $result = merge $result (dict "exists" true) }}
|
/// Resource: Hugo image resource object, not nil if the image is local
|
||||||
{{ $url := urls.Parse $imageValue }}
|
/// Permalink: Image URL
|
||||||
|
/// Height: Image height
|
||||||
|
/// Width: Image width
|
||||||
|
|
||||||
{{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }}
|
{{ $result := dict
|
||||||
<!-- Is an external image -->
|
"Local" false
|
||||||
{{ $result = merge $result (dict "permalink" $imageValue) }}
|
"Resource" nil
|
||||||
|
"Permalink" (.Image | absURL)
|
||||||
|
"Width" nil
|
||||||
|
"Height" nil
|
||||||
|
}}
|
||||||
|
|
||||||
|
{{ if not .Image }}
|
||||||
|
{{ $result = "" }}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
{{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageValue | safeURL)) }}
|
{{ $url := urls.Parse .Image }}
|
||||||
|
{{ if not (in (slice "https" "http") $url.Scheme) }}
|
||||||
{{ if $pageResourceImage }}
|
{{ $resource := .Resources.Get (printf "%s" (.Image | safeURL)) }}
|
||||||
<!-- If image is found under page bundle -->
|
{{ if $resource }}
|
||||||
{{ $result = merge $result (dict "permalink" $pageResourceImage.RelPermalink) }}
|
{{ $result = dict
|
||||||
|
"Local" true
|
||||||
<!-- Disable SVG image processing, not supported by Hugo -->
|
"Resource" $resource
|
||||||
{{ if ne (path.Ext $imageValue) ".svg" }}
|
"Permalink" $resource.RelPermalink
|
||||||
{{ $result = merge $result (dict "resource" $pageResourceImage) }}
|
"Height" $resource.Height
|
||||||
|
"Width" $resource.Width
|
||||||
|
}}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<!-- Can not find the image under page bundle. Could be a relative linked image -->
|
{{ with try (resources.GetRemote $url) }}
|
||||||
{{ $result = merge $result (dict "permalink" (relURL $imageValue)) }}
|
{{ with .Err }}
|
||||||
{{ end }}
|
{{ errorf "%s" . }}
|
||||||
|
{{ else with .Value }}
|
||||||
{{ end }}
|
{{ $result = dict
|
||||||
|
"Local" false
|
||||||
{{ else if and (ne .Type nil) (index .Context.Site.Params.defaultImage .Type) }}
|
"Resource" .
|
||||||
<!-- Type arg is set, check for defaultImage setting -->
|
"Permalink" .RelPermalink
|
||||||
{{ $defaultImageSetting := index .Context.Site.Params.defaultImage .Type }}
|
"Height" .Height
|
||||||
|
"Width" .Width
|
||||||
{{ if $defaultImageSetting.enabled }}
|
}}
|
||||||
{{ $result = merge $result (dict "isDefault" true) }}
|
|
||||||
{{ $result = merge $result (dict "exists" true) }}
|
|
||||||
|
|
||||||
{{ if $defaultImageSetting.local }}
|
|
||||||
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($defaultImageSetting.src | safeURL)) }}
|
|
||||||
|
|
||||||
{{ if $siteResourceImage }}
|
|
||||||
<!-- Try search image under site's assets folder -->
|
|
||||||
{{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }}
|
|
||||||
{{ $result = merge $result (dict "resource" $siteResourceImage) }}
|
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<!-- Can not find the image -->
|
{{ errorf "Unable to get remote resource %q" $url }}
|
||||||
{{ errorf "Failed loading image: %q" $defaultImageSetting.src }}
|
|
||||||
{{ $result = merge $result (dict "exists" false) }}
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ else }}
|
|
||||||
<!-- External image -->
|
|
||||||
{{ $result = merge $result (dict "permalink" (relURL $defaultImageSetting.src)) }}
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ return $result }}
|
{{ return $result }}
|
||||||
+5
-20
@@ -17,28 +17,13 @@
|
|||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{- $image := partialCached "helper/image" (dict "Context" . "Type" "section") .RelPermalink "section" -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{ if $image.exists }}
|
{{ if $image }}
|
||||||
<div class="section-image">
|
<div class="section-image">
|
||||||
{{ if $image.resource }}
|
<img src="{{ $image.Permalink }}"
|
||||||
{{- $Permalink := $image.resource.RelPermalink -}}
|
width="{{ $image.Width }}"
|
||||||
{{- $Width := $image.resource.Width -}}
|
height="{{ $image.Height }}"
|
||||||
{{- $Height := $image.resource.Height -}}
|
|
||||||
|
|
||||||
{{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
|
||||||
{{- $thumbnail := $image.resource.Fill "120x120" -}}
|
|
||||||
{{- $Permalink = $thumbnail.RelPermalink -}}
|
|
||||||
{{- $Width = $thumbnail.Width -}}
|
|
||||||
{{- $Height = $thumbnail.Height -}}
|
|
||||||
{{- end -}}
|
|
||||||
|
|
||||||
<img src="{{ $Permalink }}"
|
|
||||||
width="{{ $Width }}"
|
|
||||||
height="{{ $Height }}"
|
|
||||||
loading="lazy">
|
loading="lazy">
|
||||||
{{ else }}
|
|
||||||
<img src="{{ $image.permalink }}" loading="lazy" />
|
|
||||||
{{ end }}
|
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,8 +6,8 @@
|
|||||||
{{- range $filtered -}}
|
{{- range $filtered -}}
|
||||||
{{- $data := dict "title" .Title "date" .Date "permalink" .RelPermalink "content" (.Plain) -}}
|
{{- $data := dict "title" .Title "date" .Date "permalink" .RelPermalink "content" (.Plain) -}}
|
||||||
|
|
||||||
{{- $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{- if $image.exists -}}
|
{{- if $image -}}
|
||||||
{{- $imagePermalink := "" -}}
|
{{- $imagePermalink := "" -}}
|
||||||
{{- if and $image.resource (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
{{- if and $image.resource (default true .Page.Site.Params.imageProcessing.cover.enabled) -}}
|
||||||
{{- $thumbnail := $image.resource.Fill "120x120" -}}
|
{{- $thumbnail := $image.resource.Fill "120x120" -}}
|
||||||
|
|||||||
+2
-2
@@ -38,8 +38,8 @@
|
|||||||
{{ with .Site.Params.Author.email }}<author>{{.}}{{ with $.Site.Params.Author.name }} ({{.}}){{end}}</author>{{end}}
|
{{ with .Site.Params.Author.email }}<author>{{.}}{{ with $.Site.Params.Author.name }} ({{.}}){{end}}</author>{{end}}
|
||||||
<guid>{{ .Permalink }}</guid>
|
<guid>{{ .Permalink }}</guid>
|
||||||
<description>
|
<description>
|
||||||
{{- $image := partial "helper/image" (dict "Context" . "Type" "rss") -}}
|
{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}}
|
||||||
{{- if $image.exists -}}
|
{{- if $image -}}
|
||||||
{{ "<" | html }}img src="{{ $image.permalink | absURL }}" alt="Featured image of post {{ .Title }}" {{ "/>" | html}}
|
{{ "<" | html }}img src="{{ $image.permalink | absURL }}" alt="Featured image of post {{ .Title }}" {{ "/>" | html}}
|
||||||
{{- end -}}{{ $content }}</description>
|
{{- end -}}{{ $content }}</description>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user