Skip to content

feat(build): generate sitemap.xml with cook build web --sitemap - #351

Merged
dubadub merged 8 commits into
mainfrom
feat/sitemap-static-site
Jun 6, 2026
Merged

dubadub merged 8 commits into
mainfrom
feat/sitemap-static-site

Conversation

@dubadub

@dubadub dubadub commented Jun 6, 2026

Copy link
Copy Markdown
Member

Summary

Closes #346. Adds optional sitemap generation to the cook build web static-site generator.

  • New opt-in --sitemap <URL> flag. When given the deployed site's full base URL (e.g. https://recipes.example.com or https://example.com/recipes), it writes a sitemaps.org-compliant sitemap.xml at the output root. Omitting it preserves existing behavior (no sitemap).
  • Lists the homepage (<base>/), every directory listing page, and every recipe/menu page as absolute URLs, with path segments percent-encoded and a best-effort <lastmod> from each source file's modification time.
  • The flag is independent of --base-url (which only controls internal link prefixes); --sitemap carries scheme + host + optional subpath.

Robustness

  • URL validation: must be an absolute http(s) URL with a host and no query/fragment, else a clear error and non-zero exit.
  • Only pages actually written to disk are listed (the render pass skips recipes whose templates fail), so the sitemap never points at missing pages.
  • Entries are sorted, so repeated builds produce a stable, diff-friendly file.

Implementation

  • New module src/build/sitemap.rs: pure, unit-tested rendering helpers (xml_escape, encode_path, build_loc, format_lastmod, render_sitemap_xml) plus tree-walk glue mirroring the existing index.rs pattern.
  • Wiring in src/build/mod.rs: the --sitemap flag on WebBuildArgs, URL validation, and the write call in run_web (reusing the already-pruned recipe tree).

Design/plan docs: docs/superpowers/specs/2026-06-06-sitemap-static-site-design.md, docs/superpowers/plans/2026-06-06-sitemap-static-site.md.

Test Plan

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test all pass (7 new sitemap unit tests)
  • cook build web /tmp/site --base-path ./seed --sitemap https://recipes.example.com writes sitemap.xml; summary line notes it
  • Output is well-formed (xmllint --noout) and every <loc> maps to a real generated file
  • Omitting --sitemap produces no sitemap
  • Invalid / relative / query-bearing --sitemap URLs are rejected with exit code 1
  • Two consecutive builds produce byte-identical sitemap.xml (deterministic)

dubadub added 7 commits June 6, 2026 20:45
Adds a `--sitemap <URL>` flag to `cook build web`. When provided, walks
the pruned RecipeTree to collect homepage, directory, and recipe/menu
page entries (with best-effort mtime lastmod), renders a
sitemaps.org-compliant sitemap.xml, and writes it to the output root.
The URL is validated with the `url` crate before use.
…agment

Address final review: guard recipe/menu entries by on-disk existence so
recipes the render pass skipped don't become dead links; sort entries for
deterministic output; reject --sitemap URLs carrying a query or fragment.
@claude

claude Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

Code Review

Good, focused feature. The module structure mirrors index.rs cleanly, the pure helpers are well-tested, and the opt-in design preserves existing behavior. A few issues worth addressing before merging.


Medium: URL validated too late — src/build/mod.rs, run_web

The --sitemap URL is only validated after the full build completes (search index written, all pages rendered). A typo in the URL means the user waits through the entire build before getting an error. Move validation to the start of run_web, before any rendering, then reuse the result at the call site.


Minor: directory pages skip the on-disk existence check — src/build/sitemap.rs, collect

Recipe/menu leaves are gated by output.join(page_file(&relpath)).exists(), protecting against render failures. The same guard is absent for directory entries. If a directory listing fails to render, its URL ends up in the sitemap pointing at a 404. Fix: check existence for directory entries too, but still recurse into children (a failed directory page does not mean its recipes are missing).


Minor: child.path may be a directory, not the recipe file — src/build/sitemap.rs, collect (leaf branch)

file_lastmod(&child.path) is used for the recipe lastmod. Based on how prune_output_subtree uses child.path (checking if a node is inside the output directory), it looks like child.path is a directory-level path, not the .cook file. If so, the directory mtime is used rather than the file mtime, which changes whenever anything in that directory changes. Since child.recipe.as_ref() is already available, check whether RecipeEntry exposes the source file path and use that instead.


