Skip to content

Commit

Permalink
✨feat(image shortcodes): add lazy loading (#116)
Browse files Browse the repository at this point in the history
Additionally sets the `alt` parameters as optional and cleans HTML output.
  • Loading branch information
welpo authored Aug 25, 2023
1 parent 1be3fa8 commit 1c3db86
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 30 deletions.
4 changes: 3 additions & 1 deletion content/blog/shortcodes.ca.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = "Shortcodes personalitzats"
date = 2023-02-19
updated = 2023-08-24
updated = 2023-08-26
description = "Aquest tema inclou alguns shortcodes personalitzats útils que pots utilitzar per millorar les teves publicacions. Ja sigui per mostrar imatges que s'adapten als temes clar i fosc, o per donar format a una secció de referències amb un aspecte professional, aquests shortcodes personalitzats t'ajudaran."

[taxonomies]
Expand All @@ -15,6 +15,8 @@ quick_navigation_buttons = true

## Shortcodes d'imatge

**Nota**: tots els shortcodes d'imatge tenen dos paràmetres opcionals: `full_width`, que té com a valor predeterminat `false` (vegeu [a sota](#imatge-d-amplada-completa)), i `lazy_loading`, que té com a valor predeterminat `true`.

### Imatges per a temes duals

Útil si vols utilitzar una imatge diferent pels temes clar i fosc:
Expand Down
6 changes: 4 additions & 2 deletions content/blog/shortcodes.es.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = "Shortcodes personalizados"
date = 2023-02-19
updated = 2023-08-24
updated = 2023-08-26
description = "Este tema incluye algunos shortcodes personalizados útiles que puedes utilizar para mejorar tus publicaciones. Puedes mostrar imágenes que se adapten a los temas claro y oscuro, dar formato a una sección de referencias con un aspecto profesional, y más."

[taxonomies]
Expand All @@ -13,7 +13,9 @@ toc_levels = 2
quick_navigation_buttons = true
+++

## Shortcodes de imágenes
## Shortcodes de imagen

**Nota**: todos los shortcodes de imagen tienen dos parámetros opcionales: `full_width`, que tiene como valor predeterminado `false` (ver [más abajo](#imagen-a-ancho-completo)), y `lazy_loading`, que tiene como valor predeterminado `true`.

### Imágenes de doble tema

Expand Down
4 changes: 3 additions & 1 deletion content/blog/shortcodes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+++
title = "Custom shortcodes"
date = 2023-02-19
updated = 2023-08-24
updated = 2023-08-26
description = "This theme includes some useful custom shortcodes that you can use to enhance your posts. Whether you want to display images that adapt to light and dark themes, or format a professional-looking reference section, these custom shortcodes have got you covered."

[taxonomies]
Expand All @@ -15,6 +15,8 @@ quick_navigation_buttons = true

## Image shortcodes

**Note**: all image shortcodes have two optional parameters: `full_width`, which defaults to `false` (see [below](#full-width-image)), and `lazy_loading`, which defaults to `true`.

### Dual theme images

Useful if you want to use a different image for the light and dark themes:
Expand Down
6 changes: 4 additions & 2 deletions templates/shortcodes/dimmable_image.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% set meta = get_image_metadata(path=src, allow_missing=true) %}
{%- set meta = get_image_metadata(path=src, allow_missing=true) -%}
{%- set lazy_loading = lazy_loading | default(value=true) -%}

{% if full_width | default(value=false) %}
<div class="full-width">
{% endif %}
<img class="dimmable-image" src="{{ get_url(path=src) }}" {% if alt %}alt="{{ alt }}" {% endif %} {% if meta.width %}width="{{ meta.width }}" {% endif %} {% if meta.height %}height="{{ meta.height }}" {% endif %}/>
<img class="dimmable-image" src="{{ get_url(path=src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if alt %} alt="{{ alt }}"{% endif %}{% if meta.width %} width="{{ meta.width }}"{% endif %}{% if meta.height %} height="{{ meta.height }}" {% endif %}/>
{% if full_width | default(value=false) %}
</div>
{% endif %}
14 changes: 8 additions & 6 deletions templates/shortcodes/dual_theme_image.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% set light_meta = get_image_metadata(path=light_src, allow_missing=true) %}
{% set dark_meta = get_image_metadata(path=dark_src, allow_missing=true) %}
{% if full_width | default(value=false) %}
{%- set light_meta = get_image_metadata(path=light_src, allow_missing=true) -%}
{%- set dark_meta = get_image_metadata(path=dark_src, allow_missing=true) -%}
{%- set lazy_loading = lazy_loading | default(value=true) -%}

{%- if full_width | default(value=false) -%}
<div class="full-width">
{% endif %}
<img src="{{ get_url(path=light_src) }}" {% if alt %}alt="{{ alt }}" {% endif %} {% if light_meta.width %}width="{{ light_meta.width }}" {% endif %} {% if light_meta.height %}height="{{ light_meta.height }}" {% endif %} class="img-light">
<img src="{{ get_url(path=dark_src) }}" {% if alt %}alt="{{ alt }}" {% endif %} {% if dark_meta.width %}width="{{ dark_meta.width }}" {% endif %} {% if dark_meta.height %}height="{{ dark_meta.height }}" {% endif %} class="img-dark">
{%- endif -%}
<img src="{{ get_url(path=light_src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if alt %} alt="{{ alt }}"{% endif %}{% if light_meta.width %} width="{{ light_meta.width }}"{% endif %}{% if light_meta.height %} height="{{ light_meta.height }}"{% endif %} class="img-light">
<img src="{{ get_url(path=dark_src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if alt %} alt="{{ alt }}"{% endif %}{% if dark_meta.width %} width="{{ dark_meta.width }}"{% endif %}{% if dark_meta.height %} height="{{ dark_meta.height }}"{% endif %} class="img-dark">
{% if full_width | default(value=false) %}
</div>
{% endif %}
6 changes: 4 additions & 2 deletions templates/shortcodes/full_width_image.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% set meta = get_image_metadata(path=src, allow_missing=true) %}
{%- set meta = get_image_metadata(path=src, allow_missing=true) -%}
{%- set lazy_loading = lazy_loading | default(value=true) -%}

<div class="full-width">
<img {% if src %}src="{{ get_url(path=src) }}" {% endif %} {% if alt %}alt="{{ alt }}" {% endif %} {% if meta.width %}width="{{ meta.width }}" {% endif %} {% if meta.height %}height="{{ meta.height }}" {% endif %}/>
<img src="{{ get_url(path=src) }}"{% if alt %} alt="{{ alt }}"{% endif %}{% if meta.width %} width="{{ meta.width }}"{% endif %}{% if meta.height %} height="{{ meta.height }}"{% endif %}{% if lazy_loading %} loading="lazy"{% endif %}/>
</div>
9 changes: 4 additions & 5 deletions templates/shortcodes/image_hover.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{%- set default_meta = get_image_metadata(path=default_src, allow_missing=true) -%}
{%- set hovered_meta = get_image_metadata(path=hovered_src, allow_missing=true) -%}
{%- set lazy_loading = lazy_loading | default(value=true) -%}

<div class="image-hover-container {% if full_width | default(value=false) %}full-width{% endif %}">
<div class="image-hover-container{% if full_width | default(value=false) %} full-width{% endif %}">
<div class="image-default">
<img src="{{ get_url(path=default_src) }}" alt="{{ default_alt }}"
width="{{ default_meta.width }}" height="{{ default_meta.height }}">
<img src="{{ get_url(path=default_src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if hovered_alt %} alt="{{ default_alt }}"{% endif %}{% if default_meta.width %} width="{{ default_meta.width }}"{% endif %}{% if default_meta.height %} height="{{ default_meta.height }}"{% endif %}>
</div>
<div class="image-hovered">
<img src="{{ get_url(path=hovered_src) }}" alt="{{ hovered_alt }}"
width="{{ hovered_meta.width }}" height="{{ hovered_meta.height }}">
<img src="{{ get_url(path=hovered_src) }}"{% if hovered_alt %} alt="{{ hovered_alt }}"{% endif %}{% if hovered_meta.width %} width="{{ hovered_meta.width }}"{% endif %}{% if hovered_meta.height %} height="{{ hovered_meta.height }}"{% endif %}>
</div>
</div>
12 changes: 5 additions & 7 deletions templates/shortcodes/image_toggler.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
{# allowing individual interactive elements (like toggles) to function correctly. #}
{# This avoids conflicts when multiple instances of the shortcode are used. #}
{%- set random_id = get_random(end=100000) -%}

{% set default_meta = get_image_metadata(path=default_src, allow_missing=true) %}
{% set toggled_meta = get_image_metadata(path=toggled_src, allow_missing=true) %}
{%- set default_meta = get_image_metadata(path=default_src, allow_missing=true) -%}
{%- set toggled_meta = get_image_metadata(path=toggled_src, allow_missing=true) -%}
{%- set lazy_loading = lazy_loading | default(value=true) -%}

<div class="image-toggler-container {% if full_width | default(value=false) %}full-width{% endif %}">
<input type="checkbox" id="toggle-img-{{ random_id }}" class="image-toggler-toggle">
<label for="toggle-img-{{ random_id }}" class="image-label">
<div class="image-default">
<img src="{{ get_url(path=default_src) }}" alt="{{ default_alt }}"
width="{{ default_meta.width }}" height="{{ default_meta.height }}">
<img src="{{ get_url(path=default_src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if default_alt %} alt="{{ default_alt }}"{% endif %}{% if default_meta.width %} width="{{ default_meta.width }}"{% endif %}{% if default_meta.height %} height="{{ default_meta.height }}"{% endif %}>
</div>
<div class="image-toggled">
<img src="{{ get_url(path=toggled_src) }}" alt="{{ toggled_alt }}"
width="{{ toggled_meta.width }}" height="{{ toggled_meta.height }}">
<img src="{{ get_url(path=toggled_src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if toggled_alt %} alt="{{ toggled_alt }}"{% endif %}{% if toggled_meta.width %} width="{{ toggled_meta.width }}"{% endif %}{% if toggled_meta.height %} height="{{ toggled_meta.height }}"{% endif %}>
</div>
</label>
</div>
10 changes: 6 additions & 4 deletions templates/shortcodes/invertible_image.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{% set meta = get_image_metadata(path=src, allow_missing=true) %}
{%- set meta = get_image_metadata(path=src, allow_missing=true) -%}
{%- set lazy_loading = lazy_loading | default(value=true) -%}

{% if full_width | default(value=false) %}
<div class="full-width">
{% endif %}
<img class="invertible-image" {% if src %}src="{{ get_url(path=src) }}" {% endif %} {% if alt %}alt="{{ alt }}" {% endif %} {% if meta.width %}width="{{ meta.width }}" {% endif %} {% if meta.height %}height="{{ meta.height }}" {% endif %}/>
{% if full_width | default(value=false) %}
<img class="invertible-image" src="{{ get_url(path=src) }}"{% if lazy_loading %} loading="lazy"{% endif %}{% if alt %} alt="{{ alt }}"{% endif %}{% if meta.width %} width="{{ meta.width }}"{% endif %}{% if meta.height %} height="{{ meta.height }}" {% endif %}/>
{%- if full_width | default(value=false) -%}
</div>
{% endif %}
{%- endif -%}

0 comments on commit 1c3db86

Please sign in to comment.