Skip to content

Commit

Permalink
Refactor image (again) (#1283)
Browse files Browse the repository at this point in the history
* refactor: get remote resource logic

* refactor: image optimisation pipeline

* refactor: profile

* refactor: adjust params names

* chore: add more comments

* refactor: restore light gallary

* refactor: optimise render-image and image shortcod

* feat: optimise author

* fix: home alt text

* fix: home alt text

* fix: alt text

* fix: home-profile sizing

* chore: add documentation

* chore: add correct width and height to featuredImage

* chore: sizes auto

* fix: sizes 800px for front page images

* feat: optimise sizes option

* refactor: read options from config

* feat: support optimise and cachRemote override in image shortcode

* feat(docs): add image shortcode docs

* feat(docs): update params config

* fix: author avatar margin right
  • Loading branch information
HEIGE-PCloud authored May 26, 2024
1 parent 14aa93b commit 357d445
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 119 deletions.
20 changes: 9 additions & 11 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ enablePWA = false
license = '<a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a>'
# [Experimental] Bundle js
bundle = false
# [Experimental] cache remote images in markdown, more info: https://github.com/HEIGE-PCloud/DoIt/pull/860#issue-1574342372
# [试验性功能] 缓存 markdown 中的图片链接到本地,详情请见:https://github.com/HEIGE-PCloud/DoIt/pull/860#issue-1574342372
cacheRemoteImages = true
# [Experimental] generate image srcset attribute with hugo image processing feature.
# [试验性功能] 使用 hugo 自带的图片处理功能生成 srcset 属性
optimizeImages = true
# [Experimental] resize method for srcset attribute in optimizeImages, more info: https://gohugo.io/content-management/image-processing/#image-processing-options
# [实验性功能] optimizeImages 中 srcset 属性的缩放方法, 详情请见:https://gohugo.io/content-management/image-processing/#image-processing-options
srcsetSmallResizeMethod = "400x webp Lanczos q75"
srcsetDefaultResizeMethod = "600x webp Lanczos q75"
srcsetLargeResizeMethod = "800x webp Lanczos q75"

[image]
# cache remote images for better optimisations
# 缓存图片链接到本地
cacheRemote = true
# Image resizing and optimisation
# 图片缩放和优化
optimise = true

# Header config
# 页面头部导航栏配置
[header]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ Please open the code block below to view the complete sample configuration {{< f
ignoreLocation = false
useExtendedSearch = false
ignoreFieldNorm = false

[params.image]
# cache remote images for better optimisations
cacheRemote = true
# Image resizing and optimisation
optimise = true
# Header config
[params.header]
# {{< version 0.2.11 >}} Theme change mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ optimizeImages = true
useExtendedSearch = false
ignoreFieldNorm = false

[params.image]
# 缓存图片链接到本地
cacheRemote = true
# 图片缩放和优化
optimise = true
# 页面头部导航栏配置
[params.header]
# {{< version 0.2.11 >}} 主题切换模式
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,6 @@ The `image` shortcode has the following named parameters:

`class` attribute of the HTML `figure` tag.

* **src_s** *[optional]*

URL of the image thumbnail, used for lightgallery, the default value is the value of the **src** parameter.

* **src_l** *[optional]*

URL of the HD image, used for lightgallery, the default value is the value of the **src** parameter.

* **height** *[optional]*

`height` attribute of the image.
Expand All @@ -179,6 +171,14 @@ The `image` shortcode has the following named parameters:

Additional `rel` attributes of the HTML `a` tag, if **linked** parameter is set to `true`.

* **optimise** *[optional]*

Whether to optimise the image, override the site configuration.

* **cacheRemote** *[optional]*

Whether to cache the remote image, override the site configuration.

Example `image` input:

```markdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ This is a **right-aligned** paragraph.

HTML `figure` 标签的 `class` 属性.

* **src_s** *[可选]*

图片缩略图的 URL, 用在画廊模式中, 默认值是 **src** 参数的值.

* **src_l** *[可选]*

高清图片的 URL, 用在画廊模式中, 默认值是 **src** 参数的值.

* **height** *[可选]*

图片的 `height` 属性.
Expand All @@ -183,6 +175,14 @@ This is a **right-aligned** paragraph.

HTML `a` 标签 的 `rel` 补充属性, 仅在 **linked** 属性设置成 `true` 时有效.

* **optimise** *[可选]*

图片是否需要被优化,覆盖全局配置。

* **cacheRemote** *[可选]*

是否缓存远程图片,覆盖全局配置。

一个 `image` 示例:

```markdown
Expand Down
19 changes: 10 additions & 9 deletions layouts/_default/_markup/render-image.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{{- $lightgallery := .Page.Params.lightgallery | default site.Params.Page.lightgallery | default false -}}
{{- if .Title -}}
<figure>
{{- dict "Src" .Destination "Title" .Text "Caption" .Title "Linked" $lightgallery "Resources" .Page.Resources "Optim" true "Remote" true | partial "plugin/image.html" -}}
{{- $optim := slice
(dict "Process" "resize 800x webp q75" "descriptor" "800w")
(dict "Process" "resize 1200x webp q75" "descriptor" "1200w")
(dict "Process" "resize 1600x webp q75" "descriptor" "1600w")
-}}
<figure>
{{- dict "Src" .Destination "Title" .Text "Caption" .Title "Linked" $lightgallery "Resources" .Page.Resources "OptimConfig" $optim | partial "plugin/image.html" -}}
{{- if .Title -}}
<figcaption class="image-caption">
{{- .Title | safeHTML -}}
</figcaption>
</figure>
{{- else -}}
<figure>
{{- dict "Src" .Destination "Title" .Text "Linked" $lightgallery "Resources" .Page.Resources "Optim" true "Remote" true | partial "plugin/image.html" -}}
</figure>
{{- end -}}
{{- end -}}
</figure>
7 changes: 6 additions & 1 deletion layouts/_default/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
{{- with $image -}}
<div class="featured-image-preview">
<a href="{{ $.RelPermalink }}" aria-label={{ $.Title }}>
{{- dict "Src" . "Title" $.Description "Resources" $.Resources "Loading" "eager" "Optim" true | partial "plugin/image.html" -}}
{{ $optim := slice
(dict "Process" "fill 800x240 Center webp q75" "descriptor" "800w")
(dict "Process" "fill 1200x360 Center webp q75" "descriptor" "1200w")
(dict "Process" "fill 1600x480 Center webp q75" "descriptor" "1600w")
}}
{{- dict "Src" . "Title" $.Title "Resources" $.Resources "Loading" "eager" "Width" "800" "Height" "240" "Sizes" "(max-width: 680px) 100vw, (max-width: 1000px) 80vw, (max-width: 1440px) 56vw, 800px" "OptimConfig" $optim "Alt" (printf "Featured image for %v" $.Title) | partial "plugin/image.html" -}}
</a>
</div>
{{- end -}}
Expand Down
18 changes: 0 additions & 18 deletions layouts/partials/function/imageHandler.html

This file was deleted.

28 changes: 16 additions & 12 deletions layouts/partials/home/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
{{- if $avatar -}}
<div class="home-avatar">
{{- $menus := $.Site.Menus.main | default slice -}}
{{- $optim := slice
(dict "Process" "fill center 96x96 webp q75" "descriptor" "96w")
(dict "Process" "fill center 144x144 webp q75" "descriptor" "144w")
(dict "Process" "fill center 192x192 webp q75" "descriptor" "192w")
-}}
{{- with index $menus 0 -}}
{{- $url := .URL | relLangURL -}}
{{- with .Page -}}
{{- $url = .RelPermalink -}}
{{- end -}}
{{- with $profile.avatarLinkURL -}}
{{- $url = . -}}
{{- end -}}
<a href="{{ $url }}" {{ with .Title | default .Name }} title="{{ . }}" {{ end }}{{ if (urls.Parse $url).Host }}
rel="noopener noreferrer" target="_blank" {{ end }}>
{{- dict "Src" $avatar "Height" "100" "Width" "100" "Loading" "eager" | partial "plugin/image.html" -}}
</a>
{{- $url := .URL | relLangURL -}}
{{- with .Page -}}
{{- $url = .RelPermalink -}}
{{- end -}}
{{- with $profile.avatarLinkURL -}}
{{- $url = . -}}
{{- end -}}
<a href="{{ $url }}" {{ with .Title | default .Name }} title="{{ . }}" {{ end }}{{ if (urls.Parse $url).Host }} rel="noopener noreferrer" target="_blank" {{ end }}>
{{- dict "Src" $avatar "Height" "100" "Width" "100" "Loading" "eager" "OptimConfig" $optim "Alt" "Home avatar" | partial "plugin/image.html" -}}
</a>
{{- else -}}
{{- dict "Src" $avatar "Height" "100" "Width" "100" | partial "plugin/image.html" -}}
{{- dict "Src" $avatar "Height" "100" "Width" "100" "Loading" "eager" "OptimConfig" $optim "Alt" "Home avatar" | partial "plugin/image.html" -}}
{{- end -}}
</div>
{{- end -}}
Expand Down
11 changes: 7 additions & 4 deletions layouts/partials/meta/author.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{{- $optim := slice
(dict "Process" "resize 16x16 Center webp q75" "descriptor" "16w")
(dict "Process" "resize 24x24 Center webp q75" "descriptor" "24w")
(dict "Process" "resize 32x32 Center webp q75" "descriptor" "32w")
-}}
{{- if .Params.authors -}}
{{- $lang := ( $.Params.lang | default $.Lang ) -}}
<span class='author'>
Expand All @@ -10,9 +15,7 @@
"lang" $lang
) -}}
{{- if gt $i 0 }}, {{ end -}}
<a href='{{ .link | absURL }}'><img width="16" height="16" src='{{ .avatar | absURL }}' alt='{{ .name }} avatar' class="tw-inline-block tw-max-h-4 tw-rounded-full tw-translate-y-[-2px] tw-mr-1"/>
{{- .name -}}
</a>
<a href='{{ .link | absURL }}'>{{- dict "Width" "16" "Height" "16" "Src" .avatar "Alt" (printf "%v avatar" .name) "OptimConfig" $optim "Class" "tw-inline-block tw-max-h-4 tw-rounded-full tw-translate-y-[-2px] tw-mr-1" | partial "plugin/image.html" -}}{{- .name -}}</a>
{{- end -}}
{{- else -}}
{{- with partial "function/author.html" (dict
Expand All @@ -35,7 +38,7 @@
{{- $avatar := .Params.authoravatar | default .Site.Author.avatar | default $gravatar | default false -}}
{{- $options := dict "Class" "author" "Destination" $link "Title" "Author" "Rel" "author" "Content" $author -}}
{{- if $avatar -}}
<img src="{{ $avatar | absURL }}" alt="{{ $author }} avatar" class="tw-inline-block tw-max-h-4 tw-rounded-full tw-translate-y-[-2px] tw-mr-1">
{{- dict "Width" "16" "Height" "16" "Src" $avatar "Alt" (printf "%v avatar" $author) "OptimConfig" $optim "Class" "tw-inline-block tw-max-h-4 tw-rounded-full tw-translate-y-[-2px] tw-mr-1" | partial "plugin/image.html" -}}
{{- else -}}
{{- partial "plugin/fontawesome.html" (dict "Style" "solid" "Icon" "user-circle") -}}
{{- end -}}
Expand Down
101 changes: 60 additions & 41 deletions layouts/partials/plugin/image.html
Original file line number Diff line number Diff line change
@@ -1,51 +1,70 @@
{{- /* lightgallery.js */ -}}
{{- $default := dict "RelPermalink" .Src -}}
{{- $remote := .Remote | default false -}}
{{- $optimize := .Optim | default false -}}
{{- $small := $default -}}
{{- $large := $default -}}
{{- $height := "" -}}
{{- $width := "" -}}
{{- $optimized := false -}}

{{- with dict "Path" .Src "Resources" .Resources "Remote" $remote | partial "function/getImage.html" -}}
{{- $output := dict "Optim" $optimize "Image" . | partial "function/imageHandler.html" -}}
{{- $small = $output.S -}}
{{- $default = $output.M -}}
{{- $large = $output.L -}}
{{- $optimized = $output.Optimized -}}
{{- $height = $default.Height -}}
{{- $width = $default.Width -}}
{{- end -}}
{{/*
The image plugin is a partial that handles image resources.

Parameters:
- Src: The path to the image.
It can be a page resource (inside a page bundle) https://gohugo.io/methods/page/resources/
or a global resource (inside the assets directory) https://gohugo.io/functions/resources/get/
or a link to a remote resource, which can be cached if the site parameter is set https://gohugo.io/functions/resources/getremote/
- Resources: If the image is a page resource, this parameter is required. It is the .Resources of the page.
- OptimConfig: optimisation parameter, defined like
{{- $optim := slice
(dict "Process" "resize 800x Center webp q75" "descriptor" "800w")
(dict "Process" "resize 1200x Center webp q75" "descriptor" "1200w")
(dict "Process" "resize 1600x Center webp q75" "descriptor" "1600w")
-}}
See https://gohugo.io/content-management/image-processing/ for more information.
- Alt: alt text for the image. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#alt
- Height: The intrinsic height of the image, in pixels. Must be an integer without a unit.
We try to generate this value automatically if possible. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#height
- Width: The intrinsic width of the image, in pixels. Must be an integer without a unit.
We try to generate this value automatically if possible. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#width
- Loading: Indicates how the browser should load the image: eager or lazy. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#loading
- Optimise: Override the site parameter to optimise the image.
- CacheRemote: Override the site parameter to cache remote images.
*/}}

{{- $resource := (.Resources.Get .Src) | default (resources.Get .Src) | default nil -}}

{{- $cacheRemote := .CacheRemote | default site.Params.image.cacheRemote | default false -}}
{{- $optimise := .Optimise | default site.Params.image.optimise | default false -}}

{{- $alt := .Alt | default .Title | default .Src -}}
{{- $loading := .Loading | default "lazy" -}}
{{- $isUrlRemote := urls.Parse .Src | partial "function/isUrlRemote.html" -}}

{{- with .Height -}}
{{- $height = . -}}
{{- if not $resource | and $isUrlRemote | and $cacheRemote -}}
{{- with $remoteResource := resources.GetRemote .Src -}}
{{- with .Err -}}
{{- warnf "%s" . -}}
{{ else }}
{{- if $remoteResource.ResourceType | eq "image" -}}
{{- $resource = $remoteResource -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- with .Width -}}
{{- $width = . -}}
{{- $optim := .OptimConfig -}}
{{- $srcset := "" -}}
{{- if $optim | and $resource | and $optimise -}}
{{ $srcsetSlice := slice -}}
{{- range $optim -}}
{{- if .Process -}}
{{- $processed := $resource.Process .Process -}}
{{- $srcsetSlice = $srcsetSlice | append (printf "%s %s" $processed.RelPermalink .descriptor) -}}
{{- end -}}
{{- end -}}
{{- $srcset = delimit $srcsetSlice ", " -}}
{{- end -}}

{{- $src := $resource.RelPermalink | default .Src -}}
{{- $alt := .Alt | default .Title -}}
{{- $height := .Height | default $resource.Height | default "" -}}
{{- $width := .Width | default $resource.Width | default "" -}}

{{- if .Linked -}}
<a class="lightgallery" href="{{ $large.RelPermalink | safeURL }}" title="{{ .Title | default $alt }}" data-thumbnail="{{ $small.RelPermalink | safeURL }}"{{ with .Caption }} data-sub-html="<h2>{{ . }}</h2>{{ with $.Title }}<p>{{ . }}</p>{{ end }}"{{ end }}{{ with .Rel }} rel="{{ . }}"{{ end }}>
<a class="lightgallery" href="{{ $src }}" title="{{ .Title | default .Alt }}" data-thumbnail="{{ $src }}"{{ with .Caption }} data-sub-html="<h2>{{ . }}</h2>{{ with $.Title }}<p>{{ . }}</p>{{ end }}"{{ end }}{{ with .Rel }} rel="{{ . }}"{{ end }}>
{{- end -}}
<img
{{ with .Class }}class="{{ . }}"{{ end }}
loading="{{ $loading }}"
src="{{ $default.RelPermalink | safeURL }}"
srcset="
{{- if eq $optimized false -}}
{{ $small.RelPermalink | safeURL }}, {{ $default.RelPermalink | safeURL }} 1.5x, {{ $large.RelPermalink | safeURL }} 2x
{{- else -}}
{{ $small.RelPermalink | safeURL }} {{ $small.Width }}w,
{{ $default.RelPermalink | safeURL }} {{ $default.Width }}w,
{{ $large.RelPermalink | safeURL }} {{ $large.Width }}w
{{- end -}}
"
alt="{{ $alt }}"{{ with $height }} height="{{ . }}" {{ end }}{{ with $width }} width="{{ . }}" {{ end }}>
<img {{ with .Class }}class="{{ . }}"{{ end }} {{ with .Loading }}loading="{{ . }}"{{ end }} src="{{ $src }}" {{ with $srcset }}srcset="{{ . }}"{{ end }} {{ with .Sizes }}sizes="{{ . }}"{{ end }} {{ with $alt }}alt="{{ . }}"{{ end }} {{ with $height }}height="{{ . }}"{{ end }} {{ with $width }}width="{{ . }}"{{ end }}>
{{- if .Linked -}}
</a>
</a>
{{- end -}}
4 changes: 2 additions & 2 deletions layouts/partials/single/sponsor.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
"name" $name
"author" (index $.Site.Data.authors $name)
"lang" $lang) -}}
{{- dict "Src" .avatar | partial "plugin/image.html" -}}
{{- dict "Src" .avatar "Resources" $.Resources | partial "plugin/image.html" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- else if $avatar -}}
{{- dict "Src" $avatar | partial "plugin/image.html" -}}
{{- dict "Src" $avatar "Resources" $.Resources | partial "plugin/image.html" -}}
{{- end -}}
</div>
{{- with $bio -}}
Expand Down
7 changes: 6 additions & 1 deletion layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ <h2 class="single-subtitle">{{ . }}</h2>
{{- $image := $params.featuredimage -}}
{{- with $image -}}
<div class="featured-image">
{{- dict "Src" . "Title" $.Description "Resources" $.Resources "Loading" "eager" "Optim" true | partial "plugin/image.html" -}}
{{- $optim := slice
(dict "Process" "resize 800x webp q75" "descriptor" "800w")
(dict "Process" "resize 1200x webp q75" "descriptor" "1200w")
(dict "Process" "resize 1600x webp q75" "descriptor" "1600w")
-}}
{{- dict "Src" . "Title" $.Description "Resources" $.Resources "Loading" "eager" "OptimConfig" $optim | partial "plugin/image.html" -}}
</div>
{{- end -}}
{{- /* Series list */ -}}
Expand Down
Loading

0 comments on commit 357d445

Please sign in to comment.