From b2b47095ebd0027eac32b098c8b480f69d96cd88 Mon Sep 17 00:00:00 2001
From: Cell <1024@lruihao.cn>
Date: Fri, 23 Feb 2024 10:33:08 +0800
Subject: [PATCH] :bug: Fix: to create a scratch pad that is not reset on
server rebuilds, use the Store method instead of Scratch method (fixes #417)
---
layouts/_default/single.md | 2 +-
layouts/partials/init/patch.html | 30 -------------------
layouts/partials/plugin/reward.html | 8 +++--
layouts/partials/single/post-author.html | 38 +++++++++++++++++++++---
layouts/partials/single/reward.html | 2 +-
layouts/shortcodes/reward.html | 2 +-
6 files changed, 43 insertions(+), 39 deletions(-)
diff --git a/layouts/_default/single.md b/layouts/_default/single.md
index 896459710..7fe4cff81 100644
--- a/layouts/_default/single.md
+++ b/layouts/_default/single.md
@@ -1,5 +1,5 @@
{{- $params := .Scratch.Get "params" -}}
-{{- $author := .Scratch.Get "author" -}}
+{{- $author := .Store.Get "author" -}}
# {{ .Title }}
{{ if $params.password -}}
diff --git a/layouts/partials/init/patch.html b/layouts/partials/init/patch.html
index 5fb01c5a0..af3f71ecc 100644
--- a/layouts/partials/init/patch.html
+++ b/layouts/partials/init/patch.html
@@ -1,36 +1,6 @@
{{- /* FixIt theme patches */ -}}
{{- $params := .Scratch.Get "params" -}}
-{{- /* Author data patch */ -}}
-{{- $authorDefault := dict "name" "Anonymous" "link" "" "email" "" "avatar" "" -}}
-{{- $author := .Site.Params.author | merge $authorDefault -}}
-{{- $authorPost := dict -}}
-{{- $gravatar := .Site.Params.gravatar -}}
-{{- if reflect.IsMap $params.author -}}
- {{- $authorPost = $params.author -}}
-{{- else if isset $params "author" -}}
- {{- $authorPost = dict "name" $params.author -}}
-{{- end -}}
-{{- if isset $authorPost "name" | and (ne $authorPost.name .Site.Params.author.name) -}}
- {{- $author = $authorPost | merge $authorDefault | merge $author -}}
-{{- else -}}
- {{- with $authorPost.link -}}{{ $author = dict "link" . | merge $author }}{{- end -}}
- {{- with $authorPost.email -}}{{ $author = dict "email" . | merge $author }}{{- end -}}
- {{- with $authorPost.avatar -}}{{ $author = dict "avatar" . | merge $author }}{{- end -}}
-{{- end -}}
-
-{{- if $gravatar.enable | and $author.email -}}
- {{- with $gravatar -}}
- {{- $author = dict "avatar" (printf "https://%v/avatar/%v?s=32&d=%v"
- (path.Clean .Host | default "www.gravatar.com")
- (md5 $author.email)
- (.Style | default ""))
- | merge $author
- -}}
- {{- end -}}
-{{- end -}}
-{{- .Scratch.Set "author" $author -}}
-
{{- /* Toc data patch */ -}}
{{- $toc := $params.toc -}}
{{- if eq $toc true -}}
diff --git a/layouts/partials/plugin/reward.html b/layouts/partials/plugin/reward.html
index 9e830f3a0..a5e262ade 100644
--- a/layouts/partials/plugin/reward.html
+++ b/layouts/partials/plugin/reward.html
@@ -1,6 +1,6 @@
{{- $reward := .Reward -}}
{{- $id := .Id -}}
-{{- $author := .Author -}}
+{{- $author := .Author | default "" -}}
{{- if $reward.enable -}}
@@ -12,7 +12,11 @@
{{- with T (printf "single.reward.%v" $way) -}}{{ $way = . }}{{- end -}}
{{- if $image -}}
- {{- dict "Src" $image "Alt" (printf "%v %v" $author $way) | partial "plugin/image.html" -}}
+ {{-
+ dict "Src" $image
+ "Alt" (strings.TrimPrefix " " (printf "%v %v" $author $way))
+ | partial "plugin/image.html"
+ -}}
{{ $way }}
{{- end -}}
diff --git a/layouts/partials/single/post-author.html b/layouts/partials/single/post-author.html
index 81ac927d3..a9321c1f9 100644
--- a/layouts/partials/single/post-author.html
+++ b/layouts/partials/single/post-author.html
@@ -1,11 +1,41 @@
-{{- $params := .Scratch.Get "params" -}}
-{{- $author := .Scratch.Get "author" -}}
+{{- $params := .Params | merge .Site.Params.page -}}
+
+{{- /* Author data patch */ -}}
+{{- $authorDefault := dict "name" "" "link" "" "email" "" "avatar" "" -}}
+{{- $author := .Site.Params.author | merge $authorDefault -}}
+{{- $authorPost := dict -}}
+{{- if reflect.IsMap $params.author -}}
+ {{- $authorPost = $params.author -}}
+{{- else if isset $params "author" -}}
+ {{- warnf "检测到你的 author 参数格式错误,请参考主题文档设置为正常的格式。" -}}
+ {{- $authorPost = dict "name" $params.author -}}
+{{- end -}}
+{{- if isset $authorPost "name" | and (ne $authorPost.name .Site.Params.author.name) -}}
+ {{- $author = $authorPost | merge $authorDefault | merge $author -}}
+{{- else -}}
+ {{- with $authorPost.link -}}{{ $author = dict "link" . | merge $author }}{{- end -}}
+ {{- with $authorPost.email -}}{{ $author = dict "email" . | merge $author }}{{- end -}}
+ {{- with $authorPost.avatar -}}{{ $author = dict "avatar" . | merge $author }}{{- end -}}
+{{- end -}}
+
+{{- $gravatar := .Site.Params.gravatar -}}
+{{- if $gravatar.enable | and $author.email -}}
+ {{- with $gravatar -}}
+ {{- $author = dict "avatar" (printf "https://%v/avatar/%v?s=32&d=%v"
+ (path.Clean .Host | default "www.gravatar.com")
+ (md5 $author.email)
+ (.Style | default ""))
+ | merge $author
+ -}}
+ {{- end -}}
+{{- end -}}
+{{- .Store.Set "author" $author -}}
- {{- $content := $author.name -}}
+ {{- $content := $author.name | default "Anonymous" -}}
{{- $icon := dict "Class" "fa-solid fa-user-circle" -}}
{{- if $author.avatar | and $params.authorAvatar -}}
- {{- $content = printf "%v %v" (dict "Src" $author.avatar "Class" "avatar" "Alt" $author.name | partial "plugin/image.html") $author.name -}}
+ {{- $content = printf "%v %v" (dict "Src" $author.avatar "Class" "avatar" "Alt" $content | partial "plugin/image.html") $content -}}
{{- $icon = "" -}}
{{- end -}}
{{- if $author.link -}}
diff --git a/layouts/partials/single/reward.html b/layouts/partials/single/reward.html
index e5c79c9ea..d7f347117 100644
--- a/layouts/partials/single/reward.html
+++ b/layouts/partials/single/reward.html
@@ -1,4 +1,4 @@
{{- $reward := .Scratch.Get "reward" -}}
-{{- $author := .Scratch.Get "author" -}}
+{{- $author := .Store.Get "author" -}}
{{- $options := dict "Reward" $reward "Id" "fi-reward" "Author" $author.name -}}
{{- partial "plugin/reward.html" $options -}}
diff --git a/layouts/shortcodes/reward.html b/layouts/shortcodes/reward.html
index 182a4d759..8afd1252f 100644
--- a/layouts/shortcodes/reward.html
+++ b/layouts/shortcodes/reward.html
@@ -3,7 +3,7 @@
{{- $alipay := (.Get "alipay") | default (.Get 1) -}}
{{- $paypal := (.Get "paypal") | default (.Get 2) -}}
{{- $bitcoin := (.Get "bitcoin") | default (.Get 3) -}}
-{{- $author := (.Get "author") | default (.Get 4) | default (.Page.Scratch.Get "author").name -}}
+{{- $author := (.Get "author") | default (.Get 4) -}}
{{- $comment := (.Get "comment") | default (.Get 5) -}}
{{- $mode := (.Get "mode") | default (.Get 6) -}}
{{- $reward := dict "enable" true "comment" $comment "mode" $mode -}}