JavaScript embedding doesn't add directory slash importing image paths, breaks images

AI-generated summary

A bug has been reported where JavaScript embedding fails to add a directory slash when importing image paths with relative URLs, resulting in broken images. icaria36 provided an example of the issue and a potential related bug report. pfaffman was able to reproduce the issue and identified the problematic code in topic_embed.rb, suggesting a possible fix by modifying the condition to check if the image URL starts with http.

This looks like… a simple bug?

Note the slash missing in “catmedia”.

I didn’t find a report for this bug, although this one might be related: https://meta.discourse.org/t/embedded-topics-dont-handle-images-properly/37355

Can anyone reproduce? Or is there a fix that we could implement on our side? (other than defining images with absolute URLs, which we could do moving forward but what about the articles already published)

Well, it’s been a while, but yes.

The above code appears to normalize image URLs that start with a / (fixing up the hostname), but not those relative to the current URL.

Maybe that should instead be this?

    if src.present? && !src.start_with?('http')

If I’m right, I think that the code a few lines above has the same bug.

2 Likes