typstify-generator 0.1.3

Static site generation engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! Sitemap generation.
//!
//! Generates XML sitemaps for search engine optimization.

use std::io::Write;

use chrono::{DateTime, Utc};
use thiserror::Error;
use tracing::debug;
use typstify_core::{Config, Page};

/// Sitemap generation errors.
#[derive(Debug, Error)]
pub enum SitemapError {
    /// IO error.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// XML encoding error.
    #[error("XML encoding error: {0}")]
    Xml(String),
}

/// Result type for sitemap operations.
pub type Result<T> = std::result::Result<T, SitemapError>;

/// Change frequency for sitemap entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeFreq {
    Always,
    Hourly,
    Daily,
    Weekly,
    Monthly,
    Yearly,
    Never,
}

impl ChangeFreq {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Always => "always",
            Self::Hourly => "hourly",
            Self::Daily => "daily",
            Self::Weekly => "weekly",
            Self::Monthly => "monthly",
            Self::Yearly => "yearly",
            Self::Never => "never",
        }
    }
}

/// A sitemap URL entry.
#[derive(Debug, Clone)]
pub struct SitemapUrl {
    /// URL location.
    pub loc: String,

    /// Last modification date.
    pub lastmod: Option<DateTime<Utc>>,

    /// Change frequency.
    pub changefreq: Option<ChangeFreq>,

    /// Priority (0.0 to 1.0).
    pub priority: Option<f32>,

    /// Alternate language versions.
    pub alternates: Vec<AlternateLink>,
}

/// Alternate language link for a URL.
#[derive(Debug, Clone)]
pub struct AlternateLink {
    /// Language code (e.g., "en", "zh").
    pub hreflang: String,

    /// URL for this language version.
    pub href: String,
}

/// Sitemap generator.
#[derive(Debug)]
pub struct SitemapGenerator {
    config: Config,
}

