Skip to content

Commit

Permalink
✨ Feat: optionally exclude content from sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Oct 11, 2024
1 parent f738e4a commit 3edabbe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 47 deletions.
1 change: 1 addition & 0 deletions hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ enableEmoji = true

[sitemap]
changefreq = "weekly"
disable = false
filename = "sitemap.xml"
priority = 0.5

Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/init/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- .Scratch.Set "version" "v0.3.13-95436c11" -}}
{{- .Scratch.Set "version" "v0.3.13-b32f1a99" -}}
{{- .Scratch.Set "this" dict -}}

{{- partial "init/detection-env.html" . -}}
Expand Down
57 changes: 11 additions & 46 deletions layouts/sitemap.xml
Original file line number Diff line number Diff line change
@@ -1,47 +1,12 @@
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
>
{{- range (where .Data.Pages "Section" "!=" "gallery") -}}
<url>
<loc>
{{- .Permalink -}}
</loc>
{{- if not .Lastmod.IsZero -}}
<lastmod>
{{- .Lastmod.Format "2006-01-02T15:04:05-07:00" -}}
</lastmod>
{{- end -}}
{{- with .Sitemap.ChangeFreq -}}
<changefreq>
{{- . -}}
</changefreq>
{{- end -}}

{{- if ge .Sitemap.Priority 0.0 -}}
{{- $weeks := div (sub now.Unix .Lastmod.Unix) 604800 -}}
{{- $priority := sub 1 (div $weeks 10.0 ) -}}
{{- if ge .Sitemap.Priority $priority -}}
<priority>{{ .Sitemap.Priority }}</priority>
{{- else -}}
<priority>{{ $priority }}</priority>
{{- end -}}
{{- 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 -}}
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{- range where .Pages "Sitemap.Disable" "ne" true -}}{{ if .Permalink }}
<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="{{ .Language.LanguageCode }}" href="{{ .Permalink }}"/>{{ end }}
<xhtml:link rel="alternate" hreflang="{{ .Language.LanguageCode }}" href="{{ .Permalink }}"/>{{ end }}
</url>{{ end }}{{ end }}
</urlset>

0 comments on commit 3edabbe

Please sign in to comment.