Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Page.Resources.GetMatch returns nil when the image file name ends with .en.png #12076

Closed
masakichi opened this issue Feb 20, 2024 · 4 comments · Fixed by #12078
Closed

.Page.Resources.GetMatch returns nil when the image file name ends with .en.png #12076

masakichi opened this issue Feb 20, 2024 · 4 comments · Fixed by #12078

Comments

@masakichi
Copy link

masakichi commented Feb 20, 2024

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.123.0+extended linux/amd64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes

After upgraded to latest v0.123.0, I can't build my site successfully, the error message is:

Error: error building site: render: failed to render pages: render of "page" failed: "/home/yuanji/Projects/nikki/themes/futu/layouts/_default/baseof.html:9:8": execute of template failed: template: _default/single.html:9:8: executing "_default/single.html" at <partial "head.html" .>: error calling partial: "/home/yuanji/Projects/nikki/themes/futu/layouts/partials/head.html:15:3": execute of template failed: template: partials/head.html:15:3: executing "partials/head.html" at <partial "schema_jsonld.html" .>: error calling partial: "/home/yuanji/Projects/nikki/themes/futu/layouts/partials/schema_jsonld.html:42:20": execute of template failed: template: partials/schema_jsonld.html:42:20: executing "partials/schema_jsonld.html" at <.Content>: error calling Content: "/home/yuanji/Projects/nikki/content/posts/my_post_title/index.en.md:1:1": "/home/yuanji/Projects/nikki/themes/futu/layouts/_default/_markup/render-image.html:4:15": execute of template failed: template: _default/_markup/render-image.html:4:15: executing "_default/_markup/render-image.html" at <$image.MediaType.SubType>: nil pointer evaluating resource.Resource.MediaType

I found if I rename the image file name from cover.en.png to cover_en.png (along with its reference in markdown), then the error will disappear.

@masakichi masakichi changed the title .Page.Resources.GetMatch returns nil when the image file name ends with en.png .Page.Resources.GetMatch returns nil when the image file name ends with .en.png Feb 20, 2024
@jmooring
Copy link
Member

Please provide image render hook code.

@masakichi
Copy link
Author

Hi @jmooring , thanks for your reply. Here it is.

{{ $image := .Page.Resources.GetMatch .Destination }}
{{/* https://github.com/gohugoio/hugo/issues/5030 */}}
{{ if ne $image.MediaType.SubType "gif" }}
  {{/* https://discourse.gohugo.io/t/image-conversion-without-resizing/32429 */}}
  {{ $image = $image.Resize (print $image.Width "x" $image.Height " webp") }}
{{ end }}

{{ if .IsBlock }}
  <figure>
    <img
      src="{{ $image.RelPermalink }}"
      alt="{{ .Text }}"
      width="{{ $image.Width }}"
      height="{{ $image.Height }}"
      loading="lazy"
      {{ with .Title }}title="{{ . }}"{{ end }}
    />
    <figcaption>{{ with .Title }}{{ . | safeHTML }}{{ else }}{{ .Text | safeHTML }}{{ end }}</figcaption>
  </figure>
{{ else }}
  <img
    src="{{ $image.RelPermalink }}"
    alt="{{ .Text }}"
    width="{{ $image.Width }}"
    height="{{ $image.Height }}"
    loading="lazy"
    {{ with .Title }}title="{{ . }}"{{ end }}
  />
  <span>{{ with .Title }}{{ . | safeHTML }}{{ else }}{{ .Text | safeHTML }}{{ end }}</span>
{{ end }}

@bep bep self-assigned this Feb 20, 2024
@bep bep removed the NeedsTriage label Feb 20, 2024
@bep bep added this to the v0.123.1 milestone Feb 20, 2024
@bep
Copy link
Member

bep commented Feb 20, 2024

We could (should) apply the same fallback check as we do in resources.Get -- I assumed that all/most resources.GetMatch would use some kind of wildcard.

But I suspect that this would be more correct:

{{ $image := .Page.Resources.Get .Destination }}

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants