Skip to content

Commit

Permalink
sitemap: hide hidden pages from sitemap and SEO indexing matcornic#85
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby committed Sep 9, 2021
1 parent c5382e0 commit fed2e03
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 57 deletions.
2 changes: 2 additions & 0 deletions exampleSite/content/basics/configuration/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions layouts/_default/sitemap.xml
Original file line number Diff line number Diff line change
@@ -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>
56 changes: 2 additions & 54 deletions layouts/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{{ hugo.Generator }}
{{ partial "meta.html" . }}
{{ hugo.Generator -}}
{{- partial "meta.html" . }}
{{ partial "favicon.html" . }}
<title>{{ .Title }} {{ default "::" .Site.Params.titleSeparator }} {{ .Site.Title }}</title>

Expand Down Expand Up @@ -38,58 +38,6 @@
</style>
{{ partial "custom-header.html" . }}
</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 }}">
{{ partial "menu.html" . }}
<section id="body">
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h3>{{ if not $disableShortcutsTitle}}{{ T "Shortcuts-Title"}}{{ end }}</h3>
{{ $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}}
Expand Down
10 changes: 8 additions & 2 deletions layouts/partials/meta.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}">
{{ with .Site.Params.author }}<meta name="author" content="{{ . }}">{{ end }}
{{- 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 }}">
{{- with .Site.Params.author }}
<meta name="author" content="{{ . }}">
{{- end }}
53 changes: 53 additions & 0 deletions layouts/partials/page-meta.go
Original file line number Diff line number Diff line change
@@ -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 }}

0 comments on commit fed2e03

Please sign in to comment.