refactor: unify title and description handling in meta files (#329)

This commit is contained in:
Robert Kaussow 2022-02-07 10:57:43 +01:00 committed by GitHub
parent d94859e4ae
commit d2668b43f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 240 additions and 105 deletions

View File

@ -4,8 +4,8 @@
{ {
"id": {{ $index }}, "id": {{ $index }},
"href": "{{ $page.RelPermalink }}", "href": "{{ $page.RelPermalink }}",
"title": {{ (partial "title" $page) | jsonify }}, "title": {{ (partial "utils/title" $page) | jsonify }},
"parent": {{ with $page.Parent }}{{ (partial "title" .) | jsonify }}{{ else }}""{{ end }}, "parent": {{ with $page.Parent }}{{ (partial "utils/title" .) | jsonify }}{{ else }}""{{ end }},
"content": {{ $page.Plain | jsonify }} "content": {{ $page.Plain | jsonify }}
} }
{{ end }} {{ end }}

View File

@ -1,4 +1,9 @@
--- ---
description: >
Geekdoc is a simple Hugo theme for documentations. It is intentionally designed as a fast and lean theme
and may not fit the requirements of complex projects. If a more feature-complete theme is required
there are a lot of good alternatives out there.
geekdocToC: 3 geekdocToC: 3
geekdocTagsToMenu: true geekdocTagsToMenu: true

View File

@ -1,6 +1,5 @@
--- ---
title: Welcome to the documentation title: Welcome to the documentation
description: Geekdoc is a simple Hugo theme for documentations. It is intentionally designed as a fast and lean theme and may not fit the requirements of complex projects. If a more feature-complete theme is required there are a lot of good alternatives out there.
geekdocNav: false geekdocNav: false
geekdocAlign: center geekdocAlign: center
geekdocAnchor: false geekdocAnchor: false

View File

@ -3,16 +3,17 @@
<head> <head>
{{ partial "head/meta" . }} {{ partial "head/meta" . }}
<title> <title>
{{ if not (eq .Kind "home") }} {{- if eq .Kind "home" -}}
{{ partial "title" . }}{{ printf " | " }} {{ .Site.Title }}
{{ end }}{{ .Site.Title }} {{- else -}}
{{ printf "%s | %s" (partial "utils/title" .) .Site.Title }}
{{- end -}}
</title> </title>
{{ partial "head/favicons" . }} {{ partial "head/favicons" . }}
{{ partial "head/rel-me" . }} {{ partial "head/rel-me" . }}
{{ partial "head/microformats" . }} {{ partial "head/microformats" . }}
{{ partial "head/others" . }} {{ partial "head/others" . }}
{{ partial "head/schema" . }}
{{ partial "head/custom" . }} {{ partial "head/custom" . }}
</head> </head>

View File

@ -5,7 +5,7 @@
<article <article
class="gdoc-markdown gdoc-markdown__align--{{ default "left" (.Page.Params.GeekdocAlign | lower) }}" class="gdoc-markdown gdoc-markdown__align--{{ default "left" (.Page.Params.GeekdocAlign | lower) }}"
> >
<h1>{{ partial "title" . }}</h1> <h1>{{ partial "utils/title" . }}</h1>
{{ partial "content" . }} {{ partial "utils/content" . }}
</article> </article>
{{ end }} {{ end }}

View File

@ -5,7 +5,7 @@
<article <article
class="gdoc-markdown gdoc-markdown__align--{{ default "left" (.Page.Params.GeekdocAlign | lower) }}" class="gdoc-markdown gdoc-markdown__align--{{ default "left" (.Page.Params.GeekdocAlign | lower) }}"
> >
<h1>{{ partial "title" . }}</h1> <h1>{{ partial "utils/title" . }}</h1>
{{ partial "content" . }} {{ partial "utils/content" . }}
</article> </article>
{{ end }} {{ end }}

View File

@ -1,4 +0,0 @@
{{- $content := .Content -}}
{{- $content = $content | replaceRE `<nav id="TableOfContents">\s*<ul>\s*<li>\s*<ul>` `<nav id="TableOfContents"><ul>` | replaceRE `</ul>\s*</li>\s*</ul>\s*</nav>` `</ul></nav>` | safeHTML -}}
{{- $content = $content | replaceRE `(<table>(?:.|\n)+?</table>)` `<div class=table-wrap> ${1} </div>` | safeHTML -}}
{{- $content -}}

View File

@ -1,13 +1,14 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="referrer" content="no-referrer" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light dark" /> <meta name="color-scheme" content="light dark" />
{{ hugo.Generator }}
{{ $description := default (default .Site.Title .Site.Params.description) (default .Summary .Description) }}
{{ $keywords := default .Site.Params.Keywords .Keywords }} {{ $keywords := default .Site.Params.Keywords .Keywords }}
{{ with $description }} {{- with partial "utils/description" . }}
<meta name="description" content="{{ . }}" /> <meta name="description" content="{{ trim (. | plainify) "\n" }}" />
{{ end }} {{- end }}
{{ with $keywords }} {{- with $keywords }}
<meta name="keywords" content="{{ delimit . "," }}" /> <meta name="keywords" content="{{ delimit . "," }}" />
{{ end }} {{- end }}

View File

@ -1,2 +1,3 @@
{{ template "_internal/opengraph.html" . }} {{ partial "microformats/opengraph.html" . }}
{{ template "_internal/twitter_cards.html" . }} {{ partial "microformats/twitter_cards.html" . }}
{{ partial "microformats/schema" . }}

View File

@ -1,66 +0,0 @@
{{ if .IsHome -}}
{{ $thumbnail := default (default "brand.svg" .Site.Params.logo) (index (default slice .Site.Params.images) 0) | absURL }}<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"name": "{{ .Site.Title }}",
"url": "{{ .Site.BaseURL }}",
{{- with .Site.Params.description }}
"description": "{{ . }}",
{{- end }}
"thumbnailUrl": "{{ $thumbnail }}"
{{- with .Site.Params.GeekdocContentLicense }},
"license": "{{ .name }}"
{{- end }}
}
</script>
{{ else if .IsPage }}
{{ $description := default .Site.Params.description (default .Description .Summary) }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "TechArticle",
"articleSection": "{{ .Section }}",
"name": "{{ .Title | safeJS }}",
"headline": "{{ .Title | safeJS }}",
"alternativeHeadline": "{{ .Params.lead }}",
{{- with $description }}
"description": "{{ . }}",
{{- end }}
"inLanguage": {{ .Site.Language.Lang }},
"isFamilyFriendly": "true",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "{{ .Permalink }}"
},
"copyrightHolder" : "{{ .Site.Title }}",
"copyrightYear" : "{{ .Date.Format "2006" }}",
"dateCreated": "{{ .Date.Format "2006-01-02T15:04:05.00Z" | safeJS }}",
"datePublished": "{{ .PublishDate.Format "2006-01-02T15:04:05.00Z" | safeJS }}",
"dateModified": "{{ .Lastmod.Format "2006-01-02T15:04:05.00Z" | safeJS }}",
"publisher":{
"@type":"Organization",
"name": {{ .Site.Title }},
"url": {{ .Site.BaseURL }},
"logo": {
"@type": "ImageObject",
"url": "{{ (default "brand.svg" .Site.Params.logo) | absURL }}",
"width":"32",
"height":"32"
}
},
{{- with $images := $.Resources.ByType "image" }}
"image": [ {{ range $i, $image := $images }}{{ if $i }}, {{ end }}"{{ $image.Permalink | absURL }}" {{ end }}],
{{- else }}
{{- with $images := .Params.images }}
"image": [ {{ range $i, $image := $images }}{{ if $i }}, {{ end }}"{{ $image | absURL }}" {{ end }}],
{{- end }}
{{- end }}
"url" : "{{ .Permalink }}",
"wordCount" : "{{ .WordCount }}",
{{- with $tags := .Params.tags }}
"genre" : [ {{ range $i, $tag := $tags }}{{ if $i }}, {{ end }}"{{ $tag }}" {{ end }}]
{{- end }}
}
</script>
{{ end }}

