Skip to content

fix: correct the Twitter Card in social share preview #1498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2024
Merged
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
15 changes: 10 additions & 5 deletions _includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

{% unless src contains '://' %}
{%- capture img_url -%}
{% include img-url.html src=src img_path=page.img_path %}
{% include img-url.html src=src img_path=page.img_path absolute=true %}
{%- endcapture -%}

{%- capture old_url -%}{{ src | absolute_url }}{%- endcapture -%}
Expand All @@ -31,15 +31,20 @@

{% elsif site.social_preview_image %}
{%- capture img_url -%}
{% include img-url.html src=site.social_preview_image %}
{% include img-url.html src=site.social_preview_image absolute=true %}
{%- endcapture -%}

{%- capture og_image -%}
<meta property="og:image" content="{{ img_url }}" />
{%- endcapture -%}

{% assign old_meta_clip = '<meta name="twitter:card"' %}
{% assign new_meta_clip = og_image | append: old_meta_clip %}
{%- capture twitter_image -%}
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:image" content="{{ img_url }}" />
{%- endcapture -%}

{% assign old_meta_clip = '<meta name="twitter:card" content="summary" />' %}
{% assign new_meta_clip = og_image | append: twitter_image %}
{% assign seo_tags = seo_tags | replace: old_meta_clip, new_meta_clip %}
{% endif %}

Expand Down Expand Up @@ -85,7 +90,7 @@
{% endif %}

<!-- Bootstrap -->
<link rel="stylesheet" href="{{ site.data.origin[type].bootstrap.css | relative_url}}">
<link rel="stylesheet" href="{{ site.data.origin[type].bootstrap.css | relative_url }}">

<!-- Font Awesome -->
<link rel="stylesheet" href="{{ site.data.origin[type].fontawesome.css | relative_url }}">
Expand Down
13 changes: 10 additions & 3 deletions _includes/img-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Generate image final URL based on `site.img_cdn`, `page.img_path`

Arguments:
src - basic image path, required
img_path - relative path of image, optional
src - required, basic image path
img_path - optional, relative path of image
absolute - optional, boolean, if true, generate absolute URL

Return:
image URL
Expand All @@ -14,7 +15,7 @@
{%- if url -%}
{% unless url contains ':' %}
{%- comment -%} CND URL {%- endcomment -%}
{% assign prefix = site.img_cdn | default: '' | relative_url %}
{% assign prefix = site.img_cdn | default: '' %}

{%- comment -%} Add page image path prefix {%- endcomment -%}
{% assign url = include.img_path | default: '' | append: '/' | append: url %}
Expand All @@ -26,6 +27,12 @@
| replace: '//', '/'
| replace: ':', ':/'
%}

{% if include.absolute %}
{% assign url = url | absolute_url %}
{% else %}
{% assign url = url | relative_url %}
{% endif %}
{% endunless %}
{%- endif -%}

Expand Down