refactor!: rename media-url file and related parameters (#1651)

- Changed variable `img_cdn` to `cdn` in site configuration file.
- Changed the variable defining the relative path of the image in the post from `img_url` to `media_subpath`
This commit is contained in:
Azamat Mambetov 2024-04-13 00:34:48 +04:00 committed by Cotes Chung
parent 61bdca2db4
commit 9f8aeaadbf
No known key found for this signature in database
GPG key ID: 0D9E54843167A808
13 changed files with 31 additions and 31 deletions

View file

@ -86,12 +86,12 @@ pageviews:
# #
theme_mode: # [light | dark] theme_mode: # [light | dark]
# The CDN endpoint for images. # The CDN endpoint for media resources.
# Notice that once it is assigned, the CDN url # Notice that once it is assigned, the CDN url
# will be added to all image (site avatar & posts' images) paths starting with '/' # will be added to all media resources (site avatar, posts' images, audio and video files) paths starting with '/'
# #
# e.g. 'https://cdn.com' # e.g. 'https://cdn.com'
img_cdn: "https://chirpy-img.netlify.app" cdn: "https://chirpy-img.netlify.app"
# the avatar on sidebar, support local or CORS resources # the avatar on sidebar, support local or CORS resources
avatar: "/commons/avatar.jpg" avatar: "/commons/avatar.jpg"

View file

@ -4,7 +4,7 @@
{% unless src contains '://' %} {% unless src contains '://' %}
{%- capture src -%} {%- capture src -%}
{% include img-url.html src=src %} {% include media-url.html src=src %}
{%- endcapture -%} {%- endcapture -%}
{% endunless %} {% endunless %}

View file

@ -5,14 +5,14 @@
{% unless video_url contains '://' %} {% unless video_url contains '://' %}
{%- capture video_url -%} {%- capture video_url -%}
{% include img-url.html src=video_url %} {% include media-url.html src=video_url %}
{%- endcapture -%} {%- endcapture -%}
{% endunless %} {% endunless %}
{% if poster_url %} {% if poster_url %}
{% unless poster_url contains '://' %} {% unless poster_url contains '://' %}
{%- capture poster_url -%} {%- capture poster_url -%}
{% include img-url.html src=poster_url img_path=page.img_path %} {% include media-url.html src=poster_url subpath=page.media_subpath %}
{%- endcapture -%} {%- endcapture -%}
{% endunless %} {% endunless %}
{% assign poster = 'poster="' | append: poster_url | append: '"' %} {% assign poster = 'poster="' | append: poster_url | append: '"' %}

View file

@ -20,7 +20,7 @@
{% unless src contains '://' %} {% unless src contains '://' %}
{%- capture img_url -%} {%- capture img_url -%}
{% include img-url.html src=src img_path=page.img_path absolute=true %} {% include media-url.html src=src subpath=page.media_subpath absolute=true %}
{%- endcapture -%} {%- endcapture -%}
{%- capture old_url -%}{{ src | absolute_url }}{%- endcapture -%} {%- capture old_url -%}{{ src | absolute_url }}{%- endcapture -%}
@ -31,7 +31,7 @@
{% elsif site.social_preview_image %} {% elsif site.social_preview_image %}
{%- capture img_url -%} {%- capture img_url -%}
{% include img-url.html src=site.social_preview_image absolute=true %} {% include media-url.html src=site.social_preview_image absolute=true %}
{%- endcapture -%} {%- endcapture -%}
{%- capture og_image -%} {%- capture og_image -%}

View file

@ -1,25 +1,25 @@
{%- comment -%} {%- comment -%}
Generate image final URL based on `site.img_cdn`, `page.img_path` Generate media resource final URL based on `site.cdn`, `page.media_subpath`
Arguments: Arguments:
src - required, basic image path src - required, basic media resources path
img_path - optional, relative path of image subpath - optional, relative path of media resources
absolute - optional, boolean, if true, generate absolute URL absolute - optional, boolean, if true, generate absolute URL
Return: Return:
image URL media resources URL
{%- endcomment -%} {%- endcomment -%}
{% assign url = include.src %} {% assign url = include.src %}
{%- if url -%} {%- if url -%}
{% unless url contains ':' %} {% unless url contains ':' %}
{%- comment -%} Add page image path prefix {%- endcomment -%} {%- comment -%} Add media resources subpath prefix {%- endcomment -%}
{% assign url = include.img_path | default: '' | append: '/' | append: url %} {% assign url = include.subpath | default: '' | append: '/' | append: url %}
{%- comment -%} Prepend CND URL {%- endcomment -%} {%- comment -%} Prepend CND URL {%- endcomment -%}
{% if site.img_cdn %} {% if site.cdn %}
{% assign url = site.img_cdn | append: '/' | append: url %} {% assign url = site.cdn | append: '/' | append: url %}
{% endif %} {% endif %}
{% assign url = url | replace: '///', '/' | replace: '//', '/' | replace: ':/', '://' %} {% assign url = url | replace: '///', '/' | replace: '//', '/' | replace: ':/', '://' %}

View file

@ -97,7 +97,7 @@
{% assign _lazyload = true %} {% assign _lazyload = true %}
{%- capture _img_url -%} {%- capture _img_url -%}
{% include img-url.html src=_src img_path=page.img_path %} {% include media-url.html src=_src subpath=page.media_subpath %}
{%- endcapture -%} {%- endcapture -%}
{% assign _path_prefix = _img_url | remove: _src %} {% assign _path_prefix = _img_url | remove: _src %}

View file

@ -5,7 +5,7 @@
<a href="{{ '/' | relative_url }}" id="avatar" class="rounded-circle"> <a href="{{ '/' | relative_url }}" id="avatar" class="rounded-circle">
{%- if site.avatar != empty and site.avatar -%} {%- if site.avatar != empty and site.avatar -%}
{%- capture avatar_url -%} {%- capture avatar_url -%}
{% include img-url.html src=site.avatar %} {% include media-url.html src=site.avatar %}
{%- endcapture -%} {%- endcapture -%}
<img src="{{- avatar_url -}}" width="112" height="112" alt="avatar" onerror="this.style.display='none'"> <img src="{{- avatar_url -}}" width="112" height="112" alt="avatar" onerror="this.style.display='none'">
{%- endif -%} {%- endif -%}

View file

@ -49,7 +49,7 @@ refactor: true
{% if post.image %} {% if post.image %}
{% assign src = post.image.path | default: post.image %} {% assign src = post.image.path | default: post.image %}
{% unless src contains '//' %} {% unless src contains '//' %}
{% assign src = post.img_path | append: '/' | append: src | replace: '//', '/' %} {% assign src = post.media_subpath | append: '/' | append: src | replace: '//', '/' %}
{% endunless %} {% endunless %}
{% assign alt = post.image.alt | xml_escape | default: 'Preview Image' %} {% assign alt = post.image.alt | xml_escape | default: 'Preview Image' %}

View file

@ -247,14 +247,14 @@ The screenshots of the program window can be considered to show the shadow effec
### CDN URL ### CDN URL
If you host the images on the CDN, you can save the time of repeatedly writing the CDN URL by assigning the variable `img_cdn` of `_config.yml`{: .filepath} file: If you host the media resources on the CDN, you can save the time of repeatedly writing the CDN URL by assigning the variable `cdn` of `_config.yml`{: .filepath} file:
```yaml ```yaml
img_cdn: https://cdn.com cdn: https://cdn.com
``` ```
{: file='_config.yml' .nolineno} {: file='_config.yml' .nolineno}
Once `img_cdn` is assigned, the CDN URL will be added to the path of all images (images of site avatar and posts) starting with `/`. Once `cdn` is assigned, the CDN URL will be added to the path of all media resources (site avatar, posts' images, audio and video files) starting with `/`.
For instance, when using images: For instance, when using images:
@ -270,13 +270,13 @@ The parsing result will automatically add the CDN prefix `https://cdn.com` befor
``` ```
{: .nolineno } {: .nolineno }
### Image Path ### Media Subpath
When a post contains many images, it will be a time-consuming task to repeatedly define the path of the images. To solve this, we can define this path in the YAML block of the post: When a post contains many images, it will be a time-consuming task to repeatedly define the path of the media resources. To solve this, we can define this path in the YAML block of the post:
```yml ```yml
--- ---
img_path: /img/path/ media_subpath: /img/path/
--- ---
``` ```
@ -308,7 +308,7 @@ image:
--- ---
``` ```
Note that the [`img_path`](#image-path) can also be passed to the preview image, that is, when it has been set, the attribute `path` only needs the image file name. Note that the [`media_subpath`](#media-subpath) can also be passed to the preview image, that is, when it has been set, the attribute `path` only needs the image file name.
For simple use, you can also just use `image` to define the path. For simple use, you can also just use `image` to define the path.

View file

@ -8,7 +8,7 @@ date: 2019-08-09 20:55:00 +0800
categories: [Blogging, Tutorial] categories: [Blogging, Tutorial]
tags: [getting started] tags: [getting started]
pin: true pin: true
img_path: '/posts/20180809' media_subpath: '/posts/20180809'
--- ---
## Prerequisites ## Prerequisites

View file

@ -24,8 +24,8 @@ const swconf = {
{%- comment -%} The request url with below domain will be cached. {%- endcomment -%} {%- comment -%} The request url with below domain will be cached. {%- endcomment -%}
allowHosts: [ allowHosts: [
{% if site.img_cdn and site.img_cdn contains '//' %} {% if site.cdn and site.cdn contains '//' %}
'{{ site.img_cdn | split: '//' | last | split: '/' | first }}', '{{ site.cdn | split: '//' | last | split: '/' | first }}',
{% endif %} {% endif %}
{%- unless site.assets.self_host.enabled -%} {%- unless site.assets.self_host.enabled -%}

View file

@ -93,7 +93,7 @@ init_files() {
mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW} mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW}
## Cleanup image settings in site config ## Cleanup image settings in site config
_sedi "s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml _sedi "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml
fi fi
# remove the other files # remove the other files

View file

@ -156,7 +156,7 @@ build_gem() {
git checkout "$PROD_BRANCH" git checkout "$PROD_BRANCH"
# Remove unnecessary theme settings # Remove unnecessary theme settings
sed -i "s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml sed -i "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml
rm -f ./*.gem rm -f ./*.gem
npm run build npm run build