Adding "external" content to sitemap.xml

Hi everyone.

Just starting with Hugo, so probably a basic question:

I have some content generated outside Hugo (basically some Jupyter notebooks transformed into HTML) which, on Hugo, I place under /static/....

This works fine, but I wanted to add those external HTML files to the sitemap.xml.
Is there a way to do it automatically from Hugo?

Thanks.

You can do something like this in your layouts/sitemap.xml:

<!-- rest of the sitemap -->

{{ range readDir "static" }}

  {{ if (findRE ".html$" .Name) }}

    {{ printf "<url>" | safeHTML }}

      {{ printf "<loc>%s%s</loc>" $.Site.BaseURL .Name | safeHTML }}
      {{ printf "<changefreq>monthly</changefreq>" | safeHTML }}
      {{ printf "<priority>0.3</priority>" | safeHTML }}

    {{ printf "</url>" | safeHTML }}

  {{ end }}

{{ end }}

This assumes that you’ve HTML files directly inside the static folder without any other directory. If you’ve created another directory, you’d have to modify the code accordingly. Also, I’m not sure if the code works recursively for all directories or no. If it doesn’t you’d have to place all your HTML files in one directory itself.

This also assumes that you’ve configured the website’s Base URL in config.toml with a forward slash / at the ending. If not, you’d have to change the <loc> to %s/%s.

2 Likes

Awesome, thank you!

That’s exactly the kind of thing I needed.
All the HTML files are on the same directory, but under /static/notebooks, so that’s easy change to your code.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.