From 2ad65a4c07de1c27e7967e82e9d1ec20d6d21a5e Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Wed, 23 Dec 2020 21:02:35 +0100 Subject: [PATCH] feat: add option to disable image processing (#83) * feat: add option to disable image processing closes https://github.com/CaiJimmy/hugo-theme-stack/issues/37 * refactor(list): use article/components/header inside default.html * refactor: use .Fill for cover image --- exampleSite/config.yaml | 6 ++++ layouts/_default/_markup/render-image.html | 28 +++++++++------ layouts/_default/list.html | 22 +++++++++--- layouts/page/search.json | 8 +++-- layouts/partials/article-list/compact.html | 18 ++++++++-- layouts/partials/article-list/default.html | 20 +---------- layouts/partials/article-list/tile.html | 19 +++++++++-- .../partials/article/components/header.html | 34 ++++++++++++++----- 8 files changed, 104 insertions(+), 51 deletions(-) diff --git a/exampleSite/config.yaml b/exampleSite/config.yaml index f06128d..a0266c8 100644 --- a/exampleSite/config.yaml +++ b/exampleSite/config.yaml @@ -86,6 +86,12 @@ params: # Available values: auto, light, dark default: auto + imageProcessing: + cover: + enabled: true + content: + enabled: true + menu: main: - identifier: home diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index d90cd90..21f650a 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -1,19 +1,27 @@ {{- $image := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) -}} {{- if $image -}} - {{- $small := $image.Resize "480x" -}} - {{- $big := $image.Resize "1024x" -}} {{- $alt := .PlainText | safeHTML -}} - {{- $caption := "" -}} - {{- with $alt -}} - {{- $caption = . | safeHTML -}} - {{- end -}}
- {{ if $alt }}{{ $alt }}{{ else if $caption }}{{ $caption | markdownify | plainify }}{{ else }} {{ end }} + {{- $Permalink := $image.RelPermalink -}} + {{- $Width := $image.Width -}} + {{- $Height := $image.Height -}} + {{- $Srcset := "" -}} + + {{- if (default true .Page.Site.Params.imageProcessing.content.enabled) -}} + {{- $small := $image.Resize "480x" -}} + {{- $big := $image.Resize "1024x" -}} + {{- $Srcset = printf "%s 480w, %s 1024w" $small.RelPermalink $big.RelPermalink -}} + {{- end -}} + + - {{ with $caption }} + {{ with $alt }}
{{ . | markdownify }}
{{ end }}
diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 2ee1ad1..b4efb67 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -19,12 +19,24 @@ {{- $image := partialCached "helper/image" (dict "Context" . "Type" "section") .RelPermalink "section" -}} {{ if $image.exists }}
- {{ if $image.resource }} - {{- $thumbnail := $image.resource.Fill "120x120" -}} - + {{ 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 ce09a79..34d05c2 100644 --- a/layouts/page/search.json +++ b/layouts/page/search.json @@ -9,8 +9,12 @@ {{- $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" -}} {{- if and $image.exists $image.resource -}} - {{- $thumbnail := $image.resource.Fill "120x120" -}} - {{- $image := dict "image" (absURL $thumbnail.Permalink) -}} + {{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}} + {{- $thumbnail := $image.resource.Fill "120x120" -}} + {{- $image := dict "image" (absURL $thumbnail.Permalink) -}} + {{- else -}} + {{- $image := dict "image" (absURL $image.resource.Permalink) -}} + {{- end -}} {{- $data = merge $data $image -}} {{ end }} diff --git a/layouts/partials/article-list/compact.html b/layouts/partials/article-list/compact.html index cedef2a..0f5a216 100644 --- a/layouts/partials/article-list/compact.html +++ b/layouts/partials/article-list/compact.html @@ -15,9 +15,21 @@ {{ if $image.exists }}
{{ if $image.resource }} - {{- $thumbnail := $image.resource.Fill "120x120" -}} - + {{- $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 }} Featured image of post {{ .Title }} {{ end }} diff --git a/layouts/partials/article-list/default.html b/layouts/partials/article-list/default.html index aeb5bb1..02e0b30 100644 --- a/layouts/partials/article-list/default.html +++ b/layouts/partials/article-list/default.html @@ -1,22 +1,4 @@ {{ $image := partialCached "helper/image" (dict "Context" . "Type" "articleList") .RelPermalink "articleList" }} \ No newline at end of file diff --git a/layouts/partials/article-list/tile.html b/layouts/partials/article-list/tile.html index f76f596..0fbf812 100644 --- a/layouts/partials/article-list/tile.html +++ b/layouts/partials/article-list/tile.html @@ -6,10 +6,23 @@
{{ if $image.resource }} {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}} - {{- $thumbnail := $imageRaw.Fill .size -}} + {{- $Permalink := $imageRaw.RelPermalink -}} + {{- $Width := $imageRaw.Width -}} + {{- $Height := $imageRaw.Height -}} - + {{- if (default true .Page.Site.Params.imageProcessing.cover.enabled) -}} + {{- $thumbnail := $imageRaw.Fill .size -}} + {{- $Permalink = $thumbnail.RelPermalink -}} + {{- $Width = $thumbnail.Width -}} + {{- $Height = $thumbnail.Height -}} + {{- end -}} + + {{ else }} {{ end }} diff --git a/layouts/partials/article/components/header.html b/layouts/partials/article/components/header.html index 7707b1d..7582ec2 100644 --- a/layouts/partials/article/components/header.html +++ b/layouts/partials/article/components/header.html @@ -2,16 +2,32 @@ {{- $image := partialCached "helper/image" (dict "Context" . "Type" "article") .RelPermalink "article" -}} {{ if $image.exists }}
- {{ if $image.resource }} - {{- $tablet := $image.resource.Resize "1024x" -}} - {{- $desktop := $image.resource.Resize "2000x" -}} + + {{ 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 }} + Featured image of post {{ .Title }} + {{ else }} + Featured image of post {{ .Title }} + {{ end }} +
{{ end }}