sitemap-writer

Crates.iositemap-writer
lib.rssitemap-writer
version1.0.1
created_at2022-06-09 13:22:22.824243+00
updated_at2026-01-06 15:25:48.245665+00
descriptionA simple and lightweight Rust library for generating XML sitemaps
homepagehttps://github.com/uiuifree/rust-sitemap-writer
repositoryhttps://github.com/uiuifree/rust-sitemap-writer
max_upload_size
id602607
size29,594
Misaki (uiuifree)

documentation

https://docs.rs/sitemap-writer

README

sitemap-writer

Crates.io Documentation License: MIT

A simple and lightweight Rust library for generating XML sitemaps.

Features

  • Simple API for creating sitemaps
  • Automatic XML escaping for special characters
  • Support for all sitemap properties (loc, lastmod, changefreq, priority)
  • Support for Sitemap Index (for large sites with 50,000+ URLs)
  • Write directly to file or build as String
  • No heavy dependencies

Installation

Add this to your Cargo.toml:

[dependencies]
sitemap-writer = "1.0"

Quick Start

use sitemap_writer::{SitemapWriter, SitemapUrl, SitemapChangeFreq};

fn main() {
    let result = SitemapWriter::make("sitemap.xml", vec![
        SitemapUrl {
            loc: "https://example.com/".to_string(),
            lastmod: Some("2024-01-01".to_string()),
            changefreq: Some(SitemapChangeFreq::DAILY),
            priority: Some(1.0),
        },
        SitemapUrl::new("https://example.com/about/"),
    ]);

    assert!(result.is_ok());
}

API Reference

SitemapWriter

SitemapWriter::make(path, urls) - Write to File

use sitemap_writer::{SitemapWriter, SitemapUrl, SitemapChangeFreq};

let result = SitemapWriter::make("sitemap.xml", vec![
    SitemapUrl {
        loc: "https://example.com/".to_string(),
        lastmod: Some("2024-01-01".to_string()),
        changefreq: Some(SitemapChangeFreq::DAILY),
        priority: Some(1.0),
    },
    SitemapUrl::new("https://example.com/contact/"),
]);

SitemapWriter::build(urls) - Build as String

use sitemap_writer::{SitemapWriter, SitemapUrl};

let xml = SitemapWriter::build(vec![
    SitemapUrl::new("https://example.com/"),
    SitemapUrl::new("https://example.com/about/"),
]);

SitemapUrl

Field Type Description
loc String Required. The URL of the page.
lastmod Option<String> The date of last modification (YYYY-MM-DD).
changefreq Option<SitemapChangeFreq> How frequently the page changes.
priority Option<f32> Priority relative to other URLs (0.0 to 1.0).

SitemapChangeFreq

Value Description
ALWAYS Changes every access
HOURLY Changes hourly
DAILY Changes daily
WEEKLY Changes weekly
MONTHLY Changes monthly
YEARLY Changes yearly
NEVER Archived content

Output Example

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://example.com/</loc>
    <lastmod>2024-01-01</lastmod>
    <changefreq>daily</changefreq>
    <priority>1</priority>
  </url>
  <url>
    <loc>https://example.com/about/</loc>
  </url>
</urlset>

Sitemap Index

For large sites with more than 50,000 URLs, use Sitemap Index:

SitemapIndexWriter

SitemapIndexWriter::make(path, sitemaps) - Write to File

use sitemap_writer::{SitemapIndexWriter, SitemapIndex};

let result = SitemapIndexWriter::make("sitemap_index.xml", vec![
    SitemapIndex {
        loc: "https://example.com/sitemap1.xml".to_string(),
        lastmod: Some("2024-01-01".to_string()),
    },
    SitemapIndex::new("https://example.com/sitemap2.xml"),
]);

SitemapIndexWriter::build(sitemaps) - Build as String

use sitemap_writer::{SitemapIndexWriter, SitemapIndex};

let xml = SitemapIndexWriter::build(vec![
    SitemapIndex::new("https://example.com/sitemap1.xml"),
    SitemapIndex::new("https://example.com/sitemap2.xml"),
]);

SitemapIndex

Field Type Description
loc String Required. The URL of the sitemap file.
lastmod Option<String> The date of last modification (YYYY-MM-DD).

Output Example

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>https://example.com/sitemap1.xml</loc>
    <lastmod>2024-01-01</lastmod>
  </sitemap>
  <sitemap>
    <loc>https://example.com/sitemap2.xml</loc>
  </sitemap>
</sitemapindex>

License

MIT License

Author

uiuifree

Commit count: 9

cargo fmt