# `PhoenixKit.Modules.Sitemap.Generator`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.87/lib/modules/sitemap/generator.ex#L1)

Main sitemap generator for PhoenixKit.

Generates a `<sitemapindex>` at `/sitemap.xml` referencing per-module
sitemap files at `/sitemaps/sitemap-{source}.xml`.

## Architecture

- `/sitemap.xml` - Always a `<sitemapindex>` referencing per-module files
- `/sitemaps/sitemap-static.xml` - Static pages
- `/sitemaps/sitemap-routes.xml` - Router discovery
- `/sitemaps/sitemap-publishing.xml` - Publishing posts (or per-blog files)
- `/sitemaps/sitemap-shop.xml` - Shop products (auto-split at 50k)
- `/sitemaps/sitemap-entities.xml` - Entities (or per-type files)

HTML sitemaps are rendered by `PhoenixKit.Modules.Sitemap.HtmlGenerator`.

## Usage

    # Generate all sitemaps (index + per-module files)
    {:ok, result} = Generator.generate_all(base_url: "https://example.com")

    # Generate HTML sitemap (collects from all sources)
    {:ok, html} = Generator.generate_html(base_url: "https://example.com")

    # Backward compatible: generate_xml returns the sitemapindex
    {:ok, xml} = Generator.generate_xml(base_url: "https://example.com")

# `collect_all_entries`

```elixir
@spec collect_all_entries(
  keyword(),
  [module()]
) :: [PhoenixKit.Modules.Sitemap.UrlEntry.t()]
```

Collects URL entries from all enabled sources.

When the Languages module is enabled, automatically collects entries for all
enabled languages and adds hreflang alternate links.

# `generate_all`

```elixir
@spec generate_all(keyword()) :: {:ok, map()} | {:error, any()}
```

Generates all sitemaps: per-module files and the sitemapindex.

## Options

- `:base_url` - Base URL for building full URLs (required)
- `:xsl_style` - XSL stylesheet style: "table" or "minimal" (default: "table")
- `:xsl_enabled` - Enable XSL stylesheet reference (default: true)

## Returns

    {:ok, %{
      index_xml: "<?xml ...sitemapindex...",
      modules: [
        %{filename: "sitemap-static", url_count: 3, lastmod: ~U[...]}
      ],
      total_urls: 150
    }}

# `generate_html`

```elixir
@spec generate_html(keyword()) :: {:ok, String.t()} | {:error, any()}
```

Generates HTML sitemap from all enabled sources.

Delegates to `PhoenixKit.Modules.Sitemap.HtmlGenerator`.

## Options

- `:base_url` - Base URL for building full URLs (required)
- `:style` - Display style: "hierarchical", "grouped", or "flat" (default: "hierarchical")
- `:cache` - Enable/disable caching (default: true)
- `:title` - Page title (default: "Sitemap")

# `generate_index`

```elixir
@spec generate_index(
  [PhoenixKit.Modules.Sitemap.SitemapFile.t()],
  String.t(),
  String.t(),
  boolean()
) ::
  String.t()
```

Builds `<sitemapindex>` XML from a list of `%SitemapFile{}` structs.

# `generate_module`

```elixir
@spec generate_module(
  module(),
  keyword()
) :: [PhoenixKit.Modules.Sitemap.SitemapFile.t()]
```

Generates sitemap file(s) for a single source module.

Returns a list of `%SitemapFile{}` structs (one per file generated).
Empty sources produce no files and return [].

# `generate_xml`

```elixir
@spec generate_xml(keyword()) ::
  {:ok, String.t()} | {:ok, String.t(), [map()]} | {:error, any()}
```

Generates XML sitemap. Returns the sitemapindex XML.

Delegates to `generate_all/1` and returns the index XML for backward compatibility.

# `get_sitemap_part`

```elixir
@spec get_sitemap_part(integer()) :: {:ok, String.t()} | {:error, :not_found}
```

Gets a specific sitemap part by index (1-based).

Legacy function for backward compatibility with old numbered sitemap parts.

# `invalidate_and_regenerate`

```elixir
@spec invalidate_and_regenerate() :: {:ok, Oban.Job.t()} | {:error, term()}
```

Invalidates cache AND triggers async regeneration.

# `invalidate_cache`

```elixir
@spec invalidate_cache() :: :ok
```

Invalidates all cached sitemaps.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
