Skip to content

Commit 241bb4d

Browse files
authored
feat: add site-wide social preview image settings (#1463)
- Add configuration field `social_preview_image` that sets the site-wide default social preview image. For pages that do not have `page.image` set in font matter, the seo tag will use the image specified by `site.social_preview_image` as the Open Graph image. - Refactored the generation of image URLs to reduce redundant code and enhance fault tolerance for missing or repeated slash `/` when defining image paths.
1 parent 82d8f2d commit 241bb4d

File tree

5 files changed

+68
-50
lines changed

5 files changed

+68
-50
lines changed

_config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ img_cdn: "https://chirpy-img.netlify.app"
7575
# the avatar on sidebar, support local or CORS resources
7676
avatar: "/commons/avatar.jpg"
7777

78+
# The URL of the site-wide social preview image used in SEO `og:image` meta tag.
79+
# It can be overridden by a customized `page.image` in front matter.
80+
social_preview_image: # string, local or CORS resources
81+
7882
# boolean type, the global switch for TOC in posts.
7983
toc: true
8084

_includes/head.html

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
<!-- The Head -->
2-
31
<head>
42
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
53
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#f7f7f7">
@@ -11,29 +9,38 @@
119
content="width=device-width, user-scalable=no initial-scale=1, shrink-to-fit=no, viewport-fit=cover"
1210
>
1311

14-
{% capture seo_tags %}
12+
{%- capture seo_tags -%}
1513
{% seo title=false %}
16-
{% endcapture %}
14+
{%- endcapture -%}
15+
16+
<!-- Setup Open Graph image -->
1717

1818
{% if page.image %}
19-
{% assign img = page.image.path | default: page.image %}
20-
21-
{% unless img contains '://' %}
22-
{% assign img_path = page.img_path | append: '/' | append: img | replace: '//', '/' %}
23-
{% capture target %}"{{ img | absolute_url }}"{% endcapture %}
24-
25-
{% if site.img_cdn contains '//' %}
26-
<!-- it's a cross-origin URL -->
27-
{% capture replacement %}"{{ site.img_cdn }}{{ img_path }}"{% endcapture %}
28-
{% else %}
29-
<!-- it's a local file path -->
30-
{%- capture replacement -%}
31-
"{{ site.img_cdn | append: '/' | append: img_path | replace: '//', '/' | absolute_url }}"
32-
{%- endcapture -%}
33-
{% endif %}
34-
35-
{% assign seo_tags = seo_tags | replace: target, replacement %}
19+
{% assign src = page.image.path | default: page.image %}
20+
21+
{% unless src contains '://' %}
22+
{%- capture img_url -%}
23+
{% include img-url.html src=src img_path=page.img_path %}
24+
{%- endcapture -%}
25+
26+
{%- capture old_url -%}{{ src | absolute_url }}{%- endcapture -%}
27+
{%- capture new_url -%}{{ img_url }}{%- endcapture -%}
28+
29+
{% assign seo_tags = seo_tags | replace: old, new %}
3630
{% endunless %}
31+
32+
{% elsif site.social_preview_image %}
33+
{%- capture img_url -%}
34+
{% include img-url.html src=site.social_preview_image %}
35+
{%- endcapture -%}
36+
37+
{%- capture og_image -%}
38+
<meta property="og:image" content="{{ img_url }}" />
39+
{%- endcapture -%}
40+
41+
{% assign old_meta_clip = '<meta name="twitter:card"' %}
42+
{% assign new_meta_clip = og_image | append: old_meta_clip %}
43+
{% assign seo_tags = seo_tags | replace: old_meta_clip, new_meta_clip %}
3744
{% endif %}
3845

3946
{{ seo_tags }}

_includes/img-url.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{%- comment -%}
2+
Generate image final URL based on `site.img_cdn`, `page.img_path`
3+
4+
Arguments:
5+
src - basic image path, required
6+
img_path - relative path of image, optional
7+
8+
Return:
9+
image URL
10+
{%- endcomment -%}
11+
12+
{% assign url = include.src %}
13+
14+
{%- if url -%}
15+
{%- comment -%} CND URL {%- endcomment -%}
16+
{% assign prefix = site.img_cdn | default: '' | relative_url %}
17+
18+
{%- comment -%} Add page image path prefix {%- endcomment -%}
19+
{% assign url = include.img_path | default: '' | append: '/' | append: url %}
20+
21+
{% assign url = prefix | append: '/' | append: url | replace: '///', '/' | replace: '//', '/' | replace: ':', ':/' %}
22+
{%- endif -%}
23+
24+
{{- url -}}

_includes/refactor-content.html

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,6 @@
4848
{% assign _img_content = null %}
4949
{% assign _img_snippets = _content | split: IMG_TAG %}
5050

51-
<!-- CDN URL -->
52-
{% if site.img_cdn %}
53-
{% if site.img_cdn contains '//' %}
54-
{% assign _path_prefix = site.img_cdn %}
55-
{% else %}
56-
{% assign _path_prefix = site.img_cdn | relative_url %}
57-
{% endif %}
58-
{% else %}
59-
{% assign _path_prefix = site.baseurl %}
60-
{% endif %}
61-
62-
<!-- Add image path -->
63-
{% if page.img_path %}
64-
{% assign _path = page.img_path | append: '/' | replace: '//', '/' %}
65-
{% assign _path_prefix = _path_prefix | append: _path %}
66-
{% endif %}
67-
6851
{% for _img_snippet in _img_snippets %}
6952
{% if forloop.first %}
7053
{% assign _img_content = _img_snippet %}
@@ -113,6 +96,12 @@
11396
{% assign _final_src = null %}
11497
{% assign _lazyload = true %}
11598

99+
{%- capture _img_url -%}
100+
{% include img-url.html src=_src img_path=page.img_path %}
101+
{%- endcapture -%}
102+
103+
{% assign _path_prefix = _img_url | remove: _src %}
104+
116105
{% unless _src contains '//' %}
117106
{% assign _final_src = _path_prefix | append: _src %}
118107
{% assign _src_alt = 'src="' | append: _path_prefix %}

_includes/sidebar.html

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@
33
<aside aria-label="Sidebar" id="sidebar" class="d-flex flex-column align-items-end">
44
<header class="profile-wrapper">
55
<a href="{{ '/' | relative_url }}" id="avatar" class="rounded-circle">
6-
{% if site.avatar != empty and site.avatar %}
7-
{% capture avatar_url %}
8-
{% if site.avatar contains '://' %}
9-
{{ site.avatar }}
10-
{% elsif site.img_cdn != empty and site.img_cdn %}
11-
{{ site.avatar | prepend: site.img_cdn }}
12-
{% else %}
13-
{{ site.avatar | relative_url }}
14-
{% endif %}
15-
{% endcapture %}
16-
<img src="{{ avatar_url | strip }}" width="112" height="112" alt="avatar" onerror="this.style.display='none'">
17-
{% endif %}
6+
{%- if site.avatar != empty and site.avatar -%}
7+
{%- capture avatar_url -%}
8+
{% include img-url.html src=site.avatar %}
9+
{%- endcapture -%}
10+
<img src="{{- avatar_url -}}" width="112" height="112" alt="avatar" onerror="this.style.display='none'">
11+
{%- endif -%}
1812
</a>
1913

2014
<h1 class="site-title">

0 commit comments

Comments
 (0)