Skip to content

Conversation

@Schallbert
Copy link

@Schallbert Schallbert commented Nov 16, 2025

Why this change?

Some people like me use themes where the image key in a post's front matter is either already taken (e.g. for a banner image) or unused.

I use (minimal mistakes) that has a different way of organizing a post's frontmatter with header.teaser.

This PR should solve the issue with a customizable key: thumb_path_key can be set in _config.yml and will point the liquid template to the correct thumbnail image path.

This can help deduplicate front matter information.

What I touched

- I have reorganized the generator's code just a bit so its main routine looks shorter for readability.

  • In the liquid template, thumb_path_key is evaluated and split to allow for higher nesting depths in the front matter like header.teaser
  • I updated the affected readme section with examples, might be a bit lengthy now.

Still this change keeps backwards compatibility due to default values for image and image.path.

Testing

  • add tests for changes in feed.xml
  • add tests for the updated .rb paths

@Schallbert Schallbert changed the title Add user customization to image key for thumbnails/images Make image key configurable for thumbnails/images Nov 16, 2025
- exchanged image.path and image defaults
@parkr
Copy link
Member

parkr commented Nov 17, 2025

Thanks for the PR! It's generally good hygiene to keep each PR narrowly focused. Would you mind pulling your refactor into a separate PR? Anything directly related to the image path change is fine to stay.

@Schallbert
Copy link
Author

OK, will do.
I'll also write some tests for the new behavior.

@parkr
Copy link
Member

parkr commented Nov 18, 2025

Thank you!

@Schallbert
Copy link
Author

I hope we should be good to go now.

Copy link
Member

@parkr parkr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! First pass of review.

README.md Outdated

* `image` - URL of an image that is representative of the post (can also be passed as `image.path`)
* `image` - URL of an image that is representative of the post. It can also be passed as `image.path` or set by the user.
To customize, go to your `_config.yml` and add the field `thumb_path_key: your.image.key`. Dots express nesting depth of the key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean this to be all part of one list item in the unordered list? To create a new paragraph you'll need a blank line between. If intended, no problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wanted it to be as short as possible:
image

{% endif %}

{% assign post_image = post.image.path | default: post.image %}
{% assign thumb_key = site.data.thumb_key %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the term "image" previously, not "thumb". Could you update this to "image_path_key" or "image_path_frontmatter_key"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, changing all ocurrences of "thumb" to "image" for consistency.


{% assign post_image = post.image.path | default: post.image %}
{% assign thumb_key = site.data.thumb_key %}
{% capture key_parts %}{{ thumb_key }}{% endcapture %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key is already a string. Is the capture necessary? Couldn't it be split directly? I believe this is allowing the key to have liquid of its own which might be overkill.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you are. It is a leftover of my tries to get the job done without having to split. Thought I could avoid the loop. Removed.

Jekyll.logger.info "Jekyll Feed:", "Skipping feed generation in development"
return
end
site.data['image_key'] = get_image_key
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this can be removed, is that right? I don't think we're using it in the feed.xml Liquid.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this is where I hand over the image key to feed.xml so it becomes available for retrieving the image path.
If we pulled the key from the config directly in Liquid, we could avoid adding code in ruby altogether. Without ruby involved though, I wouldn't know how to add the log line about using a custom path key.

{% endif %}

{% assign post_image = post.image.path | default: post.image %}
{% assign image_key = site.data.image_key %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a site with site.data.image_key missing, will any of this fail? Perhaps we should consider adding a default: "image" here so that it always has an image_key. Then we can remove the default: post.image below on line 126.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't fail because site.data.image_key defaults to image.path which tries replicating the behavior we had before the change (if image.path is empty it tries post.image)

Jekyll.logger.info "Jekyll Feed:", "Using custom image_path_key = #{image_path_key}"
end
return image_path_key
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this -- I believe it can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written above. Would you like me to try a liquid-only variant? It would be getting the key from the config posts_limit times then and we wouldn't have a log line, but it will definetely simplify things.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the log line - I think it's useful.

@Schallbert
Copy link
Author

  • generator.rb: Modifiy get_image_key to check_custom_image_key that won't to anything but log if we detect a custom image key
  • generator_spec.rb: Removed altogether as there's nothing left in the generator that affects the feed.
  • feed.xml Read site.image_path_key directly.

Comment on lines 118 to 124
{% assign image_key = site.image_path_key | default: "image.path" %}
{% assign key_array = image_key | split: "." %}

{% assign image_value = post %}
{% for key in key_array %}
{% assign image_value = image_value[key] %}
{% endfor %}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just see that these lines could be moved above the "post" loop as they just need to be executed once which would make the code more efficient. On the other hand readability and cohesiveness would degrade, as the variable image_value is instantiated there but used somewhere else. Any opinions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The image_key and image_key_array could be pre-calculated above outside the loop to speed things up. The image_value would need to be calculated inside the loop to ensure it gets the right value for each post. I think the cleanest solution could be:

Outside the loop: if site.image_path_key is non-null, build key_array.
Inside the loop: if key_array is non-null, build image_value. Assign post_image as post_image = image_value | default: post.image.path | default: post.image

What do you think?

Jekyll.logger.info "Jekyll Feed:", "Using custom image_path_key = #{image_path_key}"
end
return image_path_key
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the log line - I think it's useful.


image_key_tests.each do |test_case|
it "correctly processes image_key: '#{test_case[:thumbs]}'" do
site.data["image_key"] = test_case[:thumbs]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be image_path_key to pass?

Comment on lines 118 to 124
{% assign image_key = site.image_path_key | default: "image.path" %}
{% assign key_array = image_key | split: "." %}

{% assign image_value = post %}
{% for key in key_array %}
{% assign image_value = image_value[key] %}
{% endfor %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The image_key and image_key_array could be pre-calculated above outside the loop to speed things up. The image_value would need to be calculated inside the loop to ensure it gets the right value for each post. I think the cleanest solution could be:

Outside the loop: if site.image_path_key is non-null, build key_array.
Inside the loop: if key_array is non-null, build image_value. Assign post_image as post_image = image_value | default: post.image.path | default: post.image

What do you think?

@Schallbert
Copy link
Author

Schallbert commented Nov 26, 2025

Outside the loop: if site.image_path_key is non-null, build key_array.

Makes sense to me. What I can't get my head around right now (too tired) is how I should build the value outside the loop and prepend post afterwards inside. Maybe I need to work with empty array index [0] , place post and reconvert it to a key or maybe I need the post array of length 1 and append the pre-build image_value...

Will take another look at it tomorrow.

spec: fix naming error image_path_key
@Schallbert
Copy link
Author

Now, three tests fail:
image
They all fail because at a place where I'd expect object-image.png the spec result shows image.png and I just cannot figure out why. My real website is built fine and has no image-swap issues.
image
I guess I need some help getting the tests to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants