Skip to content

Fix _include/media-url.html logic#2648

Open
dolfs wants to merge 2 commits intocotes2020:masterfrom
dolfs:media-url-logic-fix
Open

Fix _include/media-url.html logic#2648
dolfs wants to merge 2 commits intocotes2020:masterfrom
dolfs:media-url-logic-fix

Conversation

@dolfs
Copy link
Copy Markdown

@dolfs dolfs commented Jan 23, 2026

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

When baseurl in _config.yml is not empty, without this fix, it can be prepended more than once, which is incorrect.

This fix adds logic to prevent prepending the baseurl when it is already a prefix in the url, but correctly handles cases where it is a prefix, but the url does not follow this prefix with a path/url separator.

For example, when baseurl is set to /blog, but the url starts with /blogger it is not considered "prefixed" and /blog/ is prepended (resulting in /blog/blogger…, but if the url starts with /blog/it is considered "prefixed" and is not prepended again (keeping/resulting in/blog/…`)

A limitation is that if the user supplied a (partial) URL starting with /blog/ and truly intended to produce /blog/blog/, it won't work.

Additional context

Fixes #2639, and:

  • Prevents occasional introduction of white space
  • Check for pre-existing absolute url is made more robust
  • Better handling of scenarios where site.baseurl ends in '/', or accidentally was set to just '/'

@dolfs dolfs force-pushed the media-url-logic-fix branch from 2b72783 to a830fe2 Compare January 23, 2026 05:37
When baseurl in _config.yml is not empty, without this fix, it can be prepended more than once, which is incorrect.

This fix adds logic to prevent prepending the baseurl when it is already a prefix in the url, but correctly handles cases where it is a prefix, but the url does not follow this prefix with a path/url separator.

For example, when baseurl is set to /blog, but the url starts with /blogger it is not considered "prefixed" and /blog/ is prepended (resulting in /blog/blogger…, but if the url starts with /blog/it is considered "prefixed" and is not prepended again (keeping/resulting in/blog/…`)

A limitation is that if the user supplied a (partial) URL starting with /blog/ and truly intended to produce /blog/blog/, it won't work.

Fixes: cotes2020#2639
Signed-off-by: Dolf Starreveld <registration@starreveld.com>
@dolfs dolfs force-pushed the media-url-logic-fix branch from a830fe2 to d9de8d4 Compare January 23, 2026 05:39
dolfs added a commit to dolfs/jekyll-theme-chirpy that referenced this pull request Jan 23, 2026
…riately

Both home and post layouts did some of the work already done by _include/media-url.html. Since that _include was fixed to deal with the double prepend of baseurl, these layouts still had the problem.

Change that so they use the include. Only effective when the previous PR is also merged.

depends-on: PR cotes2020#2648
Signed-off-by: Dolf Starreveld <registration@starreveld.com>
Copy link
Copy Markdown
Owner

@cotes2020 cotes2020 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, thanks for the PR and for your time!

From the first review, I’ve identified two issues:

  1. remove_last is a feature of Liquid 5.2.0, but Jekyll currently only supports Liquid 4.x.
  2. There is a bug in this PR: when site.cdn is not empty, the output media URL results in /<baseurl><cdn>/<subpath>.

@dolfs
Copy link
Copy Markdown
Author

dolfs commented Mar 30, 2026

Changes made.

@cotes2020
Copy link
Copy Markdown
Owner

Changes made.

Where is it? Did you forget to push?

1. remove_last is a feature of Liquid 5.2.0, but Jekyll currently only supports Liquid 4.x.
Changed code to be 4.x compatible.

2. There is a bug in this PR: when site.cdn is not empty, the output media URL results in /<baseurl><cdn>/<subpath>.
Fixed.

Signed-off-by: Dolf Starreveld <registration@starreveld.com>
@dolfs
Copy link
Copy Markdown
Author

dolfs commented Mar 31, 2026

Oops, it should be there no. I am not very experienced with PRs.

@cotes2020
Copy link
Copy Markdown
Owner

cotes2020 commented Mar 31, 2026

After looking deeper into the related issue #2639, I don't think this change/PR is necessary. media-url.html is a snippet that is explicitly called, and whether the src parameter includes the baseurl is something the caller can control. Adding logic to detect the baseurl inside the snippet would not only overcomplicate it but, as you mentioned, we can't be certain if the src path just happens to match the baseurl.

A limitation is that if the user supplied a (partial) URL starting with /blog/ and truly intended to produce /blog/blog/, it won't work.

@cotes2020
Copy link
Copy Markdown
Owner

cotes2020 commented Apr 2, 2026

I've come up with a workaround: when using the link tag or other third-party filters, we can append a specific class (e.g., .remain) to the image. This acts as a flag to tell the media-url filter to skip any modifications.

Change media-url.html to:

{%- assign remain = include.remain | default: false -%}

{%- if url -%}
  {%- unless url contains '://' or remain -%}
    {%- comment -%}
      Handle src URL using existing logic.
      ...
    {%- endcomment -%}
  {%- endunless -%}

{{- url -}}

Usage:

![alt](/path/to/img)  <!-- output: <img src='[site.cdn]/[site.baseurl]/path/to/img'> -->

![alt]({% link /path/to/img %}){: .remain } <!-- output: <img src='/[site.baseurl]/path/to/img'> -->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Site generation duplicates baseurl for img when baseurl not empty

2 participants