Low: docs/superpowers/ files should not be committed

docs/superpowers/plans/2026-06-06-sitemap-static-site.md and docs/superpowers/specs/2026-06-06-sitemap-static-site-design.md are AI agent workflow artifacts (step-by-step implementation instructions, TDD scaffolding). They are internal scaffolding, not user-facing documentation, and will become stale immediately. The relevant design decisions are captured in the commit messages and PR description. Please remove them or add docs/superpowers/ to .gitignore.


Nit: timezone-sensitive lastmod — src/build/sitemap.rs, file_lastmod

chrono::Local means the date depends on the machine timezone. A recipe modified at 23:30 UTC produces different dates on a UTC server vs. a UTC-8 developer machine. chrono::Utc would make builds reproducible across environments.


Nit: tree-walk logic untested

The 7 unit tests cover all pure rendering helpers well. The collect / build_sitemap_entries path (recipe stem extraction, menu vs. recipe branching, on-disk existence filtering) has no coverage. A single test with a mock RecipeTree would catch regressions in URL-path construction.


Already good

  • Validation is thorough (scheme, host, query, fragment) — stricter than the design doc original cannot_be_a_base().
  • Deterministic output via sorting is a nice touch.
  • Best-effort lastmod (never fatal) is the right call.
  • The page_file helper cleanly encapsulates the empty-string to index.html homepage mapping.

- Validate --sitemap URL up front in run_web so typos fail before the
  full build runs, instead of after.
- Guard directory page entries with the same on-disk existence check as
  recipe/menu pages (still recurse so child recipes are unaffected).
- Use UTC for <lastmod> so dates are reproducible across machines.
- Add a tree-walk test covering stem extraction, existence filtering,
  and deterministic sorting.
@dubadub

