Skip to content

Commit

Permalink
refactor: Split head.html into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
xianmin committed Nov 4, 2024
1 parent 1df1851 commit 03690e4
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 151 deletions.
159 changes: 8 additions & 151 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
@@ -1,153 +1,10 @@
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
{{- block "title" . -}}
{{ if .IsPage }}
{{ .Title }} - {{ .Site.Title }}
{{ else }}
{{ .Site.Title }}
{{ end }}
{{- end -}}
</title>

{{/* Only add hreflang tags if site is multilingual */}}
{{ if hugo.IsMultilingual }}
{{/* Add translations */}}
{{ range .Translations }}
<link rel="alternate" hreflang="{{ .Site.LanguageCode }}" href="{{ .Permalink }}" />
{{ end }}
{{/* Add self-referencing hreflang */}}
<link rel="alternate" hreflang="{{ .Site.LanguageCode }}" href="{{ .Permalink }}" />
{{ end }}

<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
{{/* support older feature phones
see: https://stackoverflow.com/questions/1988499/meta-tags-for-mobile-should-they-be-used
*/}}
<meta name="MobileOptimized" content="width" />
<meta name="HandheldFriendly" content="true" />

{{/* fit for browsing on mobile devices and PC,
see : https://ziyuan.baidu.com/college/courseinfo?id=156
*/}}
<meta name="applicable-device" content="pc,mobile">

<meta name="color-scheme" content="light dark" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">
{{/* Add to homescreen for Chrome on Android */}}
<meta name="mobile-web-app-capable" content="yes">

<!-- author & description & keywords -->
{{- 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 -}}
<meta name="author" content="{{ $author_name | safeHTML }}" />
{{- end -}}

{{- if .Description -}}
<meta name="description" content="{{ .Description | safeHTML }}" />
{{ else if .IsPage }}
<meta name="description" content="{{ .Summary | plainify }}" />
{{ else if .Site.Params.description }}
<meta name="description" content="{{ .Site.Params.description | safeHTML }}" />
{{- end -}}

{{- if .Keywords -}}
{{ $length := len .Keywords | add -1 -}}
<meta name="keywords"
content="{{ range $index, $element := .Keywords }}{{ $element | safeHTML }}{{if ne $index $length }}, {{ end }}{{ end }}" />
{{ else if .Site.Params.keywords }}
{{ $length := len .Site.Params.keywords | add -1 -}}
<meta name="keywords"
content="{{ range $index, $element := .Site.Params.keywords }}{{ $element | safeHTML }}{{if ne $index $length }}, {{ end }}{{ end }}" />
{{- end -}}

<!-- baidu & google verification -->
{{ with .Site.Params.baidu_verification }}
<meta name="baidu-site-verification" content="{{.}}" />{{ end }}
{{ with .Site.Params.google_verification }}
<meta name="google-site-verification" content="{{.}}" />{{ end }}

<!-- Site Generator -->
<meta name="generator" content="Hugo {{ hugo.Version }}" />

<!-- Permalink & RSSlink -->
<link rel="canonical" href="{{ .Permalink }}" />
{{ with .OutputFormats.Get "RSS" }}
<link href="{{ .RelPermalink }}" rel="alternate" type="application/rss+xml" title="{{ $.Site.Title }}" />{{ end }}

{{ if .OutputFormats.Get "jsonfeed" }}
<link href="{{ with .OutputFormats.Get " jsonfeed" }}{{ .Permalink }}{{ end }}" rel="alternate" type="application/json"
title="{{ .Site.Title }}" />
{{ end }}

<!-- Custom Favicon -->
{{ partial "head/head_meta.html" . }}
{{ partial "head/head_rss.html" . }}
{{ partial "head/head_seo.html" . }}
{{ partial "custom_favicon.html" . }}

<!-- debug -->
{{- if .Site.Params.debug }}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/eruda.min.js"
integrity="sha256-Jmz4bc3kp+rRrXX2tGadNBKFLwtzMapr8kHABxSAAP8=" crossorigin="anonymous"></script>
<script>eruda.init();</script>
{{- end }}

<!-- Jane theme main style -->
{{ $styleOpts := dict "transpiler" "dartsass" "targetPath" "css/style.css" "vars" site.Params.style}}
{{ $style := resources.Get "sass/jane.scss" | toCSS $styleOpts | minify | fingerprint }}
<link rel="stylesheet" href="{{ $style.RelPermalink }}" integrity="{{ $style.Data.Integrity }}" media="screen"
crossorigin="anonymous">
<!-- End -->

