Skip to content

Commit fff86d9

Browse files
authored
feat(images): change preview image on post and home page based on light/dark mode, scaling
This PR adds the ability to change the image seen by the user for the post preview on the home page and on the page if set in the YAML front matter. Closes Post image and homepage card color change by theme change Fixes #2554
1 parent 7f35d8e commit fff86d9

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

_layouts/home.html

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,37 @@
6767
{% endif %}
6868

6969
<div class="col-md-5">
70-
<img src="{{ src }}" alt="{{ alt }}" {{ lqip }}>
70+
{% if post.image.light or post.image.dark %} {% if post.image.light %}
71+
<img
72+
src="{{ post.image.light }}"
73+
alt="{{ alt }}"
74+
class="light preview-image"
75+
style="{% if post.image.max_height %}max-height: {{ post.image.max_height }};{% else %}max-height: 200px;{% endif %} {% if post.image.object_fit %}object-fit: {{ post.image.object_fit }};{% else %}object-fit: cover;{% endif %} width: 100%; border-radius: 8px;"
76+
{{
77+
lqip
78+
}}
79+
/>
80+
{% endif %} {% if post.image.dark %}
81+
<img
82+
src="{{ post.image.dark }}"
83+
alt="{{ alt }}"
84+
class="dark preview-image"
85+
style="{% if post.image.max_height %}max-height: {{ post.image.max_height }};{% else %}max-height: 200px;{% endif %} {% if post.image.object_fit %}object-fit: {{ post.image.object_fit }};{% else %}object-fit: cover;{% endif %} width: 100%; border-radius: 8px;"
86+
{{
87+
lqip
88+
}}
89+
/>
90+
{% endif %} {% else %}
91+
<img
92+
src="{{ post.image.path | default: post.image }}"
93+
alt="{{ alt }}"
94+
class="preview-image"
95+
style="{% if post.image.max_height %}max-height: {{ post.image.max_height }};{% else %}max-height: 200px;{% endif %} {% if post.image.object_fit %}object-fit: {{ post.image.object_fit }};{% else %}object-fit: cover;{% endif %} width: 100%; border-radius: 8px;"
96+
{{
97+
lqip
98+
}}
99+
/>
100+
{% endif %}
71101
</div>
72102

73103
{% assign card_body_col = '7' %}

_layouts/post.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ <h1 data-toc-skip>{{ page.title }}</h1>
3737
{% endif %}
3838

3939
{% if page.image %}
40-
{% capture src %}src="{{ page.image.path | default: page.image }}"{% endcapture %}
41-
{% capture class %}class="preview-img{% if page.image.no_bg %}{{ ' no-bg' }}{% endif %}"{% endcapture %}
42-
{% capture alt %}alt="{{ page.image.alt | xml_escape | default: "Preview Image" }}"{% endcapture %}
43-
44-
{% if page.image.lqip %}
45-
{%- capture lqip -%}lqip="{{ page.image.lqip }}"{%- endcapture -%}
46-
{% endif %}
47-
4840
<div class="mt-3 mb-3">
49-
<img {{ src }} {{ class }} {{ alt }} w="1200" h="630" {{ lqip }}>
41+
{% if page.image.light or page.image.dark %}
42+
{% if page.image.light %}
43+
<img src="{{ page.image.light }}" class="preview-img light" alt="{{ page.image.alt | xml_escape | default: "Preview Image" }}" w="1200" h="630" style="{% if page.image.max_height %}max-height: {{ page.image.max_height }};{% endif %} {% if page.image.object_fit %}object-fit: {{ page.image.object_fit }};{% else %}object-fit: cover;{% endif %} background-color: white; padding: 10px; border-radius: 8px; width: 100%;"{% if page.image.lqip %} lqip="{{ page.image.lqip }}"{% endif %}>
44+
{% endif %}
45+
{% if page.image.dark %}
46+
<img src="{{ page.image.dark }}" class="preview-img dark" alt="{{ page.image.alt | xml_escape | default: "Preview Image" }}" w="1200" h="630" style="{% if page.image.max_height %}max-height: {{ page.image.max_height }};{% endif %} {% if page.image.object_fit %}object-fit: {{ page.image.object_fit }};{% else %}object-fit: cover;{% endif %} background-color: #1b1b1e; padding: 10px; border-radius: 8px; width: 100%;"{% if page.image.lqip %} lqip="{{ page.image.lqip }}"{% endif %}>
47+
{% endif %}
48+
{% else %}
49+
<img src="{{ page.image.path | default: page.image }}" class="preview-img" alt="{{ page.image.alt | xml_escape | default: "Preview Image" }}" w="1200" h="630" style="{% if page.image.max_height %}max-height: {{ page.image.max_height }};{% endif %} {% if page.image.object_fit %}object-fit: {{ page.image.object_fit }};{% else %}object-fit: cover;{% endif %} padding: 10px; border-radius: 8px; width: 100%;"{% if page.image.lqip %} lqip="{{ page.image.lqip }}"{% endif %}>
50+
{% endif %}
5051
{%- if page.image.alt -%}
5152
<figcaption class="text-center pt-2 pb-2">{{ page.image.alt }}</figcaption>
5253
{%- endif -%}

_posts/2019-08-08-write-a-new-post.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,41 @@ For normal images:
256256
```
257257
{: .nolineno }
258258

259+
#### Preview light/dark images
260+
261+
It is also possible to have the image differ based on if the user is seeing your site in light mode or dark mode:
262+
263+
```yaml
264+
---
265+
image:
266+
light: /path/to/image-light.png
267+
dark: /path/to/image-dark.png
268+
alt: this is some alt text
269+
---
270+
```
271+
272+
#### Preview scaling
273+
274+
For the home page, the height of the image preview and how it scales to fit can be set.
275+
276+
```yaml
277+
---
278+
image:
279+
path: /path/to/image.jpeg
280+
alt: this is some alt text
281+
object_fit: contain
282+
max_height: 200px # default
283+
---
284+
```
285+
286+
Available object-fit values:
287+
288+
- **`cover`** (default) - Scales to fill container, may crop edges to maintain aspect ratio
289+
- **`contain`** - Scales to fit entirely within container, may leave empty space
290+
- **`fill`** - Stretches to fill container exactly (may distort aspect ratio)
291+
- **`scale-down`** - Acts like `contain` but never scales up beyond original size
292+
- **`none`** - Image is not resized
293+
259294
### Social Media Platforms
260295

261296
You can embed video/audio from social media platforms with the following syntax:

0 commit comments

Comments
 (0)