images: allow option to apply image effects globally #623

This commit is contained in:
Sören Weber 2023-08-25 19:09:50 +02:00
parent fb22b500d0
commit 151c31e142
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
13 changed files with 140 additions and 38 deletions

View file

@ -42,8 +42,8 @@ The Relearn theme is a fork of the great [Learn theme](https://github.com/matcor
- [Search support for mixed language content](https://mcshelby.github.io/hugo-theme-relearn/cont/i18n#search) - [Search support for mixed language content](https://mcshelby.github.io/hugo-theme-relearn/cont/i18n#search)
- **Additional Markdown features** - **Additional Markdown features**
- [Support for GFM (GitHub Flavored Markdown](https://mcshelby.github.io/hugo-theme-relearn/cont/markdown) - [Support for GFM (GitHub Flavored Markdown](https://mcshelby.github.io/hugo-theme-relearn/cont/markdown)
- [Image styling like sizing, shadow, border and alignment](https://mcshelby.github.io/hugo-theme-relearn/cont/markdown#further-image-formatting) - [Image effects like sizing, shadow, border and alignment](https://mcshelby.github.io/hugo-theme-relearn/cont/markdown#image-effects)
- [Image lightbox](https://mcshelby.github.io/hugo-theme-relearn/cont/markdown#further-image-formatting#lightbox) - [Image lightbox](https://mcshelby.github.io/hugo-theme-relearn/cont/markdown#lightbox)
- **Shortcodes galore** - **Shortcodes galore**
- [Display files attached to page bundles](https://mcshelby.github.io/hugo-theme-relearn/shortcodes/attachments) - [Display files attached to page bundles](https://mcshelby.github.io/hugo-theme-relearn/shortcodes/attachments)
- [Marker badges](https://mcshelby.github.io/hugo-theme-relearn/shortcodes/badge) - [Marker badges](https://mcshelby.github.io/hugo-theme-relearn/shortcodes/badge)

View file

@ -45,8 +45,8 @@ The theme is a fork of the great [Learn theme](https://github.com/matcornic/hugo
- [Search support for mixed language content]({{%relref "cont/i18n#search" %}}) - [Search support for mixed language content]({{%relref "cont/i18n#search" %}})
- **Additional Markdown features** - **Additional Markdown features**
- [Support for GFM (GitHub Flavored Markdown]({{%relref "cont/markdown" %}}) - [Support for GFM (GitHub Flavored Markdown]({{%relref "cont/markdown" %}})
- [Image styling like sizing, shadow, border and alignment]({{%relref "cont/markdown#further-image-formatting" %}}) - [Image effects like sizing, shadow, border and alignment]({{%relref "cont/markdown#image-effects" %}})
- [Image lightbox]({{%relref "cont/markdown#further-image-formatting#lightbox" %}}) - [Image lightbox]({{%relref "cont/markdown#lightbox" %}})
- **Shortcodes galore** - **Shortcodes galore**
- [Display files attached to page bundles]({{%relref "shortcodes/attachments" %}}) - [Display files attached to page bundles]({{%relref "shortcodes/attachments" %}})
- [Marker badges]({{%relref "shortcodes/badge" %}}) - [Marker badges]({{%relref "shortcodes/badge" %}})

View file

@ -91,6 +91,11 @@ Note that some of these parameters are explained in details in other sections of
disableExplicitIndexURLs = false disableExplicitIndexURLs = false
# For external links you can define how they are opened in your browser; this setting will only be applied to the content area but not the shortcut menu # For external links you can define how they are opened in your browser; this setting will only be applied to the content area but not the shortcut menu
externalLinkTarget = "_blank" externalLinkTarget = "_blank"
# Override default values for image effects, you can even add your own arbitrary effects to the list
[params.imageEffects]
border = false
lightbox = true
shadow = false
``` ```
## Serving your page from a subfolder ## Serving your page from a subfolder

View file

@ -41,6 +41,8 @@ This document shows you what's new in the latest release. For a detailed list of
For existing variants nothing has changed visually. For existing variants nothing has changed visually.
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The default values for the [image effects]({{% relref "cont/markdown#image-effects" %}}) are [now configurable]({{% relref "cont/imageeffects" %}}) for your whole site via `config.toml` or for each page thru frontmatter.
--- ---
## 5.19.0 (2023-08-12) {#5190} ## 5.19.0 (2023-08-12) {#5190}
@ -313,7 +315,7 @@ This document shows you what's new in the latest release. For a detailed list of
- {{% badge style="note" title=" " %}}Change{{% /badge %}} The way images are processed has changed. Now images are lazy loaded by default which speeds up page load on slow networks and/or big pages and also the print preview. - {{% badge style="note" title=" " %}}Change{{% /badge %}} The way images are processed has changed. Now images are lazy loaded by default which speeds up page load on slow networks and/or big pages and also the print preview.
For that the JavaScript code to handle the [lightbox and image effects]({{% relref "cont/markdown#further-image-formatting" %}}) on the client side was removed in favour for static generation of those effects on the server. For that the JavaScript code to handle the [lightbox and image effects]({{% relref "cont/markdown#image-effects" %}}) on the client side was removed in favour for static generation of those effects on the server.
If you have used HTML directly in your Markdown files, this now has the downside that it doesn't respect the effect query parameter anymore. In this case you have to migrate all your HTML `img` URLs manually to the respective HTML attributes. If you have used HTML directly in your Markdown files, this now has the downside that it doesn't respect the effect query parameter anymore. In this case you have to migrate all your HTML `img` URLs manually to the respective HTML attributes.

View file

@ -0,0 +1,61 @@
+++
title = "Image Effects"
weight = 4
+++
The theme supports non-standard [image effects]({{% relref "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.
Instead, you can configure the defaults in your `config.toml` aswell as overriding these default in the pages frontmatter.
Explicitly set URL query parameter will override the defaults in effect for a page.
Without any settings in your `config.toml` this defaults to
````toml {title="config.toml"}
[params]
[params.imageEffects]
border = false
lightbox = true
shadow = false
````
This can be overridden in a pages frontmatter by eg.
````toml {title="frontmatter"}
+++
[imageEffects]
border = true
+++
````
Or by explicitly override settings by URL query parameter
````markdown {title="URL"}
![Minion](https://octodex.github.com/images/minion.png?lightbox=false&bg-white=true)
````
The settings applied to the above image would be
````toml {title="Result"}
border = true
lightbox = false
shadow = false
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">
````
## Extending
As you can see in the above example, the `bg-white` parameter is not initially supported in the themes default settings. Nevertheless you are free to define arbitrary parameter by just adding them to the URL query parameter or set them in your `config.toml` or pages frontmatter.
{{% notice note %}}
If no extended parameter like `bg-white` in the example is set on the URL, a `classes="nobg-white"` in the HTML will only be generated if a default value was set in the `config.toml` or pages frontmatter.
{{% /notice %}}

View file

@ -0,0 +1,5 @@
+++
title = "Image Effects"
weight = 4
+++
{{< piratify >}}

View file

@ -564,9 +564,9 @@ Images can also be linked by reference ID to later define the URL location. This
[laforge]: https://octodex.github.com/images/trekkie.jpg?width=20vw "Geordi La Forge" [laforge]: https://octodex.github.com/images/trekkie.jpg?width=20vw "Geordi La Forge"
{{% /notice %}} {{% /notice %}}
### Further Image Formatting ### Image Effects
This theme allows additional non-standard formatting by setting query parameter at the end of the image URL. This theme allows additional non-standard formatting by setting query parameter at the end of the image URL. The default [behavior is configurable]({{% relref "cont/imageeffects" %}}) thru your `config.toml` or frontmatter parameter.
#### Resizing #### Resizing

View file

@ -82,6 +82,11 @@ hidden = false
LastModifierDisplayName = "" LastModifierDisplayName = ""
# Email of this page modifier. If set with LastModifierDisplayName, it will be displayed in the footer # Email of this page modifier. If set with LastModifierDisplayName, it will be displayed in the footer
LastModifierEmail = "" LastModifierEmail = ""
# Override default values for image effects, you can even add your own arbitrary effects to the list
[params.imageEffects]
border = false
lightbox = true
shadow = false
+++ +++
``` ```

View file

@ -6,8 +6,13 @@
{{- $url := .url }} {{- $url := .url }}
{{- $title := .title }} {{- $title := .title }}
{{- $alt := .alt }} {{- $alt := .alt }}
{{- $classes := slice }} {{- $effects := dict "border" false "lightbox" true "shadow" false }}
{{- $lightbox := true }} {{- if $page.Site.Params.imageeffects }}
{{- $effects = merge $effects $page.Site.Params.imageeffects }}
{{- end }}
{{- if $page.Params.imageEffects }}
{{- $effects = merge $effects $page.Params.imageEffects }}
{{- end }}
{{- $height := "auto" }} {{- $height := "auto" }}
{{- $width := "auto" }} {{- $width := "auto" }}
{{- $dest_url := urls.Parse $url }} {{- $dest_url := urls.Parse $url }}
@ -26,13 +31,27 @@
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- if $dest_url.RawQuery }} {{- if $dest_url.RawQuery }}
{{- if $dest_url.Query.Get "classes" }} {{- if $dest_url.Query.Has "classes" }}
{{- $classes = $classes | append (split ($dest_url.Query.Get "classes") ",") }} {{- $classes := slice | append (split ($dest_url.Query.Get "classes") ",") }}
{{- range $classes }}
{{- $k := . }}
{{- $v := true }}
{{- if strings.HasPrefix $k "no" }}
{{- $k := strings.TrimPrefix "no" $k }}
{{- $v := false }}
{{- end }}
{{- $effects = merge $effects (dict $k $v) }}
{{- end }}
{{- end }} {{- end }}
{{- if $dest_url.Query.Has "featherlight" }} {{- if $dest_url.Query.Has "featherlight" }}
{{- warnf "%q: DEPRECATED usage of 'featherlight' image CSS class found, use 'lightbox' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration/#5110" $page.File.Path }} {{- warnf "%q: DEPRECATED usage of 'featherlight' image CSS class found, use 'lightbox' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration/#5110" $page.File.Path }}
{{- $effects = merge $effects (dict "lightbox" (ne ($dest_url.Query.Get "featherlight") "false")) }}
{{- end }}
{{- range $k, $v := $effects }}
{{- if $dest_url.Query.Has $k }}
{{- $effects = merge $effects (dict $k (ne ($dest_url.Query.Get $k) "false")) }}
{{- end }}
{{- end }} {{- end }}
{{- $lightbox = and (ne ($dest_url.Query.Get "lightbox") "false") (ne ($dest_url.Query.Get "featherlight") "false") }}
{{- with $dest_url.Query.Get "height" }} {{- with $dest_url.Query.Get "height" }}
{{ $height = . }} {{ $height = . }}
{{- end }} {{- end }}
@ -40,14 +59,19 @@
{{ $width = . }} {{ $width = . }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- $classes := slice }}
{{- range $k, $v := $effects }}
{{- $c := printf "%s%s" (cond $v "" "no") $k }}
{{- $classes = $classes | append $c }}
{{- end }}
{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }} {{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }}
{{- if $lightbox }} {{- if $effects.lightbox }}
<a href="#image-{{ $id }}" class="lightbox-link"> <a href="#image-{{ $id }}" class="lightbox-link">
{{- end }} {{- end }}
<img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }}{{ if len ($classes) }} class="{{ delimit $classes " " }}"{{ end }} style="height: {{ $height }}; width: {{ $width }};" loading="lazy"> <img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }}{{ if len ($classes) }} class="{{ delimit $classes " " }}"{{ end }} style="height: {{ $height }}; width: {{ $width }};" loading="lazy">
{{- if $lightbox }} {{- if $effects.lightbox }}
</a> </a>
<a href="javascript:history.back();" class="lightbox" id="image-{{ $id }}"> <a href="javascript:history.back();" class="lightbox-back" id="image-{{ $id }}">
<img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }} class="lightbox-image" loading="lazy"> <img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }} class="lightbox-image" loading="lazy">
</a> </a>
{{- end }} {{- end }}

View file

@ -521,7 +521,7 @@
background-color: rgba( 255, 255, 255, 1 ); /* var(--INTERNAL-MAIN-BG-color) */ background-color: rgba( 255, 255, 255, 1 ); /* var(--INTERNAL-MAIN-BG-color) */
} }
.lightbox img{ .lightbox-back img{
background-color: rgba( 255, 255, 255, 1 ) /* var(--INTERNAL-MAIN-BG-color); */ background-color: rgba( 255, 255, 255, 1 ) /* var(--INTERNAL-MAIN-BG-color); */
} }

View file

@ -1780,7 +1780,7 @@ article ul > li > input[type="checkbox"]:checked::before {
} }
/* CSS Lightbox https://codepen.io/gschier/pen/kyRXVx */ /* CSS Lightbox https://codepen.io/gschier/pen/kyRXVx */
.lightbox { .lightbox-back {
align-items: center; align-items: center;
background: rgba( 0, 0, 0, .8 ); background: rgba( 0, 0, 0, .8 );
bottom: 0; bottom: 0;
@ -1795,11 +1795,11 @@ article ul > li > input[type="checkbox"]:checked::before {
z-index: 1999; z-index: 1999;
} }
.lightbox:target { .lightbox-back:target {
display: flex; display: flex;
} }
.lightbox img { .lightbox-back img {
max-height: 95%; max-height: 95%;
max-width: 95%; max-width: 95%;
overflow: auto; overflow: auto;

View file

@ -245,7 +245,7 @@ table {
background-color: var(--INTERNAL-MAIN-BG-color); background-color: var(--INTERNAL-MAIN-BG-color);
} }
.lightbox img{ .lightbox-back img{
background-color: var(--INTERNAL-MAIN-BG-color); background-color: var(--INTERNAL-MAIN-BG-color);
} }

View file

@ -987,7 +987,7 @@ function initSwipeHandler(){
} }
function initImage(){ function initImage(){
document.querySelectorAll( '.lightbox' ).forEach( function(e){ e.addEventListener("keydown", imageEscapeHandler); }, false); document.querySelectorAll( '.lightbox-back' ).forEach( function(e){ e.addEventListener("keydown", imageEscapeHandler); }, false);
} }
function clearHistory() { function clearHistory() {
@ -1098,7 +1098,7 @@ function mark() {
topbarLinks[i].classList.add( 'highlight' ); topbarLinks[i].classList.add( 'highlight' );
} }
var bodyInnerLinks = document.querySelectorAll( '#body-inner a:not(.lightbox-link):not(.btn):not(.lightbox)' ); var bodyInnerLinks = document.querySelectorAll( '#body-inner a:not(.lightbox-link):not(.btn):not(.lightbox-back)' );
for( var i = 0; i < bodyInnerLinks.length; i++ ){ for( var i = 0; i < bodyInnerLinks.length; i++ ){
bodyInnerLinks[i].classList.add( 'highlight' ); bodyInnerLinks[i].classList.add( 'highlight' );
} }