Skip to content

Commit 2b72783

Browse files
committed
Fix _include/media-url.html logic
- Handle case where site.baseurl ends in / better - Improve check for pre-existing absolute url so regular url can have colons (unlikely though) - Fix possible side effect of producing white space - IMPORTANT: Only ever prepend baseurl if not already present Signed-off-by: Dolf Starreveld <[email protected]>
1 parent 0fbaa53 commit 2b72783

File tree

1 file changed

+72
-22
lines changed

1 file changed

+72
-22
lines changed

_includes/media-url.html

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,86 @@
22
Generate media resource final URL based on `site.cdn`, `page.media_subpath`
33

44
Arguments:
5-
src - required, basic media resources path
6-
subpath - optional, relative path of media resources
7-
absolute - optional, boolean, if true, generate absolute URL
5+
src - required, basic media resource path
6+
subpath - optional, relative path of media resources
7+
absolute - optional, boolean, force full absolute URL
88

99
Return:
1010
media resources URL
1111
{%- endcomment -%}
1212

13-
{% assign url = include.src %}
13+
{%- assign url = include.src -%}
1414

15-
{%- if url -%}
16-
{% unless url contains ':' %}
17-
{%- comment -%} Add media resources subpath prefix {%- endcomment -%}
18-
{% assign url = include.subpath | default: '' | append: '/' | append: url %}
15+
{%- comment -%}
16+
Normalize site.baseurl:
17+
- strip whitespace
18+
- treat "/" as empty (common misconfiguration)
19+
- remove trailing slash for other non-empty values
20+
{%- endcomment -%}
21+
{%- assign b_url = site.baseurl | default: '' | strip -%}
22+
{%- if b_url == '/' -%}
23+
{%- assign b_url = '' -%}
24+
{%- elsif (b_url | slice: -1, 1) == '/' -%}
25+
{%- assign b_url = b_url | remove_last: '/' -%}
26+
{%- endif -%}
1927

20-
{%- comment -%} Prepend CND URL {%- endcomment -%}
21-
{% if site.cdn %}
22-
{% assign url = site.cdn | append: '/' | append: url %}
23-
{% endif %}
28+
{%- comment -%}
29+
Normalize CDN:
30+
- strip whitespace
31+
- remove trailing slash
32+
{%- endcomment -%}
33+
{%- assign cdn = site.cdn | default: '' | strip -%}
34+
{%- if cdn != '' and (cdn | slice: -1, 1) == '/' -%}
35+
{%- assign cdn = cdn | remove_last: '/' -%}
36+
{%- endif -%}
2437

25-
{% assign url = url | replace: '///', '/' | replace: '//', '/' | replace: ':/', '://' %}
38+
{%- comment -%}
39+
Only process non-absolute URLs
40+
{%- endcomment -%}
41+
{%- unless url contains '://' -%}
2642

27-
{% unless url contains '://' %}
28-
{% if include.absolute %}
29-
{% assign url = site.url | append: site.baseurl | append: url %}
30-
{% else %}
31-
{% assign url = site.baseurl | append: url %}
32-
{% endif %}
33-
{% endunless %}
34-
{% endunless %}
35-
{%- endif -%}
43+
{%- comment -%}Apply optional subpath{%- endcomment -%}
44+
{%- assign url = include.subpath | default: '' | append: '/' | append: url -%}
45+
46+
{%- comment -%}Apply CDN if present{%- endcomment -%}
47+
{%- if cdn != '' -%}
48+
{%- assign url = cdn | append: '/' | append: url -%}
49+
{%- endif -%}
50+
51+
{%- comment -%}Cleanup duplicate slashes{%- endcomment -%}
52+
{%- assign url = url
53+
| replace: '///', '/'
54+
| replace: '//', '/'
55+
| replace: ':/', '://'
56+
-%}
57+
58+
{%- comment -%}
59+
Detect whether URL already has baseurl (boundary-aware).
60+
We only consider it "already prefixed" if:
61+
- url starts with b_url, AND
62+
- the next character is end-of-string or one of "/ # ?"
63+
This avoids false matches like "/blogerati" when b_url is "/blog".
64+
{%- endcomment -%}
65+
{%- assign has_baseurl = false -%}
66+
{%- if b_url != '' -%}
67+
{%- assign b_url_size = b_url | size -%}
68+
{%- assign url_prefix = url | slice: 0, b_url_size -%}
69+
{%- assign next_char = url | slice: b_url_size, 1 -%}
70+
{%- if url_prefix == b_url and (next_char == '' or '/#?' contains next_char) -%}
71+
{%- assign has_baseurl = true -%}
72+
{%- endif -%}
73+
{%- endif -%}
74+
75+
{%- comment -%}Prepend baseurl if needed{%- endcomment -%}
76+
{%- if b_url != '' and has_baseurl == false -%}
77+
{%- assign url = b_url | append: url -%}
78+
{%- endif -%}
79+
80+
{%- comment -%}Make absolute if requested{%- endcomment -%}
81+
{%- if include.absolute -%}
82+
{%- assign url = site.url | append: url -%}
83+
{%- endif -%}
84+
85+
{%- endunless -%}
3686

3787
{{- url -}}

0 commit comments

Comments
 (0)