Skip to content
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,31 @@ This file starts at 0.4.0. Older releases are described on the
`<meta name="robots" content="noindex">` tag and leaves it out of
`sitemap.xml`, both its own entry and the `hreflang` alternates of its
translations.
- `widths` and `sizes` under `params.image` set the widths of the WebP copies
in the `srcset` of content images and their `sizes` attribute for the whole
site ([#1112]). Markdown images, the `image` shortcode and featured images
use them; the header logo, the home page avatar and author avatars keep
their own. The `sizes` of the content column stay the default, and
`widths = []` turns the copies off.

### Changed

- Content images get WebP copies 480, 768, 1024, 1440 and 1920 pixels wide
instead of one at 800 pixels, so phones load a small copy and wide or
high-density screens a sharp one ([#1112]). The largest width caps the
copies: an image wider than it no longer lists the original file in
`srcset`. The `src` is the widest copy of at most 1024 pixels (the narrowest
copy when all are wider) instead of the original file; lightGallery still
opens the original. The cap and the `src` also apply to the home page avatar
and author avatars, which keep their own widths: an avatar wider than its
largest width no longer lists its original file either. A build makes up to
five copies per image instead of one, so a build without Hugo's resource
cache (`resources/_gen` by default) takes noticeably longer; later builds
reuse the cached copies. `widths = [480, 768, 1024]` under `params.image`
builds faster, but images are less sharp on high-density screens.
`widths = [800]` makes a single copy again, but without the original file in
`srcset`, so browsers load images wider than 800 pixels as an 800 pixels wide
copy; add a larger width to keep them sharp on wide or high-density screens.
- Third-party libraries are served from the site by default:
`params.cdn.data` defaults to `""` instead of `"jsdelivr.yml"`, in the theme
and in the example site, so pages load the library files bundled with the
Expand Down Expand Up @@ -721,5 +743,6 @@ for the full upgrade guide.
[#1096]: https://github.com/dillonzq/LoveIt/pull/1096
[#1098]: https://github.com/dillonzq/LoveIt/pull/1098
[#1101]: https://github.com/dillonzq/LoveIt/pull/1101
[#1112]: https://github.com/dillonzq/LoveIt/issues/1112
[#1115]: https://github.com/dillonzq/LoveIt/issues/1115
[#1120]: https://github.com/dillonzq/LoveIt/issues/1120
12 changes: 12 additions & 0 deletions exampleSite/content/posts/theme-documentation-basics/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,18 @@ enableEmoji = true
Phone = ""
RSS = "" # {{< version 0.2.0 >}}

# {{< version 0.4.3 >}} Image config
[params.image]
# widths in pixels of the WebP copies in the srcset of content images
# (Markdown images, the image shortcode, featured images), smallest to largest;
# widths not below the width of an image become one copy at its own width, and the largest width
# caps the copies; the src is the widest copy of at most 1024 pixels (else the narrowest copy);
# [] turns the copies off
widths = [480, 768, 1024, 1440, 1920]
# sizes attribute of those images, which tells the browser how wide the image is shown
# ("" uses the width of the content column of the theme)
sizes = ""

# {{< version 0.2.0 changed >}} Page global config
[params.page]
# {{< version 0.2.0 >}} whether to hide a page from home page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,15 @@ enableEmoji = true
Phone = ""
RSS = "" # {{< version 0.2.0 >}}

# {{< version 0.4.3 >}} 图片配置
[params.image]
# 内容图片 (Markdown 图片, image shortcode, 特色图片) srcset 中 WebP 副本的宽度 (像素), 从小到大;
# 不小于图片宽度的值会合并为一个原始宽度的副本, 最大的宽度是副本宽度的上限;
# src 使用不超过 1024 像素的最宽副本 (没有时使用最窄的副本); [] 关闭副本
widths = [480, 768, 1024, 1440, 1920]
# 这些图片的 sizes 属性, 告诉浏览器图片显示的宽度 ("" 使用主题内容栏的宽度)
sizes = ""

# {{< version 0.2.0 changed >}} 文章页面全局配置
[params.page]
# {{< version 0.2.0 >}} 是否在主页隐藏一篇文章
Expand Down
17 changes: 14 additions & 3 deletions exampleSite/content/posts/theme-documentation-content/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,20 @@ This applies to Markdown images, the `image` shortcode, featured images and the
(the header logo only gets its `width` and `height`):

* JPEG, PNG and WebP images get their `width` and `height`, which keeps the page from shifting while they load,
and a `srcset` with a WebP copy up to 800 pixels wide,
so browsers download a size that fits the screen.
The original file is the last `srcset` candidate when it is wider, and the `src` of the image.
and a `srcset` of WebP copies, so browsers download a size that fits the screen.
{{< version 0.4.3 changed >}} Markdown images, the `image` shortcode and featured images get copies
480, 768, 1024, 1440 and 1920 pixels wide (800 before): only those narrower than the image plus one at its own width,
and 1920 pixels at most. Their `src` is the widest copy of at most 1024 pixels, or the narrowest copy when every copy is wider;
lightGallery opens the original file.
{{< version 0.4.3 >}} `widths` and `sizes` under `params.image` in the
[site configuration](../theme-documentation-basics#site-configuration) set the widths of these copies and the `sizes` attribute,
for example `widths = [800]` for a single copy as in earlier versions.
Unlike earlier versions, the original file is no longer added to `srcset`,
so browsers load images wider than 800 pixels as an 800 pixels wide copy; add a larger width to keep them sharp on wide or high-density screens.
Up to five copies per image make a build without Hugo's resource cache (`resources/_gen` by default) noticeably slower;
later builds reuse the cached copies. `widths = [480, 768, 1024]` builds faster, but images are less sharp on high-density screens.
The home page avatar keeps its own widths (160, 320 and 480 pixels) and is not affected by these settings,
but the same rules apply to it: its copies stop at 480 pixels and its `src` is the widest copy.
Images follow their Exif orientation. Before Hugo 0.155.0, PNG and WebP images
keep the original file so the browser can apply that orientation.
* GIF, BMP, TIFF and animated PNG or WebP images get their `width` and `height` only, so animations keep playing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ Markdown 图片, `image` shortcode, 文章特色图片和主页头像都会这
(网站 logo 只会带上 `width` 和 `height`):

* JPEG, PNG 和 WebP 图片会带上 `width` 和 `height`, 避免图片加载时页面发生偏移,
并带上包含宽度不超过 800 像素的 WebP 副本的 `srcset`,
浏览器会下载适合屏幕的尺寸.
原图比副本更宽时会作为 `srcset` 的最后一个候选, 同时也是图片的 `src`.
并带上包含 WebP 副本的 `srcset`, 浏览器会下载适合屏幕的尺寸.
{{< version 0.4.3 changed >}} Markdown 图片, `image` shortcode 和特色图片的副本宽度为 480, 768, 1024, 1440 和 1920 像素 (之前为 800),
只生成比图片窄的副本, 再加上一个原始宽度的副本, 最宽不超过 1920 像素.
它们的 `src` 是不超过 1024 像素的最宽副本, 所有副本都更宽时使用最窄的副本; lightGallery 打开原图.
{{< version 0.4.3 >}} [网站配置](../theme-documentation-basics#site-configuration)中 `params.image` 下的
`widths` 和 `sizes` 用来设置这些副本的宽度和 `sizes` 属性,
例如 `widths = [800]` 会像之前版本一样只生成一个副本.
但与之前版本不同, 原图不再加入 `srcset`, 因此比 800 像素宽的图片只会加载 800 像素宽的副本;
如果要在宽屏或高分辨率屏幕上保持清晰, 需要加上更大的宽度.
每张图片最多生成五个副本, 没有 Hugo 资源缓存 (默认为 `resources/_gen`) 时构建会明显变慢, 之后的构建会复用缓存的副本.
`widths = [480, 768, 1024]` 构建更快, 但图片在高分辨率屏幕上不够清晰.
主页头像保留自己的宽度 (160, 320 和 480 像素), 不受这些设置影响,
但同样适用上面的规则: 它的副本最宽为 480 像素, `src` 是最宽的副本.
图片会按照 Exif 方向信息旋转. Hugo 0.155.0 之前的版本会保留 PNG 和 WebP 原图,
由浏览器按方向信息显示.
* GIF, BMP, TIFF 图片和 PNG, WebP 动图只会带上 `width` 和 `height`, 因此动图仍然会动.
Expand Down
17 changes: 17 additions & 0 deletions exampleSite/hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,23 @@ ignoreErrors = ["error-remote-getjson", "error-missing-instagram-accesstoken"]
Email = ""
RSS = ""

# Image config
# 图片配置
[params.image]
# widths in pixels of the WebP copies in the srcset of content images
# (Markdown images, the image shortcode, featured images), smallest to largest;
# widths not below the width of an image become one copy at its own width, and the largest width
# caps the copies; the src is the widest copy of at most 1024 pixels (else the narrowest copy);
# [] turns the copies off
# 内容图片 (Markdown 图片, image shortcode, 特色图片) srcset 中 WebP 副本的宽度 (像素), 从小到大;
# 不小于图片宽度的值会合并为一个原始宽度的副本, 最大的宽度是副本宽度的上限;
# src 使用不超过 1024 像素的最宽副本 (没有时使用最窄的副本); [] 关闭副本
widths = [480, 768, 1024, 1440, 1920]
# sizes attribute of those images, which tells the browser how wide the image is shown
# ("" uses the width of the content column of the theme)
# 这些图片的 sizes 属性, 告诉浏览器图片显示的宽度 ("" 使用主题内容栏的宽度)
sizes = ""

# Page global config
# 文章页面全局配置
[params.page]
Expand Down
17 changes: 17 additions & 0 deletions hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,23 @@
Phone = ""
RSS = ""

# Image config
# 图片配置
[params.image]
# widths in pixels of the WebP copies in the srcset of content images
# (Markdown images, the image shortcode, featured images), smallest to largest;
# widths not below the width of an image become one copy at its own width, and the largest width
# caps the copies; the src is the widest copy of at most 1024 pixels (else the narrowest copy);
# [] turns the copies off
# 内容图片 (Markdown 图片, image shortcode, 特色图片) srcset 中 WebP 副本的宽度 (像素), 从小到大;
# 不小于图片宽度的值会合并为一个原始宽度的副本, 最大的宽度是副本宽度的上限;
# src 使用不超过 1024 像素的最宽副本 (没有时使用最窄的副本); [] 关闭副本
widths = [480, 768, 1024, 1440, 1920]
# sizes attribute of those images, which tells the browser how wide the image is shown
# ("" uses the width of the content column of the theme)
# 这些图片的 sizes 属性, 告诉浏览器图片显示的宽度 ("" 使用主题内容栏的宽度)
sizes = ""

# Page global config
# 文章页面全局配置
[params.page]
Expand Down
59 changes: 50 additions & 9 deletions layouts/_partials/plugin/img.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,60 @@
- Lazy: false loads the image at once without raising its priority.
Other images are lazy-loaded natively and decoded asynchronously.
- Widths: widths of the WebP copies in srcset; an empty slice disables them.
Without it, params.image.widths of the site, else 480, 768, 1024, 1440
and 1920. The largest width caps the copies.
- Sizes: the sizes attribute that goes with srcset.
Without it, params.image.sizes of the site, else a size for the content column.

Page resources and global resources (assets/) in JPEG, PNG or WebP get
width, height and a srcset of WebP copies made with Hugo's image
processing, followed by the original file when it is wider than the
largest copy. GIF, BMP, TIFF and animated PNG or WebP only get their size.
processing, no wider than the largest width. The src is the widest copy
of at most 1024 pixels, else the narrowest; lightGallery links the
original file. GIF, BMP, TIFF and animated PNG or WebP only get their size.
Before Hugo 0.155.0, PNG and WebP are kept as originals to preserve orientation.
SVG, other formats, files under static/ and remote URLs are linked as
they are.
*/ -}}
{{- $src := .Src -}}
{{- $width := .Width -}}
{{- $height := .Height -}}
{{- $widths := cond (isset . "Widths") .Widths (slice 800) -}}
{{- $sizes := .Sizes | default "(max-width: 680px) 100vw, (max-width: 960px) 80vw, (max-width: 1280px) 52vw, (max-width: 1440px) 56vw, (max-width: 1688px) 64vw, 1080px" -}}
{{- $widths := slice 480 768 1024 1440 1920 -}}
{{- $sizes := "(max-width: 680px) 100vw, (max-width: 960px) 80vw, (max-width: 1280px) 52vw, (max-width: 1440px) 56vw, (max-width: 1688px) 64vw, 1080px" -}}
{{- with site.Params.image -}}
{{- /* An empty list in the site configuration turns the copies off. */ -}}
{{- if isset . "widths" -}}
{{- $widths = .widths -}}
{{- end -}}
{{- with .sizes -}}
{{- $sizes = . -}}
{{- end -}}
{{- end -}}
{{- if isset . "Widths" -}}
{{- $widths = .Widths -}}
{{- end -}}
{{- with .Sizes -}}
{{- $sizes = . -}}
{{- end -}}
{{- /* Whole pixels: floats (JSON configurations give only floats) are rounded. A single number is a list of one; other values are left out. */ -}}
{{- if not (reflect.IsSlice $widths) -}}
{{- $widths = slice $widths -}}
{{- end -}}
{{- $pixels := slice -}}
{{- range $widths -}}
{{- /* float reads true as 1: booleans are no widths. */ -}}
{{- if ne (printf "%T" .) "bool" -}}
{{- with (try (float .)).Value -}}
{{- $pixel := math.Round . | int -}}
{{- if gt $pixel 0 -}}
{{- $pixels = $pixels | append $pixel -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $widths = $pixels -}}
{{- $candidates := slice -}}
{{- $thumbnail := "" -}}
{{- $imgSrc := "" -}}

{{- with dict "Path" $src "Resources" .Resources | partial "function/resource.html" -}}
{{- $src = .RelPermalink -}}
Expand Down Expand Up @@ -140,7 +177,8 @@
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Copies narrower than the image, plus one at its own width when a width reaches it. */ -}}
{{- /* Copies narrower than the image, plus one at its own width when a width reaches it; */ -}}
{{- /* an image wider than every width gets none at its own width: the largest width is the cap. */ -}}
{{- $copies := slice -}}
{{- range $widths -}}
{{- if lt . $imageWidth -}}
Expand All @@ -159,16 +197,19 @@
{{- if .Err -}}
{{- $failed = true -}}
{{- else -}}
{{- $candidates = $candidates | append (dict "URL" .Value.RelPermalink "Descriptor" (printf "%dw" .Value.Width)) -}}
{{- $candidates = $candidates | append (dict "URL" .Value.RelPermalink "Width" .Value.Width "Descriptor" (printf "%dw" .Value.Width)) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if $failed -}}
{{- $candidates = slice -}}
{{- else if $candidates -}}
{{- $thumbnail = (index $candidates 0).URL -}}
{{- if not $covered -}}
{{- $candidates = $candidates | append (dict "URL" $src "Descriptor" (printf "%dw" $imageWidth)) -}}
{{- $imgSrc = $thumbnail -}}
{{- range $candidates -}}
{{- if le .Width 1024 -}}
{{- $imgSrc = .URL -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
Expand Down Expand Up @@ -202,7 +243,7 @@
<a class="lightgallery" href="{{ $large | safeURL }}"{{ with $label }} title="{{ . }}"{{ end }} data-thumbnail="{{ $thumbnail | safeURL }}"{{ with .Caption }} data-sub-html="<h2>{{ . }}</h2>{{ with $.Title }}<p>{{ . }}</p>{{ end }}"{{ end }}{{ with .Rel }} rel="{{ . }}"{{ end }}>
{{- end -}}
<img
{{- with .Class }} class="{{ . }}"{{ end }} src="{{ $src | safeURL }}"
{{- with .Class }} class="{{ . }}"{{ end }} src="{{ $imgSrc | default $src | safeURL }}"
{{- with $srcset }} srcset="{{ range $i, $c := . }}{{ if $i }}, {{ end }}{{ $c.URL | safeURL }}{{ with $c.Descriptor }} {{ . }}{{ end }}{{ end }}"{{ end }}
{{- if $candidates }} sizes="{{ $sizes }}"{{ end }} alt="{{ $alt }}"
{{- if not $.Linked }}{{ with $.Title | default $alt }} title="{{ . }}"{{ end }}{{ end }}
Expand Down
7 changes: 4 additions & 3 deletions tests/author-avatar.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ test('author avatar in the byline and the structured data', async () => {
}
// An image under assets/ is published below the base path, with resized copies.
const img = avatar(html);
assert.equal(attribute(img, 'src'), '/blog/images/avatar.png', label(name));
if (resizesPng) assert.match(attribute(img, 'srcset'), /\/blog\/images\/avatar_[^ ]+\.webp 20w, .+ 40w, .+ 60w/, label(name));
// The src is the widest copy (60w); without copies, the original.
assert.equal(attribute(img, 'src'), resizesPng ? attribute(img, 'srcset').split(', ').at(-1).split(' ')[0] : '/blog/images/avatar.png', label(name));
if (resizesPng) assert.match(attribute(img, 'srcset'), /^\/blog\/images\/avatar_[^ ]+\.webp 20w, .+ 40w, .+ 60w$/, label(name));
assert.equal(attribute(img, 'alt'), '', label(name));
assert.equal(attribute(img, 'width'), '20', label(name));
assert.equal(attribute(img, 'height'), '20', label(name));
Expand All @@ -127,7 +128,7 @@ test('author avatar in the byline and the structured data', async () => {
if (variant === 'avatar') {
assert.equal(attribute(avatar(page('site')), 'loading'), undefined);
const img = avatar(summary);
assert.equal(attribute(img, 'src'), '/blog/images/avatar.png');
assert.equal(attribute(img, 'src'), attribute(avatar(page('site')), 'src'));
assert.equal(attribute(img, 'loading'), 'lazy');
assert.deepEqual(author(home), { ...sitePerson, image: assetsAvatar });
} else {
Expand Down
Loading
Loading