gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.39k stars 7.49k forks source link

Add resources.IsProcessableImage #11779

Open bep opened 10 months ago

bep commented 10 months ago

This is slightly related to https://github.com/gohugoio/hugo/pull/11688

Note that I'm not totally sure about this ... There may be an alternative fix lurking around for this somewhere.

We have a few options:

Not sure.

razonyang commented 10 months ago

I'll vote for the $resource.IsProcessableImage, I think $resources.ByType 'image' should return all images regardless if it's processable, such as SVG, beside this approach probably bring breaking changes.

jmooring commented 10 months ago

I agree with @razonyang. I think this is fine:

{{ range resources.ByType "image" }}
  {{ with and .IsProcessableImage (.Process "resize 200x webp") }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

Using a method as shown above seems a little cleaner to me. The alternative is:

{{ range resources.ByType "image" }}
  {{ with and (resources.IsProcessableImage .) (.Process "resize 200x webp") }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}
bep commented 10 months ago

I have been thinking about this, and on the implementation level this would be much easier/cleaner if we make it a function instead of a method -- then it would just be a matter of checking if the type implements an interface, which is a pattern we could extend on if needed, e.g:

...

Also, having $page.IsProcessableImage always return false isnt't great when we want to document this down the line and then gets $page.IsProcessableVideo etc..

hugo-sid commented 10 months ago

I upvote @razonyang's suggestion.