sitemap: hide hidden pages from sitemap and SEO indexing #85

This commit is contained in:
Sören Weber 2021-09-09 22:26:24 +02:00
parent c5382e0bcc
commit fed2e0392c
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
6 changed files with 90 additions and 57 deletions

View file

@ -25,6 +25,8 @@ Note that some of these parameters are explained in details in other sections of
disableSearch = false disableSearch = false
# Disable search in hidden pages, otherwise they will be shown in search box # Disable search in hidden pages, otherwise they will be shown in search box
disableSearchHiddenPages = false disableSearchHiddenPages = false
# Disables hidden pages from showing up in the sitemap and on Google (et all), otherwise they may be indexed by search engines
disableSeoHiddenPages = false
# Javascript and CSS cache are automatically busted when new version of site is generated. # Javascript and CSS cache are automatically busted when new version of site is generated.
# Set this to true to disable this behavior (some proxies don't handle well this optimization) # Set this to true to disable this behavior (some proxies don't handle well this optimization)
disableAssetsBusting = false disableAssetsBusting = false

View file

@ -0,0 +1,24 @@
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
{{- partial "page-meta.go" . }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{- range .Data.Pages }}
{{- if and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) }}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
<xhtml:link
rel="alternate"
hreflang="{{ .Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ .Lang }}"
href="{{ .Permalink }}"
/>{{ end }}
</url>
{{- end -}}
{{- end }}
</urlset>

View file

@ -3,8 +3,8 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
{{ hugo.Generator }} {{ hugo.Generator -}}
{{ partial "meta.html" . }} {{- partial "meta.html" . }}
{{ partial "favicon.html" . }} {{ partial "favicon.html" . }}
<title>{{ .Title }} {{ default "::" .Site.Params.titleSeparator }} {{ .Site.Title }}</title> <title>{{ .Title }} {{ default "::" .Site.Params.titleSeparator }} {{ .Site.Title }}</title>
@ -38,58 +38,6 @@
</style> </style>
{{ partial "custom-header.html" . }} {{ partial "custom-header.html" . }}
</head> </head>
{{- $currentNode := . }}
{{- $currentNode.Scratch.Set "relearnIsSelfFound" nil }}
{{- $currentNode.Scratch.Set "relearnPrevPage" nil }}
{{- $currentNode.Scratch.Set "relearnNextPage" nil }}
{{- template "relearn-structure" dict "node" .Site.Home "currentnode" $currentNode "hidden" false }}
{{- define "relearn-structure" }}
{{- $currentNode := .currentnode }}
{{- $isSelf := eq $currentNode.RelPermalink .node.RelPermalink }}
{{- $isDescendant := and (not $isSelf) (.node.IsDescendant $currentNode) }}
{{- $isAncestor := and (not $isSelf) (.node.IsAncestor $currentNode) }}
{{- $isOther := and (not $isDescendant) (not $isSelf) (not $isAncestor) }}
{{- if $isSelf }}
{{- $currentNode.Scratch.Set "relearnIsSelfFound" true }}
{{- end}}
{{- $isSelfFound := eq ($currentNode.Scratch.Get "relearnIsSelfFound") true }}
{{- $isPreSelf := and (not $isSelfFound) (not $isSelf) }}
{{- $isPostSelf := and ($isSelfFound) (not $isSelf) }}
{{- $hidden_node := or (.node.Params.hidden) (eq .node.Title "") }}
{{- $hidden_stem:= or $hidden_node .hidden }}
{{- $hidden_from_current := or (and $hidden_node (not $isAncestor) (not $isSelf) ) (and .hidden (or $isPreSelf $isPostSelf $isDescendant) ) }}
{{- .node.Scratch.Set "relearnIsHiddenNode" $hidden_node}}
{{- .node.Scratch.Set "relearnIsHiddenStem" $hidden_stem}}
{{- if not $hidden_from_current }}
{{- if $isPreSelf }}
{{- $currentNode.Scratch.Set "relearnPrevPage" .node }}
{{- else if and $isPostSelf (eq ($currentNode.Scratch.Get "relearnNextPage") nil) }}
{{- $currentNode.Scratch.Set "relearnNextPage" .node }}
{{- end}}
{{- end }}
{{- $currentNode.Scratch.Set "relearnSubPages" .node.Pages }}
{{- if .node.IsHome}}
{{- $currentNode.Scratch.Set "relearnSubPages" .node.Sections }}
{{- else if .node.Sections}}
{{- $currentNode.Scratch.Set "relearnSubPages" (.node.Pages | union .node.Sections) }}
{{- end}}
{{- $pages := ($currentNode.Scratch.Get "relearnSubPages") }}
{{- if eq .Site.Params.ordersectionsby "title"}}
{{- range $pages.ByTitle }}
{{- template "relearn-structure" dict "node" . "currentnode" $currentNode "hidden" $hidden_from_current }}
{{- end}}
{{- else}}
{{- range $pages.ByWeight }}
{{- template "relearn-structure" dict "node" . "currentnode" $currentNode "hidden" $hidden_from_current }}
{{- end}}
{{- end }}
{{- end }}
<body class="" data-url="{{ .RelPermalink }}"> <body class="" data-url="{{ .RelPermalink }}">
{{ partial "menu.html" . }} {{ partial "menu.html" . }}
<section id="body"> <section id="body">

