Skip to content

Commit

Permalink
fix: the image URL in the SEO-related tags is incomplete (cotes2020#754)
Browse files Browse the repository at this point in the history
This issue occurs when setting the `img_path` of the post or the `baseurl` of the site configuration.
  • Loading branch information
cotes2020 committed Nov 16, 2022
1 parent ae2669f commit f6e9a3f
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@
{% seo title=false %}
{% endcapture %}

{% if site.img_cdn and seo_tags contains 'og:image' %}
{% assign properties = 'og:image,twitter:image' | split: ',' %}

{% for prop in properties %}
{% if site.img_cdn contains '//' %}
<!-- `site.img_cdn` is a cross-origin URL -->
{% capture target %}<meta property="{{ prop }}" content="{{ site.url }}{% endcapture %}
{% capture replacement %}<meta property="{{ prop }}" content="{{ site.img_cdn }}{% endcapture %}
{% else %}
<!-- `site.img_cdn` is a local file path -->
{% capture target %}<meta property="{{ prop }}" content="{{ site.url }}{{ site.baseurl }}{% endcapture %}
{% assign replacement = target | append: site.img_cdn %}
{% endif %}
{% assign seo_tags = seo_tags | replace: target, replacement %}
{% if page.image.path %}
{% capture target %}"{{ site.url }}{{ page.image.path | relative_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 %}
{% else %}
<!-- it's a local file path -->
{%- capture replacement -%}
"{{ site.url }}{{ site.baseurl }}{{ site.img_cdn }}{{ page.img_path }}{{ page.image.path }}"
{%- endcapture -%}
{% endif %}

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

{% endfor %}
{% endif %}

{{ seo_tags }}
Expand Down

0 comments on commit f6e9a3f

Please sign in to comment.