Skip to content
This repository was archived by the owner on Nov 16, 2022. It is now read-only.

Commit be3e0b9

Browse files
committed
Add basic responsive image support
1 parent 1349628 commit be3e0b9

File tree

6 files changed

+59
-13
lines changed

6 files changed

+59
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules/
22
public/
33
# Generated by PostCSS
44
static/styles.css
5+
# Generated by resize_image function
6+
static/processed_images/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Zola's [shortcodes] allow easily inject HTML in Markdown and reduce repetition.
4747
- `airtable(id: string, color?: string = "gray")` [embeds Airtable base][airtable-embed] with `id` with optional `color` for `backgroundColor`.
4848
- `container(type: note|tip|info|warning|danger, title?: string)` wraps `body` in a styled container `<div>` with icon, similar to [gridsome-remark-container]. `title` defaults to `type`.
4949
- `details(summary: string)` wraps `body` in `<details>` with `<summary>`.
50+
- `figure(src: string, alt?: string, themed?: bool = false)` produces centered responsive image inside `<figure>` with `alt` in `<figcaption>` if provided. `src` is a co-located asset and relative to the page or section. If `themed` is true, `light`/`dark` image variants are used. `src` is a file name without `-light`/`-dark` suffix. For example, `figure(src='foo.png', themed=true)` requires `foo-light.png` and `foo-dark.png`.
5051

5152
## TODO
5253

content/2018/2018-04-16_next-gen-code-execution-engine/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Along with this new release, we are also releasing a number of updates that come
1313

1414
### New Languages
1515

16-
{{ themed_figure(src="new-languages.png", alt="PowerShell, Fortran, Julia, and NASM are now supported") }}
16+
{{ figure(src="new-languages.png", alt="PowerShell, Fortran, Julia, and NASM are now supported", themed=true) }}
1717

1818
We now support [**PowerShell**](https://en.wikipedia.org/wiki/PowerShell), [**Fortran**](https://en.wikipedia.org/wiki/Fortran), [**Julia**](https://en.wikipedia.org/wiki/Julia_(programming_language)) & [**NASM**](https://en.wikipedia.org/wiki/Netwide_Assembler)**!** These languages are in beta and as of press time don’t have any/many kata available yet. If you have experience working with any of these, the community could use your help translating existing content — which happens to be a pretty good way to earn some honor in the process.
1919

templates/macros.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,29 @@ <h2 class="text-lg leading-6 font-medium text-gray-900 dark:text-tgray-300"><a h
108108
<svg class="w-6 h-6 hidden dark:block text-gray-500 dark:text-tgray-600 dark:hover:text-brand" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"></path></svg>
109109
</button>
110110
{% endmacro %}
111+
112+
{% macro img_srcset(context, src, alt, class) %}
113+
{# `src` is relative to the page/section, find relative path from content/ for image functions #}
114+
{% set_global path = "" %}
115+
{% for asset in context.assets %}
116+
{% if asset | split(pat='/') | last == src %}
117+
{% set_global path = asset %}
118+
{% break %}
119+
{% endif %}
120+
{% endfor %}
121+
{# resize wide images for smaller screens #}
122+
{% set_global srcset = [] %}
123+
{% if path != "" %}
124+
{% set meta = get_image_metadata(path=path) %}
125+
{% for w in [480, 640, 768, 1024, 1280, 1536] %}
126+
{% if w < meta.width %}
127+
{% set resized = resize_image(path=path, op='fit_width', width=w) %}
128+
{% set size = w | as_str %}
129+
{% set_global srcset = srcset | concat(with=resized ~ ' ' ~ size ~ 'w') %}
130+
{% endif %}
131+
{% endfor %}
132+
{% endif %}
133+
{% set_global srcset = srcset | join(sep=', ') | json_encode() %}
134+
135+
<img src="{{ src }}" srcset={{ srcset | safe }}{% if class %} class="{{ class }}"{% endif %}{% if alt %} alt="{{ alt }}"{% endif %}>
136+
{% endmacro %}

templates/shortcodes/figure.html

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1+
{% import "macros.html" as macros %}
2+
13
<figure class="w-full">
2-
<img class="mx-auto" src="{{ src }}"{% if alt %} alt="{{ alt }}"{% endif %}>
3-
{% if alt %}
4-
<figcaption class="text-center">{{ alt }}</figcaption>
5-
{% endif %}
4+
{% if themed %} {# use light/dark variant #}
5+
{% set ext = src | split(pat='.') | last %}
6+
{% set ext = '.' ~ ext %}
7+
{% set base_src = src | trim_end_matches(pat=ext) %}
8+
{% set light = '-light' ~ ext %}
9+
{% set dark = '-dark' ~ ext %}
10+
{% set light_src = base_src ~ light %}
11+
{% set dark_src = base_src ~ dark %}
12+
13+
{% if page %}
14+
{{ macros::img_srcset(src=light_src, context=page, class="mx-auto block dark:hidden", alt=alt | default(value="")) }}
15+
{{ macros::img_srcset(src=dark_src, context=page, class="mx-auto hidden dark:block", alt=alt | default(value="")) }}
16+
{% else %}
17+
{{ macros::img_srcset(src=light_src, context=section, class="mx-auto block dark:hidden", alt=alt | default(value="")) }}
18+
{{ macros::img_srcset(src=dark_src, context=section, class="mx-auto hidden dark:block", alt=alt | default(value="")) }}
19+
{% endif %}
20+
{% else %}
21+
{% if page %}
22+
{{ macros::img_srcset(src=src, context=page, class="mx-auto", alt=alt | default(value="")) }}
23+
{% else %}
24+
{{ macros::img_srcset(src=src, context=section, class="mx-auto", alt=alt | default(value="")) }}
25+
{% endif %}
26+
{% endif %}
27+
28+
{% if alt %}
29+
<figcaption class="text-center">{{ alt }}</figcaption>
30+
{% endif %}
631
</figure>

templates/shortcodes/themed_figure.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)