diff --git a/config/_default/params.toml b/config/_default/params.toml index 85bd536..91a8c36 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -36,10 +36,6 @@ rssFullContent = true site = "" card = "summary_large_image" -[defaultImage.opengraph] - enabled = false - local = false - [colorScheme] toggle = true default = "auto" diff --git a/layouts/_partials/article-list/compact.html b/layouts/_partials/article-list/compact.html index f2784d6..7a4185c 100644 --- a/layouts/_partials/article-list/compact.html +++ b/layouts/_partials/article-list/compact.html @@ -11,30 +11,12 @@ - {{- $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" -}} - {{ if $image.exists }} -
- {{ if $image.resource }} - {{- $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 -}} - - {{ .Title }} - {{ else }} - Featured image of post {{ .Title }} - {{ end }} -
+ {{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}} + {{ if $image }} +
+ {{ .Title }} +
{{ end }} \ No newline at end of file diff --git a/layouts/_partials/article-list/default.html b/layouts/_partials/article-list/default.html index 02e0b30..df0e790 100644 --- a/layouts/_partials/article-list/default.html +++ b/layouts/_partials/article-list/default.html @@ -1,4 +1,4 @@ -{{ $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" }} -
+{{ $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) }} +
{{ partial "article/components/header" . }}
\ No newline at end of file diff --git a/layouts/_partials/article-list/tile.html b/layouts/_partials/article-list/tile.html index be5744b..5228a41 100644 --- a/layouts/_partials/article-list/tile.html +++ b/layouts/_partials/article-list/tile.html @@ -1,33 +1,11 @@ -{{ $image := partialCached "helper/image" (dict "Context" .context "Type" .Type) .context.RelPermalink .Type }} -
+{{ $image := partial "helper/image" (dict "Image" .context.Params.image "Resources" .context.Resources) }} +
- - {{ if $image.exists }} -
- {{ if $image.resource }} - {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}} - {{- $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 -}} - - Featured image of post {{ .context.Title }} - {{ else }} - - {{ end }} -
+ {{ if $image }} +
+ Featured image of post {{ .context.Title }} +
{{ end }}
@@ -36,4 +14,4 @@
-
+
\ No newline at end of file diff --git a/layouts/_partials/article/components/header.html b/layouts/_partials/article/components/header.html index 7582ec2..a6e8225 100644 --- a/layouts/_partials/article/components/header.html +++ b/layouts/_partials/article/components/header.html @@ -1,34 +1,12 @@
- {{- $image := partialCached "helper/image" (dict "Context" . "Type" "article") .RelPermalink "article" -}} - {{ if $image.exists }} -
- - {{ if $image.resource }} - {{- $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 -}} - - Featured image of post {{ .Title }} - {{ else }} - Featured image of post {{ .Title }} - {{ end }} - -
+ {{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}} + {{ if $image }} +
+ + Featured image of post {{ .Title }} + +
{{ end }} {{ partialCached "article/components/details" . .RelPermalink }} diff --git a/layouts/_partials/head/opengraph/provider/base.html b/layouts/_partials/head/opengraph/provider/base.html index 055745d..135c60c 100644 --- a/layouts/_partials/head/opengraph/provider/base.html +++ b/layouts/_partials/head/opengraph/provider/base.html @@ -37,7 +37,7 @@ {{- end -}} {{- end -}} -{{ $image := partialCached "helper/image" (dict "Context" . "Type" "opengraph") .RelPermalink "opengraph" }} -{{- if $image.exists -}} +{{ $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) }} +{{- if $image -}} {{- end -}} \ No newline at end of file diff --git a/layouts/_partials/head/opengraph/provider/twitter.html b/layouts/_partials/head/opengraph/provider/twitter.html index c39eba3..237932a 100644 --- a/layouts/_partials/head/opengraph/provider/twitter.html +++ b/layouts/_partials/head/opengraph/provider/twitter.html @@ -9,8 +9,8 @@ -{{- $image := partialCached "helper/image" (dict "Context" . "Type" "opengraph") .RelPermalink "opengraph" -}} -{{- if $image.exists -}} +{{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}} +{{- if $image -}} {{- end -}} \ No newline at end of file diff --git a/layouts/_partials/helper/image.html b/layouts/_partials/helper/image.html index 11fc3b6..c7ae2a2 100644 --- a/layouts/_partials/helper/image.html +++ b/layouts/_partials/helper/image.html @@ -1,61 +1,54 @@ -{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }} -{{ $imageField := default "image" .Context.Site.Params.featuredImageField }} -{{ $imageValue := index .Context.Params $imageField }} +/// Params: +/// Resources: Where to search for images +/// Image: Image URL -{{ if $imageValue }} - - {{ $result = merge $result (dict "exists" true) }} - {{ $url := urls.Parse $imageValue }} +/// Returns: +/// Local: true if the image is local, false if it is remote +/// Resource: Hugo image resource object, not nil if the image is local +/// Permalink: Image URL +/// Height: Image height +/// Width: Image width - {{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }} - - {{ $result = merge $result (dict "permalink" $imageValue) }} +{{ $result := dict + "Local" false + "Resource" nil + "Permalink" (.Image | absURL) + "Width" nil + "Height" nil +}} + +{{ if not .Image }} + {{ $result = "" }} +{{ else }} + {{ $url := urls.Parse .Image }} + {{ if not (in (slice "https" "http") $url.Scheme) }} + {{ $resource := .Resources.Get (printf "%s" (.Image | safeURL)) }} + {{ if $resource }} + {{ $result = dict + "Local" true + "Resource" $resource + "Permalink" $resource.RelPermalink + "Height" $resource.Height + "Width" $resource.Width + }} + {{ end }} {{ else }} - {{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageValue | safeURL)) }} - - {{ if $pageResourceImage }} - - {{ $result = merge $result (dict "permalink" $pageResourceImage.RelPermalink) }} - - - {{ if ne (path.Ext $imageValue) ".svg" }} - {{ $result = merge $result (dict "resource" $pageResourceImage) }} - {{ end }} - {{ else }} - - {{ $result = merge $result (dict "permalink" (relURL $imageValue)) }} - {{ end }} - - {{ end }} - -{{ else if and (ne .Type nil) (index .Context.Site.Params.defaultImage .Type) }} - - {{ $defaultImageSetting := index .Context.Site.Params.defaultImage .Type }} - - {{ 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 }} - - {{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }} - {{ $result = merge $result (dict "resource" $siteResourceImage) }} + {{ with try (resources.GetRemote $url) }} + {{ with .Err }} + {{ errorf "%s" . }} + {{ else with .Value }} + {{ $result = dict + "Local" false + "Resource" . + "Permalink" .RelPermalink + "Height" .Height + "Width" .Width + }} {{ else }} - - {{ errorf "Failed loading image: %q" $defaultImageSetting.src }} - {{ $result = merge $result (dict "exists" false) }} + {{ errorf "Unable to get remote resource %q" $url }} {{ end }} - - {{ else }} - - {{ $result = merge $result (dict "permalink" (relURL $defaultImageSetting.src)) }} - {{ end }} - + {{ end }} {{ end }} - {{ end }} -{{ return $result }} +{{ return $result }} \ No newline at end of file diff --git a/layouts/list.html b/layouts/list.html index d2ed9ea..26e2a61 100644 --- a/layouts/list.html +++ b/layouts/list.html @@ -17,28 +17,13 @@ {{ end }} - {{- $image := partialCached "helper/image" (dict "Context" . "Type" "section") .RelPermalink "section" -}} - {{ if $image.exists }} + {{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}} + {{ if $image }}
- {{ if $image.resource }} - {{- $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 -}} - - - {{ else }} - - {{ end }} +
{{ end }} diff --git a/layouts/page/search.json b/layouts/page/search.json index eafbdc6..237296e 100644 --- a/layouts/page/search.json +++ b/layouts/page/search.json @@ -6,8 +6,8 @@ {{- range $filtered -}} {{- $data := dict "title" .Title "date" .Date "permalink" .RelPermalink "content" (.Plain) -}} - {{- $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" -}} - {{- if $image.exists -}} + {{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}} + {{- if $image -}} {{- $imagePermalink := "" -}} {{- if and $image.resource (default true .Page.Site.Params.imageProcessing.cover.enabled) -}} {{- $thumbnail := $image.resource.Fill "120x120" -}} diff --git a/layouts/rss.xml b/layouts/rss.xml index 3d2e592..730467b 100644 --- a/layouts/rss.xml +++ b/layouts/rss.xml @@ -38,8 +38,8 @@ {{ with .Site.Params.Author.email }}{{.}}{{ with $.Site.Params.Author.name }} ({{.}}){{end}}{{end}} {{ .Permalink }} - {{- $image := partial "helper/image" (dict "Context" . "Type" "rss") -}} - {{- if $image.exists -}} + {{- $image := partial "helper/image" (dict "Image" .Params.image "Resources" .Resources) -}} + {{- if $image -}} {{ "<" | html }}img src="{{ $image.permalink | absURL }}" alt="Featured image of post {{ .Title }}" {{ "/>" | html}} {{- end -}}{{ $content }}