View file

@ -105,7 +105,7 @@
{{ $alwaysopen := .alwaysopen }} {{ $alwaysopen := .alwaysopen }}
{{ with $currentNode.File }}{{ $currentFileUniqueID = .UniqueID }}{{ end }} {{ with $currentNode.File }}{{ $currentFileUniqueID = .UniqueID }}{{ end }}
{{with .sect}} {{with .sect}}
{{ $hidden := and (eq (.Scratch.Get "relearnIsHiddenStem") true) (not $.showhidden) (not (.IsAncestor $currentNode))}} {{ $hidden := and (eq (.Scratch.Get "relearnIsHiddenFromCurrent") true) (not $.showhidden) (not (.IsAncestor $currentNode))}}
{{if $hidden }} {{if $hidden }}
{{else if .IsSection }} {{else if .IsSection }}
{{safeHTML .Params.head}} {{safeHTML .Params.head}}

View file

@ -1,2 +1,8 @@
{{- partial "page-meta.go" . }}
{{- if not (and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) ) }}
<meta name="robots" content="noindex, nofollow, noarchive, noimageindex">
{{- end }}
<meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}"> <meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}">
{{ with .Site.Params.author }}<meta name="author" content="{{ . }}">{{ end }} {{- with .Site.Params.author }}
<meta name="author" content="{{ . }}">
{{- end }}

View file

@ -0,0 +1,53 @@
{{- $currentNode := . }}
{{- $currentNode.Scratch.Set "relearnIsSelfFound" nil }}
{{- $currentNode.Scratch.Set "relearnPrevPage" nil }}
{{- $currentNode.Scratch.Set "relearnNextPage" nil }}
{{- template "relearn-structure" dict "node" .Site.Home "currentnode" $currentNode "hiddenstem" false "hiddencurrent" false }}
{{- define "relearn-structure" }}
{{- $currentNode := .currentnode }}
{{- $isSelf := eq $currentNode.RelPermalink .node.RelPermalink }}
{{- $isDescendant := and (not $isSelf) (.node.IsDescendant $currentNode) }}
{{- $isAncestor := and (not $isSelf) (.node.IsAncestor $currentNode) }}
{{- $isOther := and (not $isDescendant) (not $isSelf) (not $isAncestor) }}
{{- if $isSelf }}
{{- $currentNode.Scratch.Set "relearnIsSelfFound" true }}
{{- end}}
{{- $isSelfFound := eq ($currentNode.Scratch.Get "relearnIsSelfFound") true }}
{{- $isPreSelf := and (not $isSelfFound) (not $isSelf) }}
{{- $isPostSelf := and ($isSelfFound) (not $isSelf) }}
{{- $hidden_node := or (.node.Params.hidden) (eq .node.Title "") }}
{{- $hidden_stem:= or $hidden_node .hiddenstem }}
{{- $hidden_current_stem:= or $hidden_node .hiddencurrent }}
{{- $hidden_from_current := or (and $hidden_node (not $isAncestor) (not $isSelf) ) (and .hiddencurrent (or $isPreSelf $isPostSelf $isDescendant) ) }}
{{- .node.Scratch.Set "relearnIsHiddenNode" $hidden_node}}
{{- .node.Scratch.Set "relearnIsHiddenStem" $hidden_stem}}
{{- .node.Scratch.Set "relearnIsHiddenFromCurrent" $hidden_current_stem}}
{{- if not $hidden_from_current }}
{{- if $isPreSelf }}
{{- $currentNode.Scratch.Set "relearnPrevPage" .node }}
{{- else if and $isPostSelf (eq ($currentNode.Scratch.Get "relearnNextPage") nil) }}
{{- $currentNode.Scratch.Set "relearnNextPage" .node }}
{{- end}}
{{- end }}
{{- $currentNode.Scratch.Set "relearnSubPages" .node.Pages }}
{{- if .node.IsHome}}
{{- $currentNode.Scratch.Set "relearnSubPages" .node.Sections }}
{{- else if .node.Sections}}
{{- $currentNode.Scratch.Set "relearnSubPages" (.node.Pages | union .node.Sections) }}
{{- end}}
{{- $pages := ($currentNode.Scratch.Get "relearnSubPages") }}
{{- if eq .Site.Params.ordersectionsby "title"}}
{{- range $pages.ByTitle }}
{{- template "relearn-structure" dict "node" . "currentnode" $currentNode "hiddenstem" $hidden_stem "hiddencurrent" $hidden_from_current }}
{{- end}}
{{- else}}
{{- range $pages.ByWeight }}
{{- template "relearn-structure" dict "node" . "currentnode" $currentNode "hiddenstem" $hidden_stem "hiddencurrent" $hidden_from_current }}
{{- end}}
{{- end }}
{{- end }}