Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 71 additions & 23 deletions _includes/media-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,84 @@
Generate media resource final URL based on `site.cdn`, `page.media_subpath`

Arguments:
src - required, basic media resources path
subpath - optional, relative path of media resources
absolute - optional, boolean, if true, generate absolute URL
src - required, basic media resource path
subpath - optional, relative path of media resources
absolute - optional, boolean, force full absolute URL

Return:
media resources URL
{%- endcomment -%}

{% assign url = include.src %}
{%- assign url = include.src -%}

{%- comment -%}
Normalize site.baseurl:
- strip whitespace
- treat "/" as empty (common misconfiguration)
- remove trailing slash for other non-empty values
{%- endcomment -%}

{%- assign b_url = site.baseurl | default: '' | strip -%}
{%- assign last_base_char = b_url | slice: -1, 1 -%}
{%- if b_url == '/' -%}
{%- assign b_url = '' -%}
{%- elsif last_base_char == '/' -%}
{%- assign trimmed_length = b_url | size | minus: 1 -%}
{%- assign b_url = b_url | slice: 0, trimmed_length -%}
{%- endif -%}


{%- comment -%}
Normalize CDN:
- strip whitespace
- remove trailing slash
{%- endcomment -%}
{%- assign cdn = site.cdn | default: '' | strip -%}
{%- assign last_cdn_char = cdn | slice: -1, 1 -%}
{%- if cdn != '' and last_cdn_char == '/' -%}
{%- assign trimmed_cdn_length = cdn | size | minus: 1 -%}
{%- assign cdn = cdn | slice: 0, trimmed_cdn_length -%}
{%- endif -%}

{%- if url -%}
{% unless url contains ':' %}
{%- comment -%} Add media resources subpath prefix {%- endcomment -%}
{% assign url = include.subpath | default: '' | append: '/' | append: url %}

{%- comment -%} Prepend CND URL {%- endcomment -%}
{% if site.cdn %}
{% assign url = site.cdn | append: '/' | append: url %}
{% endif %}

{% assign url = url | replace: '///', '/' | replace: '//', '/' | replace: ':/', '://' %}

{% unless url contains '://' %}
{% if include.absolute %}
{% assign url = site.url | append: site.baseurl | append: url %}
{% else %}
{% assign url = site.baseurl | append: url %}
{% endif %}
{% endunless %}
{% endunless %}
{%- unless url contains '://' -%}

{%- comment -%}Apply optional subpath{%- endcomment -%}
{%- assign url = include.subpath | default: '' | append: '/' | append: url -%}

{%- comment -%}Apply CDN if present{%- endcomment -%}
{%- if cdn != '' -%}
{%- assign url = cdn | append: '/' | append: url -%}
{%- endif -%}

{%- comment -%}Cleanup duplicate slashes{%- endcomment -%}
{%- assign url = url
| replace: '///', '/'
| replace: '//', '/'
| replace: ':/', '://'
-%}

{%- endunless -%}

{%- unless url contains '://' -%}
{%- assign has_baseurl = false -%}
{%- if b_url != '' -%}
{%- assign b_url_size = b_url | size -%}
{%- assign url_prefix = url | slice: 0, b_url_size -%}
{%- assign next_char = url | slice: b_url_size, 1 -%}
{%- if url_prefix == b_url and (next_char == '' or '/#?' contains next_char) -%}
{%- assign has_baseurl = true -%}
{%- endif -%}
{%- endif -%}

{%- if b_url != '' and has_baseurl == false -%}
{%- assign url = b_url | append: url -%}
{%- endif -%}

{%- if include.absolute -%}
{%- assign url = site.url | append: url -%}
{%- endif -%}
{%- endunless -%}
{%- endif -%}

{{- url -}}