BlackCoffy

hugoでsitemap.xmlを出力する

hugoでsitemap.xmlを出力するときの備忘録です。公式を参考にすれば簡単。

環境

  • hugo v0.75.0

sitemap.xml

locとlastmodを出力するだけの簡単なものです.

2006-01-02T15:04:05-07:00は,golang特有の書き方です.golang Format

{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {{ range .Data.Pages }}
  {{ if .IsPage }}
  <url>
    <loc>{{ .Permalink }}</loc>
    {{ if .Lastmod }}
    <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>
    {{ end }}
  </url>
  {{ end }}
  {{ end }}
</urlset>

config.tomlを編集することで,changefreqやpriorityを設定できます.

hugo sitemap.xml