Skip to content

Commit

Permalink
feat: improve image shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesrocket committed Jan 26, 2024
1 parent ed56fe7 commit e7be8f8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
12 changes: 6 additions & 6 deletions content/posts/image/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ csp_img = ["raw.githubusercontent.com/charlesrocket/beastie/"]
input:

```rs
{{/* image(src="fireball.gif", link="https://halve-z.netlify.app/", alt="Fireball", caption="8bit fireball") */}}
{{/* image(src="/fireball.gif", link="https://halve-z.netlify.app/", alt="Fireball", caption="8bit fireball") */}}
```

- `src`: the path/URL to the image file (mandatory)
Expand All @@ -22,10 +22,10 @@ input:
output:

```html
{{ image(src="fireball.gif", link="https://halve-z.netlify.app/", alt="Fireball", caption="8bit fireball") }}
{{ image(src="/fireball.gif", link="https://halve-z.netlify.app/", alt="Fireball", caption="8bit fireball") }}
```

{{ image(src="fireball.gif", link="https://halve-z.netlify.app/", alt="Fireball", caption="8bit fireball") }}
{{ image(src="/fireball.gif", link="https://halve-z.netlify.app/", alt="Fireball", caption="8bit fireball") }}

input:

Expand All @@ -44,16 +44,16 @@ output:
input:

```rs
{{/* image(src="fireball.gif") */}}
{{/* image(src="yawn.gif") */}}
```

output:

```html
{{ image(src="fireball.gif") }}
{{ image(src="yawn.gif") }}
```

{{ image(src="fireball.gif") }}
{{ image(src="yawn.gif") }}

## CSP

Expand Down
Binary file added content/posts/image/yawn.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 5 additions & 7 deletions templates/shortcodes/image.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{%- if src is starting_with("https://") %}{% set image = src %}{% else %}{% set image = get_url(path=src, cachebust=true) %}{% set meta = get_image_metadata(path=src) %}{%- endif %}
{%- if src is starting_with("https://") %}{% set image = src %}{%- elif src is starting_with("/") %}{% set image = get_url(path=src, cachebust=true) %}{% set meta = get_image_metadata(path=src) %}{% else %}{% set file = page.path ~ src %}{% set image = get_url(path=file, cachebust=true) %}{% set meta = get_image_metadata(path=file) %}{%- endif %}
<figure>
<center>
{%- if link %}<a href={{ link | safe }}>{%- endif %}
<img class="align-center page-image" src="{{ image | safe }}"{% if meta %} width="{{ meta.width }}" height="{{ meta.height }}"{% endif %}{% if alt %} alt="{{ alt | safe }}"{% endif %}/>
{%- if link %}</a>{%- endif %}
{%- if link %}<a href={{ link | safe }}>{%- endif %}
<img class="align-center page-image" src="{{ image | safe }}"{% if meta %} width="{{ meta.width }}" height="{{ meta.height }}"{% endif %}{% if alt %} alt="{{ alt | safe }}"{% endif %}/>
{%- if link %}</a>{%- endif %}
{%- if caption %}<figcaption>{{ caption | safe }}</figcaption>{%- endif %}
</center>
</figure>
</figure>

0 comments on commit e7be8f8

Please sign in to comment.