View File

@ -52,9 +52,9 @@
{{ else }} {{ else }}
{{ relref $current .ref }} {{ relref $current .ref }}
{{ end }}" {{ end }}"
class="gdoc-nav__entry {{ if not .external }} class="gdoc-nav__entry{{- if not .external }}
{{ if $isCurrent }}is-active{{ end }} {{- if $isCurrent }}{{ printf " is-active" }}{{ end }}
{{ end }}" {{- end }}"
> >
{{ $name }} {{ $name }}
</a> </a>

View File

@ -68,13 +68,15 @@
<span class="flex"> <span class="flex">
<a <a
href="{{ .RelPermalink }}" href="{{ .RelPermalink }}"
class="gdoc-nav__entry {{ if eq $current . }}is-active{{ end }}" class="gdoc-nav__entry{{- if eq $current . }}
{{- printf " is-active" }}
{{- end }}"
> >
{{ partial "title" . }} {{ partial "utils/title" . }}
</a> </a>
</span> </span>
{{ else }} {{ else }}
<span class="flex">{{ partial "title" . }}</span> <span class="flex">{{ partial "utils/title" . }}</span>
{{ end }} {{ end }}
{{ if $doCollapse }} {{ if $doCollapse }}
<svg class="icon toggle gdoc_keyboard_arrow_left"> <svg class="icon toggle gdoc_keyboard_arrow_left">

