Skip to content

feat: generate sitemap.xml at build time#35

Merged
engineervix merged 3 commits into
engineervix:mainfrom
juan-lorenzo-bot:feat/sitemap-xml
Jun 26, 2026
Merged

feat: generate sitemap.xml at build time#35
engineervix merged 3 commits into
engineervix:mainfrom
juan-lorenzo-bot:feat/sitemap-xml

Conversation

@juan-lorenzo-bot

Copy link
Copy Markdown
Collaborator

Summary

Generate a standard sitemap.xml in output_dir during kwelea build, helping search engines discover and index all pages.

Changes

  • internal/builder/sitemap.go (new) — Writes a standard XML sitemap using the sitemaps.org schema 0.9. Each <url> entry contains a <loc> with the full URL (base_url + page path). Draft pages are excluded.
  • internal/builder/builder.go — Hook writeSitemap() into Build() as Pass 4, after search-index.json.
  • templates/layout.html — Add <link rel="sitemap" type="application/xml" href="/sitemap.xml"> to <head>.

Behaviour

  • When [site] base_url is set: sitemap.xml is written to output_dir/sitemap.xml with all non-draft pages listed.
  • When base_url is not set: sitemap generation is skipped silently (no error, no warning).
  • Draft pages (already filtered out by WalkDocs) do not appear in the sitemap. A defensive page.Draft check is included as well.

Verification

$ go build ./...        #
$ go test ./...         # ✓ all tests pass
$ ./kwelea build        # ✓ site written, sitemap.xml generated
$ cat site/sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url><loc>https://engineervix.github.io/kwelea/</loc></url>
  <url><loc>https://engineervix.github.io/kwelea/getting-started/</loc></url>
  ...
</urlset>

Confirmed <link rel="sitemap" ...> appears in rendered HTML:

<link rel="sitemap" type="application/xml" href="/kwelea/sitemap.xml">

Closes #4

- Add internal/builder/sitemap.go: writes a standard sitemap.xml
  (sitemaps.org schema 0.9) containing all non-draft pages
- Skip silently when [site] base_url is not configured
- Hook writeSitemap into Build() as Pass 4, after search-index.json
- Add <link rel="sitemap" href="/sitemap.xml"> to layout.html <head>

Closes engineervix#4

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds build-time generation of a standards-based sitemap.xml to the site output, and exposes it in rendered pages so search engines can discover and index all non-draft documentation pages.

Changes:

  • Add internal/builder/sitemap.go to generate and write output_dir/sitemap.xml using the sitemaps.org schema.
  • Hook sitemap generation into kwelea build after the search index is written.
  • Add a <link rel="sitemap" ...> tag to the HTML layout <head>.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
templates/layout.html Adds a sitemap <link> tag pointing at {{.Site.BasePath}}/sitemap.xml.
internal/builder/sitemap.go Implements sitemap XML generation from site.Pages and writes sitemap.xml to the output directory.
internal/builder/builder.go Invokes writeSitemap() as an additional build pass after search-index.json.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/builder/sitemap.go
- Replace empty-string check with hasScheme() — skip sitemap when
  base_url is missing or relative (e.g. '/kwelea'), per the sitemaps.org
  requirement that <loc> entries must be absolute URLs
- Add sitemap_test.go: TestHasScheme, TestWriteSitemap (4 sub-tests
  covering https, trailing slash, empty, relative), TestWriteSitemapExcludesDrafts

Addresses Copilot review feedback on PR engineervix#35
@juan-lorenzo-bot

Copy link
Copy Markdown
Collaborator Author

Good catch 👍 — fixed in 5defcdb.

writeSitemap now uses a hasScheme() helper that checks for http:// or https:// prefix. If base_url is empty or a relative path (e.g. /kwelea), the sitemap is skipped silently. This ensures all <loc> entries are absolute URLs per the sitemaps.org protocol.

Also added tests covering this case (TestWriteSitemap/relative_base_url_—_skip_silently) plus draft exclusion and trailing-slash handling.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread internal/builder/sitemap.go Outdated
Comment thread internal/builder/builder.go Outdated
- hasScheme now rejects scheme-only strings like 'https://' or 'https:///'
  (no host component) so writeSitemap won't produce invalid <loc> values
- Update Pass 4 comment in builder.go to reflect both skip conditions
- Add test cases for https://, http://, and https:/// edge cases

Addresses Copilot review feedback round 2 on PR engineervix#35
@juan-lorenzo-bot

Copy link
Copy Markdown
Collaborator Author

Both points addressed in cd9f347:

1. hasScheme now validates host component — rejects scheme-only strings like https:// and https:/// so writeSitemap won't produce invalid <loc> values like https:///path. Added test cases for all three edge cases.

2. Updated Pass 4 comment in builder.go — now reads skipped silently when base_url is not set or is not an absolute URL with a scheme and host.

@engineervix engineervix merged commit 3d31358 into engineervix:main Jun 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate sitemap.xml at build time

3 participants