From fed2e0392cde439cb099a6309f0e07540083fbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Thu, 9 Sep 2021 22:26:24 +0200 Subject: [PATCH] sitemap: hide hidden pages from sitemap and SEO indexing #85 --- .../content/basics/configuration/_index.en.md | 2 + layouts/_default/sitemap.xml | 24 ++++++++ layouts/partials/header.html | 56 +------------------ layouts/partials/menu.html | 2 +- layouts/partials/meta.html | 10 +++- layouts/partials/page-meta.go | 53 ++++++++++++++++++ 6 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 layouts/_default/sitemap.xml create mode 100644 layouts/partials/page-meta.go diff --git a/exampleSite/content/basics/configuration/_index.en.md b/exampleSite/content/basics/configuration/_index.en.md index 4ac96f5c08..501aa1b33e 100644 --- a/exampleSite/content/basics/configuration/_index.en.md +++ b/exampleSite/content/basics/configuration/_index.en.md @@ -25,6 +25,8 @@ Note that some of these parameters are explained in details in other sections of disableSearch = false # Disable search in hidden pages, otherwise they will be shown in search box 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. # Set this to true to disable this behavior (some proxies don't handle well this optimization) disableAssetsBusting = false diff --git a/layouts/_default/sitemap.xml b/layouts/_default/sitemap.xml new file mode 100644 index 0000000000..7ac5f64c4c --- /dev/null +++ b/layouts/_default/sitemap.xml @@ -0,0 +1,24 @@ +{{ printf "" | safeHTML }} +{{- partial "page-meta.go" . }} + +{{- range .Data.Pages }} +{{- if and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) }} + + {{ .Permalink }}{{ if not .Lastmod.IsZero }} + {{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}{{ end }}{{ with .Sitemap.ChangeFreq }} + {{ . }}{{ end }}{{ if ge .Sitemap.Priority 0.0 }} + {{ .Sitemap.Priority }}{{ end }}{{ if .IsTranslated }}{{ range .Translations }} + {{ end }} + {{ end }} + +{{- end -}} +{{- end }} + diff --git a/layouts/partials/header.html b/layouts/partials/header.html index 79f1d1ace0..0917b348c6 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -3,8 +3,8 @@ - {{ hugo.Generator }} - {{ partial "meta.html" . }} + {{ hugo.Generator -}} + {{- partial "meta.html" . }} {{ partial "favicon.html" . }} {{ .Title }} {{ default "::" .Site.Params.titleSeparator }} {{ .Site.Title }} @@ -38,58 +38,6 @@ {{ partial "custom-header.html" . }} - {{- $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 }} {{ partial "menu.html" . }}
diff --git a/layouts/partials/menu.html b/layouts/partials/menu.html index e0fdd153d3..882fe91549 100644 --- a/layouts/partials/menu.html +++ b/layouts/partials/menu.html @@ -105,7 +105,7 @@ {{ $alwaysopen := .alwaysopen }} {{ with $currentNode.File }}{{ $currentFileUniqueID = .UniqueID }}{{ end }} {{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 }} {{else if .IsSection }} {{safeHTML .Params.head}} diff --git a/layouts/partials/meta.html b/layouts/partials/meta.html index 71d9634eff..fa730ce9d4 100644 --- a/layouts/partials/meta.html +++ b/layouts/partials/meta.html @@ -1,2 +1,8 @@ - -{{ with .Site.Params.author }}{{ end }} +{{- partial "page-meta.go" . }} +{{- if not (and .Title (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableSeoHiddenPages true) ) ) }} + +{{- end }} + +{{- with .Site.Params.author }} + +{{- end }} \ No newline at end of file diff --git a/layouts/partials/page-meta.go b/layouts/partials/page-meta.go new file mode 100644 index 0000000000..a5c67fd50a --- /dev/null +++ b/layouts/partials/page-meta.go @@ -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 }} \ No newline at end of file