View File

@ -20,10 +20,12 @@
{{ with $.Site.GetPage (printf "/tags/%s" $name) }} {{ with $.Site.GetPage (printf "/tags/%s" $name) }}
<li> <li>
<a <a
class="gdoc-nav__entry {{ if eq $currentPage .RelPermalink }}is-active{{ end }}" class="gdoc-nav__entry{{- if eq $currentPage .RelPermalink }}
{{- printf " is-active" }}
{{- end }}"
href="{{ .RelPermalink }}" href="{{ .RelPermalink }}"
> >
{{ .Title }} {{ partial "utils/title" . }}
</a> </a>
</li> </li>
{{ end }} {{ end }}

View File

@ -0,0 +1,74 @@
{{- if not (eq .Kind "home") }}
<meta property="og:title" content="{{ partial "utils/title" . }}" />
{{- end }}
{{- with .Site.Title }}
<meta property="og:site_name" content="{{ . }}" />
{{- end }}
{{- with partial "utils/description" . }}
<meta property="og:description" content="{{ trim (. | plainify) "\n" }}" />
{{- end }}
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta property="og:url" content="{{ .Permalink }}" />
{{- with $.Params.images }}
{{- range first 6 . }}<meta property="og:image" content="{{ . | absURL }}" />{{ end -}}
{{- else }}
{{- $featured := ($.Resources.ByType "image").GetMatch "{*feature*,*cover*,*thumbnail*}" -}}
{{- with $featured }}
<meta property="og:image" content="{{ $featured.Permalink }}" />
{{- else }}
{{- with $.Site.Params.images }}
<meta property="og:image" content="{{ index . 0 | absURL }}" />
{{- end }}
{{- end }}
{{- end }}
{{- with .Params.audio }}
<meta property="og:audio" content="{{ . }}" />
{{- end }}
{{- with .Params.locale }}
<meta property="og:locale" content="{{ . }}" />
{{- end }}
{{- with .Params.videos }}
{{- range . }}
<meta property="og:video" content="{{ . | absURL }}" />
{{- end }}
{{- end }}
{{- /* If it is part of a series, link to related articles */}}
{{- if .Site.Taxonomies.series }}
{{- $permalink := .Permalink -}}
{{- $siteSeries := .Site.Taxonomies.series -}}
{{- with .Params.series }}
{{- range $name := . }}
{{- $series := index $siteSeries ($name | urlize) }}
{{- range $page := first 6 $series.Pages }}
{{- if ne $page.Permalink $permalink }}
<meta property="og:see_also" content="{{ $page.Permalink }}" />
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
{{ if .IsPage -}}
{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}}
<meta property="article:section" content="{{ .Section | humanize | title }}" />
{{- with .PublishDate }}
<meta
property="article:published_time"
{{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}
/>
{{- end }}
{{- with .Lastmod }}
<meta
property="article:modified_time"
{{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}
/>
{{- end }}
{{- end }}
{{- /* Facebook Page Admin ID for Domain Insights */}}
{{- with .Site.Social.facebook_admin }}
<meta property="fb:admins" content="{{ . }}" />
{{- end }}

View File

@ -0,0 +1,67 @@
{{- if .IsHome -}}
{{- $thumbnail := default (default "brand.svg" .Site.Params.logo) (index (default slice .Site.Params.images) 0) | absURL }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"name": "{{ .Site.Title }}",
"url": "{{ .Site.BaseURL }}",
{{- with partial "utils/description" . }}
"description": "{{ trim (. | plainify) "\n" }}",
{{- end }}
"thumbnailUrl": "{{ $thumbnail }}"
{{- with .Site.Params.GeekdocContentLicense }},
"license": "{{ .name }}"
{{- end }}
}
</script>
{{- else if .IsPage }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "TechArticle",
"articleSection": "{{ .Section | humanize | title }}",
"name": "{{ (partial "utils/title" .) | safeJS }}",
"headline": "{{ (partial "utils/title" .) | safeJS }}",
"alternativeHeadline": "{{ .Params.lead }}",
{{- with partial "utils/description" . }}
"description": "{{ trim (. | plainify) "\n" }}",
{{- end }}
"inLanguage": {{ .Site.Language.Lang }},
"isFamilyFriendly": "true",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "{{ .Permalink }}"
},
"copyrightHolder" : "{{ .Site.Title }}",
"copyrightYear" : "{{ .Date.Format "2006" }}",
"dateCreated": "{{ .Date.Format "2006-01-02T15:04:05.00Z" | safeJS }}",
"datePublished": "{{ .PublishDate.Format "2006-01-02T15:04:05.00Z" | safeJS }}",
"dateModified": "{{ .Lastmod.Format "2006-01-02T15:04:05.00Z" | safeJS }}",
"publisher":{
"@type":"Organization",
"name": {{ .Site.Title }},
"url": {{ .Site.BaseURL }},
"logo": {
"@type": "ImageObject",
"url": "{{ (default "brand.svg" .Site.Params.logo) | absURL }}",
"width":"32",
"height":"32"
}
},
{{- with $images := $.Resources.ByType "image" }}
"image": [ {{ range $i, $image := $images }}{{ if $i }}, {{ end }}"{{ $image.Permalink | absURL }}" {{ end }}],
{{- else }}
{{- with $images := .Params.images }}
"image": [ {{ range $i, $image := $images }}{{ if $i }}, {{ end }}"{{ $image | absURL }}" {{ end }}],
{{- end }}
{{- end }}
"url" : "{{ .Permalink }}",
"wordCount" : "{{ .WordCount }}",
{{- with $tags := .Params.tags }}
"genre" : [ {{ range $i, $tag := $tags }}{{ if $i }}, {{ end }}"{{ $tag }}" {{ end }}]
{{- end }}
}
</script>
{{- end }}

View File

@ -0,0 +1,28 @@
{{- with $.Params.images -}}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="{{ index . 0 | absURL }}" />
{{- else }}
{{- $images := $.Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- if not $featured }}
{{- $featured = $images.GetMatch "{*cover*,*thumbnail*}" -}}
{{- end }}
{{- with $featured }}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="{{ $featured.Permalink }}" />
{{- else }}
{{- with $.Site.Params.images }}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="{{ index . 0 | absURL }}" />
{{- else }}
<meta name="twitter:card" content="summary" />
{{- end }}
{{- end }}
{{- end }}
<meta name="twitter:title" content="{{ partial "utils/title" . }}" />
{{- with partial "utils/description" . }}
<meta name="twitter:description" content="{{ trim (. | plainify) "\n" }}" />
{{- end }}
{{- with .Site.Social.twitter -}}
<meta name="twitter:site" content="@{{ . }}" />
{{- end }}

View File

@ -9,7 +9,7 @@
{{ define "breadcrumb" }} {{ define "breadcrumb" }}
{{ $parent := .page.Parent }} {{ $parent := .page.Parent }}
{{ if $parent }} {{ if $parent }}
{{ $name := (partial "title" $parent) }} {{ $name := (partial "utils/title" $parent) }}
{{ $position := (sub .position 1) }} {{ $position := (sub .position 1) }}
{{ $value := (printf "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'><a itemscope itemtype='https://schema.org/WebPage' itemprop='item' itemid='%s' href='%s'><span itemprop='name'>%s</span></a><meta itemprop='position' content='%d' /></li><li> / </li>%s" $parent.RelPermalink $parent.RelPermalink $name $position .value) }} {{ $value := (printf "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'><a itemscope itemtype='https://schema.org/WebPage' itemprop='item' itemid='%s' href='%s'><span itemprop='name'>%s</span></a><meta itemprop='position' content='%d' /></li><li> / </li>%s" $parent.RelPermalink $parent.RelPermalink $name $position .value) }}
{{ template "breadcrumb" dict "page" $parent "value" $value "position" $position }} {{ template "breadcrumb" dict "page" $parent "value" $value "position" $position }}
@ -36,7 +36,7 @@
<svg class="icon gdoc_path hidden-mobile"><use xlink:href="#gdoc_path"></use></svg> <svg class="icon gdoc_path hidden-mobile"><use xlink:href="#gdoc_path"></use></svg>
<ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList"> <ol class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
{{ $position := sub (len (split .RelPermalink "/")) 1 }} {{ $position := sub (len (split .RelPermalink "/")) 1 }}
{{ $name := (partial "title" .) }} {{ $name := (partial "utils/title" .) }}
{{ $value := (printf "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'><span itemprop='name'>%s</span><meta itemprop='position' content='%d' /></li>" $name $position ) }} {{ $value := (printf "<li itemprop='itemListElement' itemscope itemtype='https://schema.org/ListItem'><span itemprop='name'>%s</span><meta itemprop='position' content='%d' /></li>" $name $position ) }}
{{ template "breadcrumb" dict "page" . "value" $value "position" $position }} {{ template "breadcrumb" dict "page" . "value" $value "position" $position }}
</ol> </ol>

View File

@ -0,0 +1,6 @@
{{ $content := .Content }}
{{ $content = $content | replaceRE `<nav id="TableOfContents">\s*<ul>\s*<li>\s*<ul>` `<nav id="TableOfContents"><ul>` | replaceRE `</ul>\s*</li>\s*</ul>\s*</nav>` `</ul></nav>` | safeHTML }}
{{ $content = $content | replaceRE `(<table>(?:.|\n)+?</table>)` `<div class=table-wrap> ${1} </div>` | safeHTML }}
{{ return $content }}

View File

@ -0,0 +1,13 @@
{{ $description := "" }}
{{ if .Description }}
{{ $description = .Description }}
{{ else }}
{{ if .IsPage }}
{{ $description = .Summary }}
{{ else if .Site.Params.description }}
{{ $description = .Site.Params.description }}
{{ end }}
{{ end }}
{{ return $description }}

View File

@ -2,7 +2,9 @@
{{ range .Paginator.Pages }} {{ range .Paginator.Pages }}
<article class="gdoc-markdown gdoc-post"> <article class="gdoc-markdown gdoc-post">
<header class="gdoc-post__header"> <header class="gdoc-post__header">
<h1 class="gdoc-post__title"><a href="{{ .RelPermalink }}">{{ .Title }}</a></h1> <h1 class="gdoc-post__title">
<a href="{{ .RelPermalink }}">{{ partial "utils/title" . }}</a>
</h1>
</header> </header>
<section> <section>
{{ .Summary }} {{ .Summary }}

View File

@ -1,13 +1,13 @@
{{ define "main" }} {{ define "main" }}
<article class="gdoc-post"> <article class="gdoc-post">
<header class="gdoc-post__header"> <header class="gdoc-post__header">
<h1 class="gdoc-post__title">{{ .Title }}</h1> <h1 class="gdoc-post__title">{{ partial "utils/title" . }}</h1>
<div class="flex flex-wrap align-center gdoc-post__meta gdoc-post__meta--head"> <div class="flex flex-wrap align-center gdoc-post__meta gdoc-post__meta--head">
{{ partial "posts/metadata.html" . }} {{ partial "posts/metadata.html" . }}
</div> </div>
</header> </header>
<section class="gdoc-markdown"> <section class="gdoc-markdown">
{{ partial "content" . }} {{ partial "utils/content" . }}
</section> </section>
</article> </article>
{{ end }} {{ end }}

View File

@ -17,13 +17,15 @@
{{ if or .Content .Params.GeekdocFlatSection }} {{ if or .Content .Params.GeekdocFlatSection }}
<span> <span>
<a href="{{ .RelPermalink }}" class="gdoc-toc__entry"> <a href="{{ .RelPermalink }}" class="gdoc-toc__entry">
{{ partial "title" . }}{{ with .Params.GeekdocDescription }}:{{ end }} {{ partial "utils/title" . }}{{ with .Params.GeekdocDescription }}:{{ end }}
</a> </a>
{{ with .Params.GeekdocDescription }}{{ . }}{{ end }} {{ with .Params.GeekdocDescription }}{{ . }}{{ end }}
</span> </span>
{{ else }} {{ else }}
<span> <span>
{{ partial "title" . }}{{ with .Params.GeekdocDescription }}: {{ . }}{{ end }} {{ partial "utils/title" . }}{{ with .Params.GeekdocDescription }}
: {{ . }}
{{ end }}
</span> </span>
{{ end }} {{ end }}

View File

@ -2,7 +2,9 @@
{{ range .Paginator.Pages }} {{ range .Paginator.Pages }}
<article class="gdoc-markdown gdoc-post"> <article class="gdoc-markdown gdoc-post">
<header class="gdoc-post__header"> <header class="gdoc-post__header">
<h1 class="gdoc-post__title"><a href="{{ .RelPermalink }}">{{ .Title }}</a></h1> <h1 class="gdoc-post__title">
<a href="{{ .RelPermalink }}">{{ partial "utils/title" . }}</a>
</h1>
</header> </header>
<section> <section>
{{ .Summary }} {{ .Summary }}