impl SitemapGenerator {
    /// Create a new sitemap generator.
    #[must_use]
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Generate sitemap XML from pages.
    pub fn generate(&self, pages: &[&Page]) -> Result<String> {
        debug!(count = pages.len(), "generating sitemap");

        let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>"#);
        xml.push('\n');
        // Add XSLT stylesheet reference for browser rendering
        xml.push_str(r#"<?xml-stylesheet type="text/xsl" href="/sitemap-style.xsl"?>"#);
        xml.push('\n');
        xml.push_str(r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9""#);

        // Add xhtml namespace if we have multiple languages
        let all_languages = self.config.all_languages();
        if all_languages.len() > 1 {
            xml.push_str(r#" xmlns:xhtml="http://www.w3.org/1999/xhtml""#);
        }
        xml.push_str(">\n");

        for page in pages {
            let url = self.page_to_url(page);
            xml.push_str(&self.url_to_xml(&url));
        }

        xml.push_str("</urlset>\n");

        Ok(xml)
    }

    /// Convert a page to a sitemap URL entry.
    fn page_to_url(&self, page: &Page) -> SitemapUrl {
        let loc = format!("{}{}", self.config.site.base_url, page.url);

        // Determine lastmod from page date or updated date
        let lastmod = page.updated.or(page.date);

        // Determine change frequency and priority based on content type
        let (changefreq, priority) = if page.url == "/" || page.url.is_empty() {
            // Home page
            (Some(ChangeFreq::Daily), Some(1.0))
        } else if page.date.is_some() {
            // Blog posts
            (Some(ChangeFreq::Monthly), Some(0.8))
        } else {
            // Static pages
            (Some(ChangeFreq::Yearly), Some(0.5))
        };

        // Build alternate links for multi-language sites
        let slug = page.url.trim_start_matches('/');
        let all_languages = self.config.all_languages();
        let alternates = if all_languages.len() > 1 {
            all_languages
                .iter()
                .map(|lang| {
                    let href = if *lang == self.config.site.default_language {
                        format!("{}/{}", self.config.site.base_url, slug)
                    } else {
                        format!("{}/{}/{}", self.config.site.base_url, lang, slug)
                    };
                    AlternateLink {
                        hreflang: lang.to_string(),
                        href,
                    }
                })
                .collect()
        } else {
            Vec::new()
        };

        SitemapUrl {
            loc,
            lastmod,
            changefreq,
            priority,
            alternates,
        }
    }

    /// Convert a URL entry to XML.
    fn url_to_xml(&self, url: &SitemapUrl) -> String {
        let mut xml = String::from("  <url>\n");

        xml.push_str(&format!("    <loc>{}</loc>\n", escape_xml(&url.loc)));

        if let Some(lastmod) = &url.lastmod {
            xml.push_str(&format!(
                "    <lastmod>{}</lastmod>\n",
                lastmod.format("%Y-%m-%d")
            ));
        }

        if let Some(changefreq) = &url.changefreq {
            xml.push_str(&format!(
                "    <changefreq>{}</changefreq>\n",
                changefreq.as_str()
            ));
        }

        if let Some(priority) = &url.priority {
            xml.push_str(&format!("    <priority>{priority:.1}</priority>\n"));
        }

        // Add alternate language links
        for alt in &url.alternates {
            xml.push_str(&format!(
                r#"    <xhtml:link rel="alternate" hreflang="{}" href="{}" />"#,
                alt.hreflang,
                escape_xml(&alt.href)
            ));
            xml.push('\n');
        }

        xml.push_str("  </url>\n");
        xml
    }

    /// Write sitemap to a writer.
    pub fn write_to<W: Write>(&self, pages: &[&Page], writer: &mut W) -> Result<()> {
        let xml = self.generate(pages)?;
        writer.write_all(xml.as_bytes())?;
        Ok(())
    }

    /// Generate sitemap index for multiple sitemaps.
    pub fn generate_index(&self, sitemaps: &[&str]) -> String {
        let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8"?>"#);
        xml.push('\n');
        xml.push_str(r#"<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#);
        xml.push('\n');

        let now = Utc::now().format("%Y-%m-%d").to_string();

        for sitemap in sitemaps {
            xml.push_str("  <sitemap>\n");
            xml.push_str(&format!(
                "    <loc>{}/{}</loc>\n",
                self.config.site.base_url, sitemap
            ));
            xml.push_str(&format!("    <lastmod>{now}</lastmod>\n"));
            xml.push_str("  </sitemap>\n");
        }

        xml.push_str("</sitemapindex>\n");
        xml
    }
}

/// Escape special XML characters.
fn escape_xml(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

/// Generate XSLT stylesheet for sitemap rendering in browsers.
///
/// This creates a modern, clean stylesheet with light/dark mode support
/// that renders the sitemap as an HTML table.
#[must_use]
pub fn generate_sitemap_xsl() -> String {
    r#"<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Sitemap</title>
    <style>
        :root {
            --bg-primary: #ffffff;
            --bg-secondary: #f8fafc;
            --bg-tertiary: #f1f5f9;
            --text-primary: #0f172a;
            --text-secondary: #475569;
            --text-muted: #94a3b8;
            --border-color: #e2e8f0;
            --accent-color: #3b82f6;
            --accent-hover: #2563eb;
            --priority-high: #22c55e;
            --priority-medium: #eab308;
            --priority-low: #94a3b8;
        }

        @media (prefers-color-scheme: dark) {
            :root {
                --bg-primary: #0f172a;
                --bg-secondary: #1e293b;
                --bg-tertiary: #334155;
                --text-primary: #f1f5f9;
                --text-secondary: #cbd5e1;
                --text-muted: #64748b;
                --border-color: #334155;
                --accent-color: #60a5fa;
                --accent-hover: #93c5fd;
            }
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: var(--bg-primary);
            color: var(--text-primary);
            line-height: 1.6;
            padding: 2rem;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        header {
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 1px solid var(--border-color);
        }

        h1 {
            font-size: 1.875rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }

        .subtitle {
            color: var(--text-secondary);
            font-size: 0.875rem;
        }

        .stats {
            display: flex;
            gap: 2rem;
            margin-top: 1rem;
            flex-wrap: wrap;
        }

        .stat {
            background: var(--bg-secondary);
            padding: 0.75rem 1.25rem;
            border-radius: 0.5rem;
            border: 1px solid var(--border-color);
        }

        .stat-label {
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            color: var(--text-muted);
        }

        .stat-value {
            font-size: 1.25rem;
            font-weight: 600;
            color: var(--accent-color);
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1.5rem;
            background: var(--bg-secondary);
            border-radius: 0.5rem;
            overflow: hidden;
            border: 1px solid var(--border-color);
        }

        thead {
            background: var(--bg-tertiary);
        }

        th {
            padding: 0.875rem 1rem;
            text-align: left;
            font-weight: 600;
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            color: var(--text-secondary);
            border-bottom: 1px solid var(--border-color);
        }

        td {
            padding: 0.875rem 1rem;
            border-bottom: 1px solid var(--border-color);
            font-size: 0.875rem;
        }

        tbody tr:hover {
            background: var(--bg-tertiary);
        }

        tbody tr:last-child td {
            border-bottom: none;
        }

        a {
            color: var(--accent-color);
            text-decoration: none;
            word-break: break-all;
        }

        a:hover {
            color: var(--accent-hover);
            text-decoration: underline;
        }

        .priority {
            display: inline-flex;
            align-items: center;
            gap: 0.375rem;
        }

        .priority-dot {
            width: 0.5rem;
            height: 0.5rem;
            border-radius: 50%;
        }

        .priority-high .priority-dot {
            background: var(--priority-high);
        }

        .priority-medium .priority-dot {
            background: var(--priority-medium);
        }

        .priority-low .priority-dot {
            background: var(--priority-low);
        }

        .changefreq {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            background: var(--bg-tertiary);
            border-radius: 0.25rem;
            font-size: 0.75rem;
            color: var(--text-secondary);
        }

        .date {
            color: var(--text-muted);
            font-size: 0.8125rem;
        }

        footer {
            margin-top: 2rem;
            padding-top: 1rem;
            border-top: 1px solid var(--border-color);
            text-align: center;
            color: var(--text-muted);
            font-size: 0.75rem;
        }

        @media (max-width: 768px) {
            body {
                padding: 1rem;
            }

            .stats {
                gap: 1rem;
            }

            th, td {
                padding: 0.625rem 0.5rem;
            }

            .hide-mobile {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>🗺️ Sitemap</h1>
            <p class="subtitle">This sitemap contains all pages available on this website.</p>
            <div class="stats">
                <div class="stat">
                    <div class="stat-label">Total URLs</div>
                    <div class="stat-value"><xsl:value-of select="count(sitemap:urlset/sitemap:url)"/></div>
                </div>
            </div>
        </header>

        <table>
            <thead>
                <tr>
                    <th>URL</th>
                    <th class="hide-mobile">Priority</th>
                    <th class="hide-mobile">Change Frequency</th>
                    <th class="hide-mobile">Last Modified</th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="sitemap:urlset/sitemap:url">
                    <xsl:sort select="sitemap:priority" order="descending"/>
                    <tr>
                        <td>
                            <a href="{sitemap:loc}"><xsl:value-of select="sitemap:loc"/></a>
                        </td>
                        <td class="hide-mobile">
                            <xsl:choose>
                                <xsl:when test="sitemap:priority &gt;= 0.8">
                                    <span class="priority priority-high">
                                        <span class="priority-dot"></span>
                                        <xsl:value-of select="sitemap:priority"/>
                                    </span>
                                </xsl:when>
                                <xsl:when test="sitemap:priority &gt;= 0.5">
                                    <span class="priority priority-medium">
                                        <span class="priority-dot"></span>
                                        <xsl:value-of select="sitemap:priority"/>
                                    </span>
                                </xsl:when>
                                <xsl:otherwise>
                                    <span class="priority priority-low">
                                        <span class="priority-dot"></span>
                                        <xsl:value-of select="sitemap:priority"/>
                                    </span>
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                        <td class="hide-mobile">
                            <xsl:if test="sitemap:changefreq">
                                <span class="changefreq"><xsl:value-of select="sitemap:changefreq"/></span>
                            </xsl:if>
                        </td>
                        <td class="hide-mobile">
                            <xsl:if test="sitemap:lastmod">
                                <span class="date"><xsl:value-of select="sitemap:lastmod"/></span>
                            </xsl:if>
                        </td>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>

        <footer>
            <p>Generated by Typstify • XML Sitemap Protocol</p>
        </footer>
    </div>
</body>
</html>
</xsl:template>

</xsl:stylesheet>"#.to_string()
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, path::PathBuf};

    use typstify_core::config::LanguageConfig;

    use super::*;

    fn test_config() -> Config {
        Config {
            site: typstify_core::config::SiteConfig {
                title: "Test Site".to_string(),
                base_url: "https://example.com".to_string(),
                default_language: "en".to_string(),
                description: None,
                author: None,
            },
            languages: HashMap::new(),
            build: typstify_core::config::BuildConfig::default(),
            search: typstify_core::config::SearchConfig::default(),
            rss: typstify_core::config::RssConfig::default(),
            robots: typstify_core::config::RobotsConfig::default(),
            taxonomies: typstify_core::config::TaxonomyConfig::default(),
        }
    }

    fn test_page(slug: &str, date: Option<DateTime<Utc>>) -> Page {
        Page {
            url: format!("/{}", slug),
            title: slug.to_string(),
            description: None,
            date,
            updated: None,
            draft: false,
            lang: "en".to_string(),
            is_default_lang: true,
            canonical_id: slug.to_string(),
            tags: vec![],
            categories: vec![],
            content: String::new(),
            summary: None,
            reading_time: None,
            word_count: None,
            toc: vec![],
            custom_js: vec![],
            custom_css: vec![],
            aliases: vec![],
            template: None,
            weight: 0,
            source_path: Some(PathBuf::from("test.md")),
        }
    }

    #[test]
    fn test_generate_sitemap() {
        let generator = SitemapGenerator::new(test_config());
        let page1 = test_page("about", None);
        let page2 = test_page("blog/post-1", Some(Utc::now()));
        let pages: Vec<&Page> = vec![&page1, &page2];

        let xml = generator.generate(&pages).unwrap();

        assert!(xml.contains(r#"<?xml version="1.0""#));
        assert!(xml.contains("<urlset"));
        assert!(xml.contains("<loc>https://example.com/about</loc>"));
        assert!(xml.contains("<loc>https://example.com/blog/post-1</loc>"));
        assert!(xml.contains("<changefreq>"));
        assert!(xml.contains("<priority>"));
    }

    #[test]
    fn test_escape_xml() {
        assert_eq!(escape_xml("a & b"), "a &amp; b");
        assert_eq!(escape_xml("<tag>"), "&lt;tag&gt;");
        assert_eq!(escape_xml("\"quoted\""), "&quot;quoted&quot;");
    }

    #[test]
    fn test_home_page_priority() {
        let generator = SitemapGenerator::new(test_config());
        let mut home = test_page("", None);
        home.url = "/".to_string();

        let url = generator.page_to_url(&home);

        assert_eq!(url.priority, Some(1.0));
        assert_eq!(url.changefreq, Some(ChangeFreq::Daily));
    }

    #[test]
    fn test_generate_index() {
        let generator = SitemapGenerator::new(test_config());
        let sitemaps = vec!["sitemap-posts.xml", "sitemap-pages.xml"];

        let xml = generator.generate_index(&sitemaps);

        assert!(xml.contains("<sitemapindex"));
        assert!(xml.contains("sitemap-posts.xml"));
        assert!(xml.contains("sitemap-pages.xml"));
    }

    #[test]
    fn test_multilang_sitemap() {
        let mut config = test_config();
        config.languages.insert(
            "en".to_string(),
            LanguageConfig {
                name: Some("English".to_string()),
                title: None,
                description: None,
            },
        );
        config.languages.insert(
            "zh".to_string(),
            LanguageConfig {
                name: Some("中文".to_string()),
                title: None,
                description: None,
            },
        );
        let generator = SitemapGenerator::new(config);

        let page = test_page("about", None);
        let pages: Vec<&Page> = vec![&page];

        let xml = generator.generate(&pages).unwrap();

        assert!(xml.contains("xmlns:xhtml"));
        assert!(xml.contains(r#"hreflang="en""#));
        assert!(xml.contains(r#"hreflang="zh""#));
    }
}