From 10ac37ba29fa55d2b3cc82933c8cfb692c753cf0 Mon Sep 17 00:00:00 2001 From: Gottfried Mayer Date: Tue, 24 Oct 2023 12:39:34 +0200 Subject: [PATCH] feat: exclude external pages from Sitemap, include pages with relative externalUrl with correct URL in Sitemap - Exclude pages with absolute externalUrl from Sitemap - Include pages with relative externalUrl with correct URL in Sitemap - Improve output XML (no more linebreaks and spaces) --- layouts/_default/sitemap.xml | 43 ++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/layouts/_default/sitemap.xml b/layouts/_default/sitemap.xml index 45789c648..94c04d555 100644 --- a/layouts/_default/sitemap.xml +++ b/layouts/_default/sitemap.xml @@ -1,28 +1,23 @@ {{ printf "" | safeHTML }} - {{ range .Data.Pages }} - {{ if not (in .Site.Params.sitemap.excludedKinds .Kind) }} - {{ if and (.Param "xml" | default true) (not (isset .Params "externalUrl")) }} - {{- if .Permalink -}} - - {{ .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 }} - {{ end }} - {{ end }} +{{ range .Data.Pages }}{{ if not (in .Site.Params.sitemap.excludedKinds .Kind) }}{{ if and (.Param "xml" | default true) (or (not .Params.externalUrl) (and (.Params.externalUrl) (and (not (hasPrefix .Params.externalUrl "http://")) (not (hasPrefix .Params.externalUrl "https://"))))) }} +{{- if .Permalink -}}{{ $link := .Permalink }}{{ if .Params.externalUrl }}{{ $link = printf "%s%s" .Site.BaseURL (substr .Params.externalUrl 1) }}{{ end }} + + {{ $link }}{{ 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 }}{{ end }}{{ end }}