From 241bb4df7878cff7f82014df660874a1dcddba76 Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Wed, 10 Jan 2024 09:04:27 +0800 Subject: [PATCH] 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. --- _config.yml | 4 +++ _includes/head.html | 43 +++++++++++++++++++-------------- _includes/img-url.html | 24 ++++++++++++++++++ _includes/refactor-content.html | 23 +++++------------- _includes/sidebar.html | 18 +++++--------- 5 files changed, 65 insertions(+), 47 deletions(-) create mode 100644 _includes/img-url.html diff --git a/_config.yml b/_config.yml index 05044ca..4ab1726 100644 --- a/_config.yml +++ b/_config.yml @@ -75,6 +75,10 @@ img_cdn: "https://chirpy-img.netlify.app" # the avatar on sidebar, support local or CORS resources avatar: "/commons/avatar.jpg" +# The URL of the site-wide social preview image used in SEO `og:image` meta tag. +# It can be overridden by a customized `page.image` in front matter. +social_preview_image: # string, local or CORS resources + # boolean type, the global switch for TOC in posts. toc: true diff --git a/_includes/head.html b/_includes/head.html index fb12d99..3885ef2 100644 --- a/_includes/head.html +++ b/_includes/head.html @@ -1,5 +1,3 @@ - -
@@ -11,29 +9,38 @@ content="width=device-width, user-scalable=no initial-scale=1, shrink-to-fit=no, viewport-fit=cover" > - {% capture seo_tags %} + {%- capture seo_tags -%} {% seo title=false %} - {% endcapture %} + {%- endcapture -%} + + {% if page.image %} - {% assign img = page.image.path | default: page.image %} + {% assign src = page.image.path | default: page.image %} - {% unless img contains '://' %} - {% assign img_path = page.img_path | append: '/' | append: img | replace: '//', '/' %} - {% capture target %}"{{ img | absolute_url }}"{% endcapture %} + {% unless src contains '://' %} + {%- capture img_url -%} + {% include img-url.html src=src img_path=page.img_path %} + {%- endcapture -%} - {% if site.img_cdn contains '//' %} - - {% capture replacement %}"{{ site.img_cdn }}{{ img_path }}"{% endcapture %} - {% else %} - - {%- capture replacement -%} - "{{ site.img_cdn | append: '/' | append: img_path | replace: '//', '/' | absolute_url }}" - {%- endcapture -%} - {% endif %} + {%- capture old_url -%}{{ src | absolute_url }}{%- endcapture -%} + {%- capture new_url -%}{{ img_url }}{%- endcapture -%} - {% assign seo_tags = seo_tags | replace: target, replacement %} + {% assign seo_tags = seo_tags | replace: old, new %} {% endunless %} + + {% elsif site.social_preview_image %} + {%- capture img_url -%} + {% include img-url.html src=site.social_preview_image %} + {%- endcapture -%} + + {%- capture og_image -%} + + {%- endcapture -%} + + {% assign old_meta_clip = ' - {% if site.img_cdn %} - {% if site.img_cdn contains '//' %} - {% assign _path_prefix = site.img_cdn %} - {% else %} - {% assign _path_prefix = site.img_cdn | relative_url %} - {% endif %} - {% else %} - {% assign _path_prefix = site.baseurl %} - {% endif %} - - - {% if page.img_path %} - {% assign _path = page.img_path | append: '/' | replace: '//', '/' %} - {% assign _path_prefix = _path_prefix | append: _path %} - {% endif %} - {% for _img_snippet in _img_snippets %} {% if forloop.first %} {% assign _img_content = _img_snippet %} @@ -113,6 +96,12 @@ {% assign _final_src = null %} {% assign _lazyload = true %} + {%- capture _img_url -%} + {% include img-url.html src=_src img_path=page.img_path %} + {%- endcapture -%} + + {% assign _path_prefix = _img_url | remove: _src %} + {% unless _src contains '//' %} {% assign _final_src = _path_prefix | append: _src %} {% assign _src_alt = 'src="' | append: _path_prefix %} diff --git a/_includes/sidebar.html b/_includes/sidebar.html index 99d6dcc..b8f92e3 100644 --- a/_includes/sidebar.html +++ b/_includes/sidebar.html @@ -3,18 +3,12 @@