Allow relative WebP image embed in markdown
What does this MR do and why?
This MR fixes a bug where embedding a WebP image that is part of the repository with a relative path in markdown causes the image to not show up.
All image embeds of WebPs in a repository, which don’t start with a URL like https://gitlab.com/group/project/-/raw/main/
followed by the path to the file, will currently not embed that WebP correctly. This is because the generated URL for the top webp in the following example points to its blob
URL and not the raw
one.
![Doesn't work](step_on_ic.webp)
<!-- becomes http://127.0.0.1:3000/root/webp-test/-/blob/main/step_on_ic.webp -->
![Works](http://127.0.0.1:3000/root/webp-test/-/raw/main/step_on_ic.webp)
<!-- stays as http://127.0.0.1:3000/root/webp-test/-/raw/main/step_on_ic.webp -->
The bug is caused by an inconsistency in what is considered an image. In !51088 (merged), we previously added support for WebP image attachments in markdown but I did not update the image?
method in lib/gitlab/blob_helper.rb
. The latter is used for determining whether a file in a repository is an image.
In summary, an <img>
is generated for the WebP because it is considered a “safe image” (see lib/gitlab/file_type_detection.rb
). But it gets the wrong URL since the file inside the repository is considered a blob
and not an image
(see lib/gitlab/blob_helper.rb
).
Screenshots
Before | After |
---|---|
How to set up and validate locally
- Clone https://gitlab.com/zipdox/git-readme-embed-test/ to your local instance
- (alternatively copy
README.md
and thestep_on_ic.webp
to a new project)
- Go to the project root
- See that both images are now rendered correctly
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Related to #340666 (closed)