From 03690e435915776ffff6b68a5ca96753a3a47c6a Mon Sep 17 00:00:00 2001 From: Chen Xianmin Date: Mon, 4 Nov 2024 16:03:29 +0800 Subject: [PATCH] refactor: Split head.html into multiple files --- layouts/partials/head.html | 159 +----------------- layouts/partials/head/head_analytics.html | 9 + layouts/partials/head/head_compatibility.html | 9 + layouts/partials/head/head_debug.html | 5 + layouts/partials/head/head_meta.html | 44 +++++ layouts/partials/head/head_rss.html | 9 + layouts/partials/head/head_seo.html | 47 ++++++ layouts/partials/head/head_style.html | 21 +++ layouts/partials/head/head_theme_init.html | 6 + 9 files changed, 158 insertions(+), 151 deletions(-) create mode 100644 layouts/partials/head/head_analytics.html create mode 100644 layouts/partials/head/head_compatibility.html create mode 100644 layouts/partials/head/head_debug.html create mode 100644 layouts/partials/head/head_meta.html create mode 100644 layouts/partials/head/head_rss.html create mode 100644 layouts/partials/head/head_seo.html create mode 100644 layouts/partials/head/head_style.html create mode 100644 layouts/partials/head/head_theme_init.html diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 6c24fc6c..4248dfe1 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -1,153 +1,10 @@ - - - - {{- block "title" . -}} - {{ if .IsPage }} - {{ .Title }} - {{ .Site.Title }} - {{ else }} - {{ .Site.Title }} - {{ end }} - {{- end -}} - - -{{/* Only add hreflang tags if site is multilingual */}} -{{ if hugo.IsMultilingual }} - {{/* Add translations */}} - {{ range .Translations }} - - {{ end }} - {{/* Add self-referencing hreflang */}} - -{{ end }} - - - -{{/* support older feature phones -see: https://stackoverflow.com/questions/1988499/meta-tags-for-mobile-should-they-be-used -*/}} - - - -{{/* fit for browsing on mobile devices and PC, -see : https://ziyuan.baidu.com/college/courseinfo?id=156 -*/}} - - - - - - -{{/* Add to homescreen for Chrome on Android */}} - - - -{{- if or .Params.author .Site.Params.author.name .Site.Author.name -}} -{{- $author_id := .Params.author | default .Site.Params.author.name | default .Site.Author.name -}} -{{- $author := (index ($.Site.Data.authors | default dict) $author_id) -}} -{{- $author_lang := (index ($author | default dict) .Site.Language.Lang) -}} -{{- $author_name := $author_lang.name.display | default $author.name.display | default $author_id -}} - -{{- end -}} - -{{- if .Description -}} - -{{ else if .IsPage }} - -{{ else if .Site.Params.description }} - -{{- end -}} - -{{- if .Keywords -}} -{{ $length := len .Keywords | add -1 -}} - -{{ else if .Site.Params.keywords }} -{{ $length := len .Site.Params.keywords | add -1 -}} - -{{- end -}} - - -{{ with .Site.Params.baidu_verification }} -{{ end }} -{{ with .Site.Params.google_verification }} -{{ end }} - - - - - - -{{ with .OutputFormats.Get "RSS" }} -{{ end }} - -{{ if .OutputFormats.Get "jsonfeed" }} - -{{ end }} - - +{{ partial "head/head_meta.html" . }} +{{ partial "head/head_rss.html" . }} +{{ partial "head/head_seo.html" . }} {{ partial "custom_favicon.html" . }} - - -{{- if .Site.Params.debug }} - - -{{- end }} - - -{{ $styleOpts := dict "transpiler" "dartsass" "targetPath" "css/style.css" "vars" site.Params.style}} -{{ $style := resources.Get "sass/jane.scss" | toCSS $styleOpts | minify | fingerprint }} - - - - -{{- if .Site.Params.wallpaper }} - -{{- end }} - - -{{ range .Site.Params.customCSS }} - -{{ end }} - -{{/* NOTE: These Hugo Internal Templates can be found starting at -https://github.com/spf13/hugo/blob/master/tpl/tplimpl/template_embedded.go#L158 */}} -{{- template "_internal/opengraph.html" . -}} -{{- template "_internal/schema.html" . -}} -{{- template "_internal/twitter_cards.html" . -}} - - -{{ `` | safeHTML }} - -{{ `` | safeHTML }} - - -{{- if and (not hugo.IsServer) .Site.Config.Services.GoogleAnalytics.ID -}} -{{ template "_internal/google_analytics.html" . }} -{{- end -}} - - +{{ partial "head/head_debug.html" . }} +{{ partial "head/head_style.html" . }} +{{ partial "head/head_compatibility.html" . }} +{{ partial "head/head_analytics.html" . }} {{ partial "custom_head.html" . }} - -{{/* init theme */}} - +{{ partial "head/head_theme_init.html" . }} diff --git a/layouts/partials/head/head_analytics.html b/layouts/partials/head/head_analytics.html new file mode 100644 index 00000000..1e0c2fe5 --- /dev/null +++ b/layouts/partials/head/head_analytics.html @@ -0,0 +1,9 @@ + +{{ with .Site.Params.baidu_verification }} +{{ end }} +{{ with .Site.Params.google_verification }} +{{ end }} + +{{- if and (not hugo.IsServer) .Site.Config.Services.GoogleAnalytics.ID -}} +{{ template "_internal/google_analytics.html" . }} +{{- end -}} diff --git a/layouts/partials/head/head_compatibility.html b/layouts/partials/head/head_compatibility.html new file mode 100644 index 00000000..6e4c5ad7 --- /dev/null +++ b/layouts/partials/head/head_compatibility.html @@ -0,0 +1,9 @@ + +{{ `` | safeHTML }} + +{{ `` | safeHTML }} diff --git a/layouts/partials/head/head_debug.html b/layouts/partials/head/head_debug.html new file mode 100644 index 00000000..d3ddf0e8 --- /dev/null +++ b/layouts/partials/head/head_debug.html @@ -0,0 +1,5 @@ +{{- if .Site.Params.debug }} + + +{{- end }} diff --git a/layouts/partials/head/head_meta.html b/layouts/partials/head/head_meta.html new file mode 100644 index 00000000..ff70eb16 --- /dev/null +++ b/layouts/partials/head/head_meta.html @@ -0,0 +1,44 @@ + + + + {{- block "title" . -}} + {{ if .IsPage }} + {{ .Title }} - {{ .Site.Title }} + {{ else }} + {{ .Site.Title }} + {{ end }} + {{- end -}} + + +{{/* Only add hreflang tags if site is multilingual */}} +{{ if hugo.IsMultilingual }} + {{/* Add translations */}} + {{ range .Translations }} + + {{ end }} + {{/* Add self-referencing hreflang */}} + +{{ end }} + + + + +{{/* support older feature phones +see: https://stackoverflow.com/questions/1988499/meta-tags-for-mobile-should-they-be-used +*/}} + + + +{{/* fit for browsing on mobile devices and PC, +see : https://ziyuan.baidu.com/college/courseinfo?id=156 +*/}} + + + + + +{{/* Add to homescreen for Chrome on Android */}} + + + + diff --git a/layouts/partials/head/head_rss.html b/layouts/partials/head/head_rss.html new file mode 100644 index 00000000..2ee9a56f --- /dev/null +++ b/layouts/partials/head/head_rss.html @@ -0,0 +1,9 @@ + + +{{ with .OutputFormats.Get "RSS" }} +{{ end }} + +{{ if .OutputFormats.Get "jsonfeed" }} + +{{ end }} diff --git a/layouts/partials/head/head_seo.html b/layouts/partials/head/head_seo.html new file mode 100644 index 00000000..329e8a11 --- /dev/null +++ b/layouts/partials/head/head_seo.html @@ -0,0 +1,47 @@ + +{{- if or .Params.author .Site.Params.author.name .Site.Author.name -}} +{{- $author_id := .Params.author | default .Site.Params.author.name | default .Site.Author.name -}} +{{- $author := (index ($.Site.Data.authors | default dict) $author_id) -}} +{{- $author_lang := (index ($author | default dict) .Site.Language.Lang) -}} +{{- $author_name := $author_lang.name.display | default $author.name.display | default $author_id -}} + +{{- end -}} + +{{- if .Description -}} + +{{ else if .IsPage }} + +{{ else if .Site.Params.description }} + +{{- end -}} + +{{/* Define keywords based on priority: + 1. .Keywords (directly set keywords) + 2. .Params.tags (for blog posts) + 3. Site.Params.keywords (default site keywords) +*/}} +{{ $keywords := slice }} + +{{/* 1. Check for directly set keywords */}} +{{ if .Keywords }} + {{ $keywords = .Keywords }} + +{{/* 2. Check if it's a post page with tags */}} +{{ else if and .IsPage .Params.tags }} + {{ $keywords = .Params.tags }} + +{{/* 3. Fallback to site keywords */}} +{{ else if .Site.Params.keywords }} + {{ $keywords = .Site.Params.keywords }} +{{ end }} + +{{/* Output keywords meta tag if we have any keywords */}} +{{ with $keywords }} + +{{ end }} + +{{/* NOTE: These Hugo Internal Templates can be found starting at +https://github.com/spf13/hugo/blob/master/tpl/tplimpl/template_embedded.go#L158 */}} +{{- template "_internal/opengraph.html" . -}} +{{- template "_internal/schema.html" . -}} +{{- template "_internal/twitter_cards.html" . -}} diff --git a/layouts/partials/head/head_style.html b/layouts/partials/head/head_style.html new file mode 100644 index 00000000..3b286d59 --- /dev/null +++ b/layouts/partials/head/head_style.html @@ -0,0 +1,21 @@ + +{{ $styleOpts := dict "transpiler" "dartsass" "targetPath" "css/style.css" "vars" site.Params.style}} +{{ $style := resources.Get "sass/jane.scss" | toCSS $styleOpts | minify | fingerprint }} + + +{{- if .Site.Params.wallpaper }} + +{{- end }} + + +{{ range .Site.Params.customCSS }} + +{{ end }} diff --git a/layouts/partials/head/head_theme_init.html b/layouts/partials/head/head_theme_init.html new file mode 100644 index 00000000..71d5a6b7 --- /dev/null +++ b/layouts/partials/head/head_theme_init.html @@ -0,0 +1,6 @@ +