Skip to content

Commit

Permalink
refactor: enhance the setting of preview image path
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Nov 18, 2022
1 parent f6e9a3f commit da7d7e2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
11 changes: 6 additions & 5 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@
{% seo title=false %}
{% endcapture %}

{% if page.image.path %}
{% capture target %}"{{ site.url }}{{ page.image.path | relative_url }}"{% endcapture %}
{% if page.image %}
{% assign img = page.image.path | default: page.image %}
{% assign img_path = page.img_path | append: '/' | append: img | replace: '//', '/' %}
{% capture target %}"{{ img | absolute_url }}"{% endcapture %}

{% if site.img_cdn contains '//' %}
<!-- it's a cross-origin URL -->
{% capture replacement %}"{{ site.img_cdn }}{{ page.img_path }}{{ page.image.path }}"{% endcapture %}
{% capture replacement %}"{{ site.img_cdn }}{{ img_path }}"{% endcapture %}
{% else %}
<!-- it's a local file path -->
{%- capture replacement -%}
"{{ site.url }}{{ site.baseurl }}{{ site.img_cdn }}{{ page.img_path }}{{ page.image.path }}"
"{{ site.img_cdn | append: '/' | append: img_path | replace: '//', '/' | absolute_url }}"
{%- endcapture -%}
{% endif %}

{% assign seo_tags = seo_tags | replace: target, replacement %}

{% endif %}

{{ seo_tags }}
Expand Down
8 changes: 1 addition & 7 deletions _includes/refactor-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,7 @@

<!-- Add image path -->
{% if page.img_path %}
{% assign _path = page.img_path %}
{% assign last_char = _path | slice: -1 %}

{% unless last_char == '/' %}
{% assign _path = _path | append: '/' %}
{% endunless %}

{% assign _path = page.img_path | append: '/' | replace: '//', '/' %}
{% assign _src_prefix = _src_prefix | append: _path %}
{% endif %}

Expand Down
4 changes: 2 additions & 2 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ <h1 data-toc-skip>{{ page.title }}</h1>
</span>
{% endif %}

{% if page.image.path %}
{% if page.image %}
{% capture bg %}
{% unless page.image.no_bg %}{{ 'bg' }}{% endunless %}
{% endcapture %}

<div class="mt-3 mb-3">
<img src="{{ page.image.path }}" class="preview-img {{ bg | strip }}"
<img src="{{ page.image.path | default: page.image }}" class="preview-img {{ bg | strip }}"
alt="{{ page.image.alt | default: "Preview Image" }}"

{% if page.image.width %}
Expand Down

0 comments on commit da7d7e2

Please sign in to comment.