From 8930731ccc6cbdc12a636e07a0e4642a24f0c372 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Thu, 17 Jul 2025 20:38:51 +0800 Subject: [PATCH] refactor(single.html): modularize author and fix the multilingual link when author is at bottom --- layouts/_default/single.html | 108 +++++++++++++++-------------------- 1 file changed, 45 insertions(+), 63 deletions(-) diff --git a/layouts/_default/single.html b/layouts/_default/single.html index d9378bb2..0487403c 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -26,48 +26,8 @@ {{ partial "article-meta/basic.html" (dict "context" . "scope" "single") }} - {{ $authorsData := .Site.Data.authors }} - {{ $taxonomies := .Site.Taxonomies.authors }} - {{ $baseURL := .Site.BaseURL }} - {{ $taxonomyLink := 0 }} - {{ $showAuthor := 0 }} - - {{ if not (strings.HasSuffix $baseURL "/") }} - {{ $baseURL = delimit (slice $baseURL "/") "" }} - {{ end }} - {{ if not (.Params.showAuthorBottom | default ( .Site.Params.article.showAuthorBottom | default false)) }} - - {{ if .Params.showAuthor | default (.Site.Params.article.showAuthor | default true) }} - {{ $showAuthor = 1 }} - {{ partial "author.html" . }} - {{ end }} - - {{ range $author := .Page.Params.authors }} - {{ $authorData := index $authorsData $author }} - {{- if $authorData -}} - {{ range $taxonomyname, $taxonomy := $taxonomies }} - {{ if (eq $taxonomyname $author) }} - {{ $taxonomyLink = delimit (slice $baseURL "authors/" $author "/") "" }} - {{ end }} - {{ end }} - - {{ $finalLink := $taxonomyLink }} - {{ $currentLang := $.Site.Language.Lang }} - {{ if eq $.Site.LanguagePrefix "" }} - {{ $finalLink = printf "%sauthors/%s/" $baseURL $author }} - {{ else }} - {{ $finalLink = printf "%s%s/authors/%s/" $baseURL $currentLang $author }} - {{ end }} - - {{ partial "author-extra.html" (dict "context" . "data" $authorData "link" $finalLink) }} - {{- end -}} - {{ end }} - - {{ if or $taxonomyLink $showAuthor }} -
- {{ end }} - + {{ template "SingleAuthor" . }} {{ end }} @@ -120,28 +80,7 @@ {{ if (.Params.showAuthorBottom | default ( .Site.Params.article.showAuthorBottom | default false)) }} - - {{ if .Params.showAuthor | default (.Site.Params.article.showAuthor | default true) }} - {{ $showAuthor = 1 }} - {{ partial "author.html" . }} - {{ end }} - - {{ range $author := .Page.Params.authors }} - {{ $authorData := index $authorsData $author }} - {{- if $authorData -}} - {{ range $taxonomyname, $taxonomy := $taxonomies }} - {{ if (eq $taxonomyname $author) }} - {{ $taxonomyLink = delimit (slice $baseURL "authors/" $author "/") "" }} - {{ end }} - {{ end }} - {{ partial "author-extra.html" (dict "context" . "data" $authorData "link" $taxonomyLink) }} - {{- end -}} - {{ end }} - - {{ if or $taxonomyLink $showAuthor }} - - {{ end }} - + {{ template "SingleAuthor" . }} {{ end }} {{ partial "series/series-closed.html" . }} @@ -184,3 +123,46 @@ {{ end }} + +{{ define "SingleAuthor" }} + {{ $authorsData := .Site.Data.authors }} + {{ $taxonomies := .Site.Taxonomies.authors }} + {{ $baseURL := .Site.BaseURL }} + {{ $taxonomyLink := 0 }} + {{ $showAuthor := 0 }} + {{ $isAuthorBottom := (.Params.showAuthorBottom | default ( .Site.Params.article.showAuthorBottom | default false)) }} + + {{ if not (strings.HasSuffix $baseURL "/") }} + {{ $baseURL = delimit (slice $baseURL "/") "" }} + {{ end }} + + {{ if .Params.showAuthor | default (.Site.Params.article.showAuthor | default true) }} + {{ $showAuthor = 1 }} + {{ partial "author.html" . }} + {{ end }} + + {{ range $author := .Page.Params.authors }} + {{ $authorData := index $authorsData $author }} + {{- if $authorData -}} + {{ range $taxonomyname, $taxonomy := $taxonomies }} + {{ if (eq $taxonomyname $author) }} + {{ $taxonomyLink = delimit (slice $baseURL "authors/" $author "/") "" }} + {{ end }} + {{ end }} + + {{ $finalLink := $taxonomyLink }} + {{ $currentLang := $.Site.Language.Lang }} + {{ if eq $.Site.LanguagePrefix "" }} + {{ $finalLink = printf "%sauthors/%s/" $baseURL $author }} + {{ else }} + {{ $finalLink = printf "%s%s/authors/%s/" $baseURL $currentLang $author }} + {{ end }} + + {{ partial "author-extra.html" (dict "context" . "data" $authorData "link" $finalLink) }} + {{- end -}} + {{ end }} + + {{ if or $taxonomyLink $showAuthor }} + + {{ end }} +{{ end }}