dubadub commented Jun 6, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review! Addressed in 9dbcd93:

  • Medium — URL validated too late: moved validation to the top of run_web, before any rendering, so a typo now fails immediately instead of after the full build. Verified: an invalid --sitemap now errors with no preceding render output.
  • Minor — directory pages skip existence check: directory entries now get the same on-disk existence guard as recipe/menu pages, while still recursing into children (a failed directory page doesn't imply its recipes are missing). Note this is defensive — walk_directories propagates errors via ?, so a directory render failure already aborts the whole build before the sitemap step.
  • Nit — timezone-sensitive lastmod: switched chrono::Local → chrono::Utc so <lastmod> is reproducible across machines.
  • Nit — tree-walk untested: added build_entries_filters_missing_pages_and_sorts, which builds a real RecipeTree and asserts stem extraction, the on-disk existence filter (a recipe with no rendered page is excluded), and deterministic sorting.

One point I looked into but did not change:

  • Minor — child.path may be a directory: verified it isn't. In cooklang-find (src/tree/mod.rs:130), recipe leaves are built with new_with_recipe(name, path, recipe) where path is the file path; only directory nodes (line 118) get a directory path. Confirmed empirically too — three recipes in the same Breakfast/ directory produce three different <lastmod> dates, which wouldn't happen with a shared directory mtime. So file_lastmod(&child.path) already uses the per-file mtime.

On the docs/superpowers/ files: keeping them to stay consistent with the existing convention (there are already 4 spec files under docs/superpowers/specs/ on main); they're also not part of this PR's diff. Happy to revisit repo-wide in a separate change if the team prefers.

@claude

claude Bot commented Jun 6, 2026

Copy link
Copy Markdown

---\n## Code Review — PR #351: cook build web --sitemap\n\nThis PR adds opt-in sitemap.xml generation to the static-site builder.\n\nThe implementation is solid. The main requests before merging: drop the docs/superpowers/ planning files and confirm urlencoding, url, and tempfile are declared in Cargo.toml.

@claude

claude Bot commented Jun 6, 2026

Copy link
Copy Markdown

Code Review — PR #351: cook build web --sitemap

Overview

This PR adds opt-in sitemap.xml generation to the static-site builder. The design is clean: a new src/build/sitemap.rs module of pure, unit-tested helpers wired into run_web via a --sitemap <URL> flag. The implementation mirrors existing patterns (index.rs tree walk, writer::write_bytes for output) and is generally well-structured.


Correctness

Solid throughout. A few things worth calling out:

  • trim_end_matches for extension stripping is used correctly in Rust: str::trim_end_matches(".cook") strips the exact suffix string (not individual chars). No issue here.
  • encode_path("") → "" produces the expected homepage URL "{base}/" — verified by the test.
  • Sort stability: the homepage (relpath = "") is pushed before collect, and "" < "any/path" lexicographically, so it naturally stays first after sorting. Correct.
  • On-disk existence check in collect (lines ~874, 890) is a good safeguard against the sitemap pointing at pages the renderer failed to produce.

Potential Issues

1. child.path on leaf nodes used for lastmod

In the leaf branch of collect:

lastmod: file_lastmod(&child.path),

If RecipeTree.path on a leaf node is a directory path (not the source .cook file path), fs::metadata will read directory mtime rather than the recipe file mtime. Worth verifying that child.path on a leaf is the recipe file path, not the parent directory.

2. trim_end_matches strips repeated suffixes

"foo.cook.cook".trim_end_matches(".cook") yields "foo". Extremely unlikely in practice but technically silently wrong. Using Path::file_stem() (strips one extension) would be more idiomatic and precise.

3. Missing Cargo.toml changes

The diff uses urlencoding::encode and url::Url but does not touch Cargo.toml. If urlencoding and url are not already declared as dependencies, this will not compile. The integration test also uses tempfile::TempDir — same question for dev-dependencies. These should be explicitly declared even if they happen to be available transitively.


Design Feedback

4. docs/superpowers/ files should not be in the repo

docs/superpowers/plans/2026-06-06-sitemap-static-site.md and docs/superpowers/specs/2026-06-06-sitemap-static-site-design.md are AI-workflow implementation artifacts — the plan doc opens with > For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development. They account for ~650 of the 969 added lines and add no value for contributors or end users. These should be dropped before merging.

5. URL validation is more thorough than the PR description suggests

The actual implementation checks scheme, host, query, and fragment — better than the plan doc's original cannot_be_a_base() approach. The PR description is worth updating to reflect this.


Code Style

  • The sitemap_written: bool variable in mod.rs is readable but the flag can be simplified since write_sitemap already returns an error on failure:
    let sitemap_note = if args.sitemap.is_some() { ", sitemap.xml" } else { "" };
  • The comment // The URL was already validated near the top of run_web. is informational overhead — the code flow already makes this apparent.

Test Coverage

7 unit tests cover all the pure helpers plus a real-filesystem integration test. The URL validation branch in mod.rs (invalid scheme, bare path, query/fragment) has no test, but that code is thin and delegates to the url crate, so it is a minor gap.


Summary

Category Status
Core logic Correct
Edge cases (missing pages, bad mtime) Handled
URL encoding + XML escaping Correct
Test coverage Good
Cargo.toml deps declared Unverified
child.path for leaf lastmod Verify
Docs artifacts in repo Should be removed

The implementation is solid. The main requests before merging: drop the docs/superpowers/ planning files and confirm urlencoding, url, and tempfile are declared in Cargo.toml.

@dubadub
dubadub merged commit 6db127a into main Jun 6, 2026
2 checks passed
@pinage404

Copy link
Copy Markdown
Contributor

thanks ! I will test on the next release =D

social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
cookcli 0.31.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## [0.31.0](cooklang/cookcli@v0.30.0...v0.31.0) (2026-06-09)


### Features

* **build:** generate sitemap.xml with `cook build web --sitemap` ([#351](cooklang/cookcli#351)) ([6db127a](cooklang/cookcli@6db127a))


### Bug Fixes

* **server:** avoid renameat2 SIGSYS on aarch64 Android (Termux) ([#353](cooklang/cookcli#353)) ([46950ab](cooklang/cookcli@46950ab)), closes [#349](https://github.com/cooklang/cookcli/issues/349)</pre>
  <p>View the full release notes at <a href="https://github.com/cooklang/cookcli/releases/tag/v0.31.0">https://github.com/cooklang/cookcli/releases/tag/v0.31.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!11090
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 with static site generator

2 participants