Skip to content

Commit

Permalink
🐛 Fix the featured image can't use external link
Browse files Browse the repository at this point in the history
  • Loading branch information
Ice-Hazymoon committed Jun 10, 2022
1 parent 8f54fdf commit f82aae0
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 92 deletions.
2 changes: 1 addition & 1 deletion assets/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scope": "/",
"start_url": "{{- "/" | relLangURL -}}",
"name": "{{- .Site.Title | safeHTML -}}",
"short_name": "{{- .Site.Params.author | safeHTML -}}",
"short_name": "{{- (or .Site.Params.author .Site.Author.name) | safeHTML -}}",
"description": "{{- .Site.Title | safeHTML -}}",
"icons": [
{
Expand Down
16 changes: 7 additions & 9 deletions exampleSite/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ params:
wordCount: false

# 开启文章内目录,可以在页面内单独设置
# Show table of contents
# You can enable it separately in the page
# Show table of contents, You can enable it separately in the page
toc:
enable: true
# 超过这个字数后将显示目录
Expand Down Expand Up @@ -353,11 +352,9 @@ params:
# backtop button
backtop: true

# 默认文章封面图片
# 图片必须位于 assets 文件夹
# Default post card featured image
# /featured.png in assets folder or https://source.unsplash.com/random
defaultFeaturedImage: false
# 默认文章封面图片,不会显示在内容页面
# Default post card featured image, not displayed on content pages
defaultFeaturedImage: false # false | featured.png in assets folder | https://source.unsplash.com/random

# 自动调整图片大小并兼容 Webp 格式
# Optimize your images and SEO
Expand All @@ -370,11 +367,12 @@ params:
- 1440
webp: true

# 设置推特卡片样式
opengraph:
twitter: # see https://developer.twitter.com/en/docs/twitter-for-websites/cards/guides/getting-started
cardType: summary_large_image # summary or summary_large_image
cardType: summary_large_image # summary | summary_large_image
site: "@Ice_Hazymoon"
image: "featured.png" # in assets folder or set to false
image: "featured.png" # in assets folder | false

author: Ice-Hazymoon
keywords:
Expand Down
41 changes: 27 additions & 14 deletions layouts/partials/article-list/components/with-cover.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
{{- $public_cdn := cond .Site.IsServer "" .Site.Params.publicCDN -}}
{{- $lazyload_image := print $public_cdn ("images/outload.svg" | relURL) -}}

{{- $featuredImage := "" -}}
{{- $processImage := "" -}}
{{- $lazyload := .Site.Params.lazyload -}}
{{- $featuredImageUrl := or .Params.featured_image .Site.Params.defaultFeaturedImage -}}


{{- if (urls.Parse ($featuredImageUrl | safeURL)).Scheme -}}
{{- $featured_image = $featuredImageUrl | safeURL -}}
{{- $processImage = partial "helper/image-process" (dict "Image" $featuredImageUrl "Site" .Site) -}}
{{- $featuredImageUrl = $featuredImageUrl | safeURL -}}
{{- else -}}
{{- $featuredImage = cond (isset .Params "featured_image") (.Resources.GetMatch .Params.featured_image) (resources.Get .Site.Params.defaultFeaturedImage) -}}
{{- $featuredImage := "" -}}
{{- if .Params.featured_image -}}
{{- $featuredImage = .Resources.GetMatch .Params.featured_image -}}
{{- else -}}
{{- $featuredImage = resources.Get .Site.Params.defaultFeaturedImage -}}
{{- end -}}

{{- $processImage = partial "helper/image-process" (dict "Image" $featuredImage "Site" .Site) -}}

{{- $featuredImageUrl = print $public_cdn $processImage.Permalink -}}
Expand All @@ -19,35 +26,41 @@

<a href="{{- .RelPermalink -}}" class="article-cover relative block h-0 w-full transition duration-100 ease-[ease]">
<picture class="noscript-hidden">
{{- with $processImage.SrcsetWebp -}}<source {{ if $lazyload }}data-{{ end }}srcset="{{- . -}}" type="image/webp" />{{- end -}}
{{- if $processImage.Processed -}}
<source {{ if $lazyload }}data-{{ end }}srcset="{{- $processImage.SrcsetWebp -}}" type="image/webp" />
{{- end -}}
<img
{{ if $lazyload }}data-{{ end }}src="{{- $featuredImage -}}"
{{ if $lazyload }}data-{{ end }}src="{{- $featuredImageUrl -}}"
{{ if $lazyload }}src="{{- or $processImage.Placeholder $lazyload_image -}}"{{ end }}
{{ if $lazyload }}data-{{ end }}srcset="{{- $processImage.Srcset -}}"
{{- with .Title -}}alt="{{- . -}}"{{- end -}}
{{- if not $processImage.Placeholder -}}style="background-color: #fff;"{{- end -}}
{{ if $processImage.Processed }}
{{ if $lazyload }}data-{{ end }}srcset="{{- $processImage.Srcset -}}"
{{ end -}}
{{ with .Title }}alt="{{- . -}}"{{ end }}
{{ if not $processImage.Placeholder }}style="background-color: #fff;"{{ end }}
class="noscript-hidden absolute left-0 top-0 h-full w-full object-cover object-center transition duration-300 ease-[ease]"
{{ if $lazyload }}data-lazyload data-lazyload-blur{{ end }}
/>
</picture>

<noscript>
<picture>
{{- with $processImage.SrcsetWebp -}}<source srcset="{{- . -}}" type="image/webp" />{{- end -}}
{{- if $processImage.Processed -}}<source srcset="{{- $processImage.SrcsetWebp -}}" type="image/webp" />{{- end -}}
<img
src="{{- $featuredImageUrl -}}"
{{- with $processImage.Srcset -}}srcset="{{- . -}}"{{- end -}}
{{- if $processImage.Processed -}}srcset="{{- $processImage.Srcset -}}"{{- end -}}
{{- with .Title -}}alt="{{- . -}}"{{- end -}}
{{- if not $processImage.Placeholder -}}style="background-color: #fff;"{{- end -}}
class="absolute left-0 top-0 h-full w-full object-cover object-center transition duration-300 ease-[ease]"
/>
</picture>
</noscript>

<div
class="cover-shadow absolute left-0 top-0 -z-10 hidden h-full w-full bg-cover bg-center bg-no-repeat transition duration-300 ease-[ease] md:block"
style="background-image:url({{- or $processImage.Placeholder $featuredImageUrl -}})"
></div>
{{- if $processImage.Processed -}}
<div
class="cover-shadow absolute left-0 top-0 -z-10 hidden h-full w-full bg-cover bg-center bg-no-repeat transition duration-300 ease-[ease] md:block"
style="background-image:url({{- or $processImage.Placeholder $featuredImageUrl -}})"
></div>
{{- end -}}

<div
class="article-title absolute bottom-0 left-0 z-10 hidden h-4/5 w-full items-end p-5 text-2xl text-white dark:text-darkText sm:flex md:p-10 md:text-3xl"
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/article/components/cover.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="relative">
{{- if or (.Site.Params.defaultFeaturedImage) (.Params.featured_image) -}}
{{- if .Params.featured_image -}}
{{- partial "article/components/with-cover" . }}
{{- else -}}
{{- partial "article/components/no-cover" . }}
Expand Down
35 changes: 17 additions & 18 deletions layouts/partials/article/components/with-cover.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,46 @@
{{- $lazyload_image := print $public_cdn ("images/outload.svg" | relURL) -}}
{{- $Title := or $.Title (T "post.untitled") -}}

{{- $featuredImage := "" -}}
{{- $processImage := "" -}}
{{- $lazyload := .Site.Params.lazyload -}}
{{- $featuredImageUrl := or .Params.featured_image .Site.Params.defaultFeaturedImage -}}
{{- $featuredImageUrl := .Params.featured_image -}}

{{- if (urls.Parse ($featuredImageUrl | safeURL)).Scheme -}}
{{- $featuredImage = $featuredImageUrl | safeURL -}}
{{- $processImage = partial "helper/image-process" (dict "Image" $featuredImageUrl "Site" .Page.Site) -}}
{{- $featuredImageUrl = $featuredImageUrl | safeURL -}}
{{- else -}}
{{- $featuredImage = cond (isset .Params "featured_image") (.Resources.GetMatch .Params.featured_image) (resources.Get .Site.Params.defaultFeaturedImage) -}}
{{- $featuredImage := .Resources.GetMatch .Params.featured_image -}}
{{- $processImage = partial "helper/image-process" (dict "Image" $featuredImage "Site" .Page.Site) -}}

{{- $featuredImageUrl = print $public_cdn $processImage.Permalink -}}
{{- end -}}


<div class="article-cover relative h-0 w-full">
<picture class="noscript-hidden">
{{- with $processImage.SrcsetWebp -}}
{{- if $processImage.Processed -}}
<source {{ if $lazyload }}data-{{ end }}srcset="{{- $processImage.SrcsetWebp -}}" type="image/webp" />
{{- end -}}
<img
{{ if $lazyload }}data-{{ end }}src="{{- $featuredImageUrl -}}"
{{ if $lazyload }}src="{{- or $processImage.Placeholder $lazyload_image -}}"{{ end }}
{{- with $processImage.Width -}}width="{{- . -}}"{{- end -}}
{{- with $processImage.Height -}}height="{{- . -}}"{{- end -}}
{{ if $lazyload }}data-{{ end }}{{- with $processImage.Srcset -}}srcset="{{- . -}}"{{- end -}}
{{- with $Title -}}alt="{{- . -}}"{{- end -}}
{{ if $lazyload }}data-{{ end }}src="{{ $featuredImageUrl }}"
{{ if $lazyload }}src="{{ or $processImage.Placeholder $lazyload_image }}"{{ end }}
{{ if $processImage.Processed }}width="{{ $processImage.Width }}"{{ end }}
{{ if $processImage.Processed }}height="{{ $processImage.Height }}"{{ end }}
{{ if $lazyload }}data-{{ end }}{{- with $processImage.Srcset -}}srcset="{{ . }}"{{ end }}
{{ with $Title }}alt="{{ . }}"{{ end }}
class="absolute left-0 top-0 h-full w-full object-cover object-center"
{{ if $lazyload }}data-lazyload data-lazyload-blur{{ end }}

/>
</picture>
<noscript>
<picture>
{{- with $processImage.SrcsetWebp -}}<source srcset="{{- $processImage.SrcsetWebp -}}" type="image/webp" />{{- end -}}
{{- if $processImage.Processed -}}<source srcset="{{- $processImage.SrcsetWebp -}}" type="image/webp" />{{- end -}}
<img
src="{{- $featuredImageUrl -}}"
{{- with $processImage.Srcset -}}srcset="{{- . -}}"{{- end -}}
{{- with $Title -}}alt="{{- . -}}"{{- end -}}
{{- with $processImage.Width -}}width="{{- . -}}"{{- end -}}
{{- with $processImage.Height -}}height="{{- . -}}"{{- end -}}
src="{{ $featuredImageUrl }}"
{{ if $processImage.Processed }}srcset="{{ $processImage.Srcset }}"{{ end }}
{{ with $Title }}alt="{{ . }}"{{- end -}}
{{ if $processImage.Processed }}width="{{ $processImage.Width }}"{{ end }}
{{ if $processImage.Processed }}height="{{ $processImage.Height }}"{{ end }}
class="absolute left-0 top-0 h-full w-full object-cover object-center"
/>
</picture>
Expand Down
88 changes: 46 additions & 42 deletions layouts/partials/helper/image-process.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,61 @@
{{- $Permalink := "" -}}
{{- $galleryImage := false -}}

{{- $NotSVG := ne $Image.MediaType.SubType "svg" -}}
{{- $NotGIF := ne $Image.MediaType.SubType "gif" -}}
{{- $Width := 0 -}}
{{- $Height := 0 -}}
{{- $Srcset := "" -}}
{{- $SrcsetWebp := "" -}}
{{- $Placeholder := "" -}}
{{- $returnValue := newScratch -}}

{{- if (urls.Parse ($Image | safeURL)).Scheme -}}
{{- $returnValue.SetInMap "returnValue" "Processed" false -}}
{{- else -}}
{{- $NotSVG := ne $Image.MediaType.SubType "svg" -}}
{{- $NotGIF := ne $Image.MediaType.SubType "gif" -}}
{{- $Width := 0 -}}
{{- $Height := 0 -}}
{{- $Srcset := "" -}}
{{- $SrcsetWebp := "" -}}
{{- $Placeholder := "" -}}

{{- $imageProcessing := .Site.Params.imageProcessing -}}
{{- $imageProcessing := .Site.Params.imageProcessing -}}

{{- $returnValue := newScratch -}}
{{- if and $Image (and $NotSVG $NotGIF) -}}
{{- $Permalink = $Image.RelPermalink -}}
{{- $Width = $Image.Width -}}
{{- $Height = $Image.Height -}}
{{- $galleryImage = true -}}
{{- if (default true $imageProcessing.content) -}}
{{- $_flag := false -}}
{{- range $index, $size := sort $imageProcessing.autoResize -}}
{{- if not $_flag -}}
{{- $isLast := or (eq (add $index 1) (len $imageProcessing.autoResize)) (le $Width $size) -}}
{{- $end := print " " . "w" (cond $isLast "" ", ") -}}
{{- $resizeWidth := cond (ge $Width $size) $size $Width -}}

{{- if and $Image (and $NotSVG $NotGIF) -}}
{{- $Permalink = $Image.RelPermalink -}}
{{- $Width = $Image.Width -}}
{{- $Height = $Image.Height -}}
{{- $galleryImage = true -}}
{{- if (default true $imageProcessing.content) -}}
{{- $_flag := false -}}
{{- range $index, $size := sort $imageProcessing.autoResize -}}
{{- if not $_flag -}}
{{- $isLast := or (eq (add $index 1) (len $imageProcessing.autoResize)) (le $Width $size) -}}
{{- $end := print " " . "w" (cond $isLast "" ", ") -}}
{{- $resizeWidth := cond (ge $Width $size) $size $Width -}}

{{- $Srcset = print $Srcset $public_cdn ($Image.Resize (print $resizeWidth "x")).RelPermalink $end -}}

{{- if $imageProcessing.webp -}}
{{- $SrcsetWebp = print $SrcsetWebp $public_cdn ($Image.Resize (print $resizeWidth "x webp")).RelPermalink $end -}}
{{- end -}}
{{- $Srcset = print $Srcset $public_cdn ($Image.Resize (print $resizeWidth "x")).RelPermalink $end -}}

{{- if le $Width $size -}}
{{- $_flag = true -}}
{{- if $imageProcessing.webp -}}
{{- $SrcsetWebp = print $SrcsetWebp $public_cdn ($Image.Resize (print $resizeWidth "x webp")).RelPermalink $end -}}
{{- end -}}

{{- if le $Width $size -}}
{{- $_flag = true -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- $Placeholder = print $public_cdn (($Image.Resize "50x jpg").Filter (images.GaussianBlur 2)).RelPermalink -}}
{{- end -}}

{{- $Placeholder = print $public_cdn (($Image.Resize "50x jpg").Filter (images.GaussianBlur 2)).RelPermalink -}}
{{- $returnValue.SetInMap "returnValue" "Processed" true -}}
{{- $returnValue.SetInMap "returnValue" "Srcset" $Srcset -}}
{{- $returnValue.SetInMap "returnValue" "SrcsetWebp" $SrcsetWebp -}}
{{- $returnValue.SetInMap "returnValue" "Placeholder" $Placeholder -}}
{{- $returnValue.SetInMap "returnValue" "Width" $Width -}}
{{- $returnValue.SetInMap "returnValue" "Height" $Height -}}
{{- $returnValue.SetInMap "returnValue" "NotSVG" $NotSVG -}}
{{- $returnValue.SetInMap "returnValue" "NotGIF" $NotGIF -}}
{{- $returnValue.SetInMap "returnValue" "Permalink" $Permalink -}}
{{- else -}}
{{- $returnValue.SetInMap "returnValue" "Processed" false -}}
{{- end -}}

{{- $returnValue.SetInMap "returnValue" "Processed" true -}}
{{- $returnValue.SetInMap "returnValue" "Srcset" $Srcset -}}
{{- $returnValue.SetInMap "returnValue" "SrcsetWebp" $SrcsetWebp -}}
{{- $returnValue.SetInMap "returnValue" "Placeholder" $Placeholder -}}
{{- $returnValue.SetInMap "returnValue" "Width" $Width -}}
{{- $returnValue.SetInMap "returnValue" "Height" $Height -}}
{{- $returnValue.SetInMap "returnValue" "NotSVG" $NotSVG -}}
{{- $returnValue.SetInMap "returnValue" "NotGIF" $NotGIF -}}
{{- $returnValue.SetInMap "returnValue" "Permalink" $Permalink -}}
{{- else -}}
{{- $returnValue.SetInMap "returnValue" "Processed" false -}}
{{- end -}}

{{- return $returnValue.Get "returnValue" -}}
9 changes: 2 additions & 7 deletions layouts/partials/html/head/site-schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,10 @@
}
},
{{- end -}}
{{- if .Params.author -}}
{{- if or .Params.author .Site.Author.name -}}
"author": {
"@type": "Person",
"name": "{{- .Params.author -}}"
},
{{- else if .Site.Author.name -}}
"author": {
"@type": "Person",
"name": "{{- .Site.Author.name -}}"
"name": "{{- or .Params.author .Site.Author.name -}}"
},
{{- end -}}
"description": "{{- .Description -}}"
Expand Down

0 comments on commit f82aae0

Please sign in to comment.