Skip to content

Commit

Permalink
image: add lazy loading image effect option #803
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby committed Mar 16, 2024
1 parent dc65242 commit 878102f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ image.errorlevel = "warning"
# Default: false
imageEffects.border = true
# Default: true
imageEffects.lazy = true
# Default: true
imageEffects.lightbox = true
# Default: false
imageEffects.shadow = false
Expand Down
2 changes: 2 additions & 0 deletions exampleSite/content/basics/migration/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ This document shows you what's new in the latest release and flags it with one o

## 5.26.0.beta (XXX) {#5260}

- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The lazy loading of images is now configurable by using the new `lazy` [image effect](cont/imageeffects). The default value hasn't changed in comparison to older versions, you don't need to change anything.

- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} It is now possible to [adjust the max width of the main area](basics/customization#change-the-main-areas-max-width), eg. in case you want to use the full page width for your content.

- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} Images and Codefences are now respecting [Hugo's Markdown attributes](https://gohugo.io/content-management/markdown-attributes/).
Expand Down
2 changes: 2 additions & 0 deletions exampleSite/content/cont/frontmatter/frontmatter.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ highlightWrap = true
# Default: false
imageEffects.border = true
# Default: true
imageEffects.lazy = true
# Default: true
imageEffects.lightbox = true
# Default: false
imageEffects.shadow = false
Expand Down
13 changes: 11 additions & 2 deletions exampleSite/content/cont/imageeffects.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ weight = 5

The theme supports non-standard [image effects](cont/markdown#image-effects).

As described, you can add this to the URL query parameter, but this may be cumbersome to do it consistently for the whole page.
| Name | Description |
| -------- | ----------------------------------------------------------------- |
| border | Draws a light thin border around the image |
| lazy | Lets the image be lazy loaded |
| lightbox | The image will be clickable to show it enlarged |
| shadow | Draws a shadow around the image to make it appear hovered/glowing |

As [described](cont/markdown#image-effects), you can add this to the URL query parameter, but this may be cumbersome to be done consistently for the whole page.

Instead, you can configure the defaults in your `hugo.toml` aswell as overriding these default in the pages frontmatter.

Expand All @@ -17,6 +24,7 @@ Without any settings in your `hugo.toml` this defaults to
[params]
[params.imageEffects]
border = false
lazy = true
lightbox = true
shadow = false
{{< /multiconfig >}}
Expand All @@ -38,6 +46,7 @@ The settings applied to the above image would be

{{< multiconfig >}}
border = true
lazy = true
lightbox = false
shadow = false
bg-white = true
Expand All @@ -46,7 +55,7 @@ bg-white = true
This ends up in the following HTML where the parameter are converted to CSS classes.

````html {title="HTML"}
<img src="https://octodex.github.com/images/minion.png?lightbox=false&bg-white=true" alt="Minion" class="bg-white border nolightbox noshadow">
<img src="https://octodex.github.com/images/minion.png?lightbox=false&bg-white=true" loading="lazy" alt="Minion" class="bg-white border lazy nolightbox noshadow">
````


Expand Down
7 changes: 5 additions & 2 deletions layouts/partials/shortcodes/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{- $url := .url }}
{{- $title := .title }}
{{- $alt := .alt }}
{{- $effects := dict "border" false "lightbox" true "shadow" false }}
{{- $effects := dict "border" false "lazy" true "lightbox" true "shadow" false }}
{{- if $page.Site.Params.imageeffects }}
{{- $effects = merge $effects $page.Site.Params.imageeffects }}
{{- end }}
Expand Down Expand Up @@ -75,8 +75,11 @@
{{- $c := printf "%s%s" (cond $v "" "no") $k }}
{{- $classes = $classes | append $c }}
{{- end }}
{{- $attributes := merge .attributes (dict "alt" $alt "src" $url "title" $title "loading" "lazy") }}
{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }}
{{- $attributes := merge .attributes (dict "alt" $alt "src" $url "title" $title) }}
{{- if $effects.lazy }}
{{- $attributes = merge $attributes (dict "loading" "lazy") }}
{{- end }}
{{- if $effects.lightbox -}}
<a href="#R-image-{{ $id }}" class="lightbox-link">
{{- end }}
Expand Down

0 comments on commit 878102f

Please sign in to comment.