|
2 | 2 | Generate media resource final URL based on `site.cdn`, `page.media_subpath` |
3 | 3 |
|
4 | 4 | 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 |
8 | 8 |
|
9 | 9 | Return: |
10 | 10 | media resources URL |
11 | 11 | {%- endcomment -%} |
12 | 12 |
|
13 | | -{% assign url = include.src %} |
| 13 | +{%- assign url = include.src -%} |
14 | 14 |
|
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 -%} |
19 | 27 |
|
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 -%} |
24 | 37 |
|
25 | | - {% assign url = url | replace: '///', '/' | replace: '//', '/' | replace: ':/', '://' %} |
| 38 | +{%- comment -%} |
| 39 | + Only process non-absolute URLs |
| 40 | +{%- endcomment -%} |
| 41 | +{%- unless url contains '://' -%} |
26 | 42 |
|
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 -%} |
36 | 86 |
|
37 | 87 | {{- url -}} |
0 commit comments