Duplicated path in RSS(index.xml) when using RelPermalink in Image render hooks

My blog’s content structure looks like this:

- content
  - posts
    - hello_world
      - index.md
      - foo.jpg

Now I can easily reference images in index.md like this:

![foo](foo.jpg)

But the problem is that when I tried to have preview of this post in home page, browser won’t be able to found the image because <img> in html generated by Hugo used a relative src by default, which is <img src="foo.jpg"> in this case. There’s nothing at <website>/foo.jpg !

To address this problem, I write a Image render hooks in my theme in layouts/_default/_markup/render-image.html:

{{- $img := .Page.Resources.GetMatch (.Destination | safeURL) -}}
{{- if $img -}}
  {{- $resized := $img.Process "webp q75" -}}
  <img src="{{ $resized.RelPermalink }}" alt="{{ .Text }}" {{ with .Title }} title="{{ . }}"{{ end }}>
{{- else -}}
  <img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" {{ with .Title }} title="{{ . }}"{{ end }}>
{{- end -}}

To prevent original jpg images being published, I added:

build:
  publishResources: false

in the front matter of the post.

Everything looks good to me at first. In post page, it generated <img src="/blog/posts/hello_world/foo_hu17796280442050758554.webp">, same as in index.html.

But the problem is that it creates duplicated image link in index.xml, something looks like this:

<description>&lt;p&gt;hello_world&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://xxx.github.io/blog/blog/posts/hello_world/foo_hu17796280442050758554.webp&#34; alt=&#34;hello&#34; &gt;&lt;/p&gt;</description>

Here’s my hugo.toml:

baseURL = 'https://xxx.github.io/blog'

Did I missed something?

I use Permalink as a temporarily workaround, but isn’t a bug or something?

No, it’s not a bug. Is your site mulitlingual?

No, but I deploy it on Github Pages, with /blog in path.

What does that mean? Is it part of the baseURL?

Yeah, that is.

Using your render hook, I am unable to reproduce the problem as described. Try it:

git clone --single-branch -b hugo-forum-topic-53340 https://github.com/jmooring/hugo-testing hugo-forum-topic-53340
cd hugo-forum-topic-53340
hugo server

Then visit http://localhost:1313/blog/

I’m sorry, but the problem exists in index.xml instead of index.html:

My Hugo version:

hugo v0.141.0+extended+withdeploy linux/amd64 BuildDate=unknown VendorInfo=nixpkgs

Yes, it is.

We’re doing some transformations with RSS content and it’s fouling things up.

For now, copy your render hook to layouts/_default/_markup/render-image.rss.xml, then change $resized.RelPermalink to $resized.Permalink

2 Likes

You can reproduce this with the HTML output format by enabling canonifyURLs in your site config, a setting that I hope will be deprecated someday:

https://github.com/gohugoio/hugo/issues/4733

Please thumbs up the issue.

1 Like

https://github.com/gohugoio/hugo/issues/13332

1 Like

This has been fixed, and will be available in the next release. Thanks bep.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.