Skip to content
Closed
Show file tree
Hide file tree
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: 72 additions & 22 deletions _includes/media-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,86 @@
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 -%}

{%- if url -%}
{% unless url contains ':' %}
{%- comment -%} Add media resources subpath prefix {%- endcomment -%}
{% assign url = include.subpath | default: '' | append: '/' | append: url %}
{%- 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 -%}
{%- if b_url == '/' -%}
{%- assign b_url = '' -%}
{%- elsif (b_url | slice: -1, 1) == '/' -%}
{%- assign b_url = b_url | remove_last: '/' -%}
{%- endif -%}

{%- comment -%} Prepend CND URL {%- endcomment -%}
{% if site.cdn %}
{% assign url = site.cdn | append: '/' | append: url %}
{% endif %}
{%- comment -%}
Normalize CDN:
- strip whitespace
- remove trailing slash
{%- endcomment -%}
{%- assign cdn = site.cdn | default: '' | strip -%}
{%- if cdn != '' and (cdn | slice: -1, 1) == '/' -%}
{%- assign cdn = cdn | remove_last: '/' -%}
{%- endif -%}

{% assign url = url | replace: '///', '/' | replace: '//', '/' | replace: ':/', '://' %}
{%- comment -%}
Only process non-absolute URLs
{%- endcomment -%}
{%- unless url contains '://' -%}

{% 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 %}
{%- endif -%}
{%- 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: ':/', '://'
-%}

{%- comment -%}
Detect whether URL already has baseurl (boundary-aware).
We only consider it "already prefixed" if:
- url starts with b_url, AND
- the next character is end-of-string or one of "/ # ?"
This avoids false matches like "/blogerati" when b_url is "/blog".
{%- endcomment -%}
{%- 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 -%}

{%- comment -%}Prepend baseurl if needed{%- endcomment -%}
{%- if b_url != '' and has_baseurl == false -%}
{%- assign url = b_url | append: url -%}
{%- endif -%}

{%- comment -%}Make absolute if requested{%- endcomment -%}
{%- if include.absolute -%}
{%- assign url = site.url | append: url -%}
{%- endif -%}

{%- endunless -%}

{{- url -}}
16 changes: 4 additions & 12 deletions _layouts/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,10 @@
{% assign card_body_col = '12' %}

{% if post.image %}
{% assign src = post.image.path | default: post.image %}

{% if post.media_subpath %}
{% unless src contains '://' %}
{% assign src = post.media_subpath
| append: '/'
| append: src
| replace: '///', '/'
| replace: '//', '/'
%}
{% endunless %}
{% endif %}
{% assign _src_url = post.image.path | default: post.image %}
{% capture src %}
{%- include media-url.html src=_src_url subpath=post.media_subpath -%}
{% endcapture %}

{% if post.image.lqip %}
{% assign lqip = post.image.lqip %}
Expand Down
6 changes: 5 additions & 1 deletion _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ <h1 data-toc-skip>{{ page.title }}</h1>
{% endif %}

{% if page.image %}
{% capture src %}src="{{ page.image.path | default: page.image }}"{% endcapture %}
{% assign _src_url = page.image.path | default: page.image %}
{% capture _img_url %}
{%- include media-url.html src=_src_url subpath=page.media_subpath -%}
{% endcapture %}
{% capture src %}src="{{ _img_url }}"{% endcapture %}
{% capture class %}class="preview-img{% if page.image.no_bg %}{{ ' no-bg' }}{% endif %}"{% endcapture %}
{% capture alt %}alt="{{ page.image.alt | xml_escape | default: "Preview Image" }}"{% endcapture %}

Expand Down
Loading