<!-- Custom wallpaper (optional) -->
{{- if .Site.Params.wallpaper }}
<style>
.wallpaper {
background-image: url('{{ .Site.Params.wallpaper }}');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
{{- end }}

<!-- custom css -->
{{ range .Site.Params.customCSS }}
<link rel="stylesheet" href="{{ `css/` | relURL }}{{ . }}">
{{ 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" . -}}

<!-- Polyfill for old browsers -->
{{ `<!--[if lte IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->` | safeHTML }}

{{ `<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dest/respond.min.js"></script>
<![endif]-->` | safeHTML }}

<!-- Analytics -->
{{- if and (not hugo.IsServer) .Site.Config.Services.GoogleAnalytics.ID -}}
{{ template "_internal/google_analytics.html" . }}
{{- end -}}

<!-- Custom head -->
{{ 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 */}}
<script>
(function () {
var savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
})();
</script>
{{ partial "head/head_theme_init.html" . }}
9 changes: 9 additions & 0 deletions layouts/partials/head/head_analytics.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- verification -->
{{ with .Site.Params.baidu_verification }}
<meta name="baidu-site-verification" content="{{.}}" />{{ end }}
{{ with .Site.Params.google_verification }}
<meta name="google-site-verification" content="{{.}}" />{{ end }}

{{- if and (not hugo.IsServer) .Site.Config.Services.GoogleAnalytics.ID -}}
{{ template "_internal/google_analytics.html" . }}
{{- end -}}
9 changes: 9 additions & 0 deletions layouts/partials/head/head_compatibility.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- Polyfill for old browsers -->
{{ `<!--[if lte IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->` | safeHTML }}

{{ `<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dest/respond.min.js"></script>
<![endif]-->` | safeHTML }}
5 changes: 5 additions & 0 deletions layouts/partials/head/head_debug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- if .Site.Params.debug }}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/eruda.min.js"
integrity="sha256-Jmz4bc3kp+rRrXX2tGadNBKFLwtzMapr8kHABxSAAP8=" crossorigin="anonymous"></script>
<script>eruda.init();</script>
{{- end }}
44 changes: 44 additions & 0 deletions layouts/partials/head/head_meta.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
{{- block "title" . -}}
{{ if .IsPage }}
{{ .Title }} - {{ .Site.Title }}
{{ else }}
{{ .Site.Title }}
{{ end }}
{{- end -}}
</title>

{{/* Only add hreflang tags if site is multilingual */}}
{{ if hugo.IsMultilingual }}
{{/* Add translations */}}
{{ range .Translations }}
<link rel="alternate" hreflang="{{ .Site.LanguageCode }}" href="{{ .Permalink }}" />
{{ end }}
{{/* Add self-referencing hreflang */}}
<link rel="alternate" hreflang="{{ .Site.LanguageCode }}" href="{{ .Permalink }}" />
{{ end }}

<meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />

{{/* support older feature phones
see: https://stackoverflow.com/questions/1988499/meta-tags-for-mobile-should-they-be-used
*/}}
<meta name="MobileOptimized" content="width" />
<meta name="HandheldFriendly" content="true" />

{{/* fit for browsing on mobile devices and PC,
see : https://ziyuan.baidu.com/college/courseinfo?id=156
*/}}
<meta name="applicable-device" content="pc,mobile">
<meta name="color-scheme" content="light dark" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">
{{/* Add to homescreen for Chrome on Android */}}
<meta name="mobile-web-app-capable" content="yes">

<!-- Site Generator -->
<meta name="generator" content="Hugo {{ hugo.Version }}" />
9 changes: 9 additions & 0 deletions layouts/partials/head/head_rss.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- Permalink & RSSlink -->
<link rel="canonical" href="{{ .Permalink }}" />
{{ with .OutputFormats.Get "RSS" }}
<link href="{{ .RelPermalink }}" rel="alternate" type="application/rss+xml" title="{{ $.Site.Title }}" />{{ end }}

{{ if .OutputFormats.Get "jsonfeed" }}
<link href="{{ with .OutputFormats.Get `jsonfeed` }}{{ .Permalink }}{{ end }}" rel="alternate" type="application/json"
title="{{ .Site.Title }}" />
{{ end }}
47 changes: 47 additions & 0 deletions layouts/partials/head/head_seo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!-- author & description & keywords -->
{{- 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 -}}
<meta name="author" content="{{ $author_name | safeHTML }}" />
{{- end -}}

{{- if .Description -}}
<meta name="description" content="{{ .Description | safeHTML }}" />
{{ else if .IsPage }}
<meta name="description" content="{{ .Summary | plainify }}" />
{{ else if .Site.Params.description }}
<meta name="description" content="{{ .Site.Params.description | safeHTML }}" />
{{- 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 }}
<meta name="keywords" content="{{ delimit . `, ` }}" />
{{ 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" . -}}
21 changes: 21 additions & 0 deletions layouts/partials/head/head_style.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Jane theme main style -->
{{ $styleOpts := dict "transpiler" "dartsass" "targetPath" "css/style.css" "vars" site.Params.style}}
{{ $style := resources.Get "sass/jane.scss" | toCSS $styleOpts | minify | fingerprint }}
<link rel="stylesheet" href="{{ $style.RelPermalink }}" integrity="{{ $style.Data.Integrity }}" media="screen"
crossorigin="anonymous">

{{- if .Site.Params.wallpaper }}
<style>
.wallpaper {
background-image: url('{{ .Site.Params.wallpaper }}');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
</style>
{{- end }}

<!-- custom css -->
{{ range .Site.Params.customCSS }}
<link rel="stylesheet" href="{{ `css/` | relURL }}{{ . }}">
{{ end }}
6 changes: 6 additions & 0 deletions layouts/partials/head/head_theme_init.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
(function () {
var savedTheme = localStorage.getItem('theme') || 'light';
document.documentElement.setAttribute('data-theme', savedTheme);
})();
</script>

0 comments on commit 03690e4

Please sign in to comment.