typstify-generator 0.1.5

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
//! Content collection and organization.
//!
//! Walks the content directory and collects all pages into a structured hierarchy.

use std::{
    collections::HashMap,
    fs,
    path::{Path, PathBuf},
};

use rayon::prelude::*;
use thiserror::Error;
use tracing::{debug, info, warn};
use typstify_core::{Config, ContentPath, ContentType, Page};
use typstify_parser::ParserRegistry;

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

    /// Parser error.
    #[error("parse error in {path}: {message}")]
    Parse { path: PathBuf, message: String },

    /// Invalid content path.
    #[error("invalid content path: {0}")]
    InvalidPath(PathBuf),
}

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

/// Collected site content.
#[derive(Debug, Default)]
pub struct SiteContent {
    /// All pages indexed by slug.
    pub pages: HashMap<String, Page>,

    /// Pages organized by section (first path component).
    pub sections: HashMap<String, Vec<String>>,

    /// Taxonomy term to page slugs mapping.
    pub taxonomies: TaxonomyIndex,

    /// Translation groups (canonical_id -> [slugs]).
    pub translations: HashMap<String, Vec<String>>,
}

/// Index of taxonomy terms.
#[derive(Debug, Default)]
pub struct TaxonomyIndex {
    /// Tag -> page slugs.
    pub tags: HashMap<String, Vec<String>>,

    /// Category -> page slugs.
    pub categories: HashMap<String, Vec<String>>,
}

/// Content collector that walks directories and parses files.
#[derive(Debug)]
pub struct ContentCollector<'a> {
    config: &'a Config,
    parser: ParserRegistry,
    content_dir: PathBuf,
}

impl<'a> ContentCollector<'a> {
    /// Create a new content collector.
    #[must_use]
    pub fn new(config: &'a Config, content_dir: impl Into<PathBuf>) -> Self {
        Self {
            config,
            parser: ParserRegistry::new(),
            content_dir: content_dir.into(),
        }
    }

    /// Collect all content from the content directory.
    pub fn collect(&self) -> Result<SiteContent> {
        info!(dir = %self.content_dir.display(), "collecting content");

        // Find all content files
        let files = self.find_content_files()?;
        info!(count = files.len(), "found content files");

        // Parse files in parallel
        let pages: Vec<_> = files
            .par_iter()
            .filter_map(|path| {
                match self.parse_file(path) {
                    Ok(page) => {
                        // Filter drafts unless configured to include them
                        if page.draft && !self.config.build.drafts {
                            debug!(url = %page.url, "skipping draft");
                            None
                        } else {
                            Some(page)
                        }
                    }
                    Err(e) => {
                        warn!(path = %path.display(), error = %e, "failed to parse file");
                        None
                    }
                }
            })
            .collect();

        // Build site content structure
        let mut content = SiteContent::default();

        for page in pages {
            let url = page.url.clone();
            let slug = url.trim_start_matches('/').to_string();

            // Add to sections
            let section = slug.split('/').next().unwrap_or("").to_string();
            if !section.is_empty() {
                content
                    .sections
                    .entry(section)
                    .or_default()
                    .push(url.clone());
            }

            // Index taxonomies
            for tag in &page.tags {
                content
                    .taxonomies
                    .tags
                    .entry(tag.clone())
                    .or_default()
                    .push(url.clone());
            }
            for category in &page.categories {
                content
                    .taxonomies
                    .categories
                    .entry(category.clone())
                    .or_default()
                    .push(url.clone());
            }

            // Index translations
            if !page.canonical_id.is_empty() {
                content
                    .translations
                    .entry(page.canonical_id.clone())
                    .or_default()
                    .push(url.clone());
            }

            content.pages.insert(url, page);
        }

        info!(
            pages = content.pages.len(),
            sections = content.sections.len(),
            tags = content.taxonomies.tags.len(),
            categories = content.taxonomies.categories.len(),
            "content collection complete"
        );

        Ok(content)
    }

    /// Find all content files recursively.
    fn find_content_files(&self) -> Result<Vec<PathBuf>> {
        let mut files = Vec::new();
        self.walk_dir(&self.content_dir, &mut files)?;
        Ok(files)
    }

    /// Recursively walk a directory for content files.
    fn walk_dir(&self, dir: &Path, files: &mut Vec<PathBuf>) -> Result<()> {
        if !dir.exists() {
            return Ok(());
        }

        for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();

            if path.is_dir() {
                // Skip hidden directories
                if path
                    .file_name()
                    .is_some_and(|n| n.to_string_lossy().starts_with('.'))
                {
                    continue;
                }
                self.walk_dir(&path, files)?;
            } else if path.is_file() {
                // Check if it's a content file
                if let Some(ext) = path.extension()
                    && ContentType::from_extension(&ext.to_string_lossy()).is_some()
                {
                    files.push(path);
                }
            }
        }

        Ok(())
    }

    /// Parse a single content file into a Page.
    fn parse_file(&self, path: &Path) -> Result<Page> {
        debug!(path = %path.display(), "parsing file");

        // Read file content
        let content = fs::read_to_string(path)?;

        // Parse content path to extract slug and language
        let relative_path = path.strip_prefix(&self.content_dir).unwrap_or(path);
        let content_path =
            ContentPath::from_path(relative_path, &self.config.site.default_language)
                .ok_or_else(|| CollectorError::InvalidPath(path.to_path_buf()))?;

        // Parse content using appropriate parser
        let parsed = self
            .parser
            .parse(&content, path)
            .map_err(|e| CollectorError::Parse {
                path: path.to_path_buf(),
                message: e.to_string(),
            })?;

        Ok(Page::from_parsed(parsed, &content_path))
    }

    /// Get pages sorted by date (newest first).
    pub fn pages_by_date(content: &SiteContent) -> Vec<&Page> {
        let mut pages: Vec<_> = content.pages.values().collect();
        pages.sort_by(|a, b| match (&b.date, &a.date) {
            (Some(b_date), Some(a_date)) => b_date.cmp(a_date),
            (Some(_), None) => std::cmp::Ordering::Less,
            (None, Some(_)) => std::cmp::Ordering::Greater,
            (None, None) => a.title.cmp(&b.title),
        });
        pages
    }

    /// Get pages for a specific section, sorted by date.
    pub fn section_pages<'b>(content: &'b SiteContent, section: &str) -> Vec<&'b Page> {
        let mut pages: Vec<_> = content
            .sections
            .get(section)
            .map(|urls| urls.iter().filter_map(|u| content.pages.get(u)).collect())
            .unwrap_or_default();

        pages.sort_by(|a, b| match (&b.date, &a.date) {
            (Some(b_date), Some(a_date)) => b_date.cmp(a_date),
            (Some(_), None) => std::cmp::Ordering::Less,
            (None, Some(_)) => std::cmp::Ordering::Greater,
            (None, None) => a.title.cmp(&b.title),
        });
        pages
    }

    /// Get pages for a taxonomy term, sorted by date.
    pub fn taxonomy_pages<'b>(
        content: &'b SiteContent,
        taxonomy: &str,
        term: &str,
    ) -> Vec<&'b Page> {
        let urls = match taxonomy {
            "tags" => content.taxonomies.tags.get(term),
            "categories" => content.taxonomies.categories.get(term),
            _ => None,
        };

        let mut pages: Vec<_> = urls
            .map(|u| u.iter().filter_map(|url| content.pages.get(url)).collect())
            .unwrap_or_default();

        pages.sort_by(|a, b| match (&b.date, &a.date) {
            (Some(b_date), Some(a_date)) => b_date.cmp(a_date),
            (Some(_), None) => std::cmp::Ordering::Less,
            (None, Some(_)) => std::cmp::Ordering::Greater,
            (None, None) => a.title.cmp(&b.title),
        });
        pages
    }
}

/// Paginate a slice of items.
pub fn paginate<T>(items: &[T], page: usize, per_page: usize) -> (&[T], usize) {
    let total_pages = items.len().div_ceil(per_page);
    let start = (page - 1) * per_page;
    let end = (start + per_page).min(items.len());

    if start >= items.len() {
        (&[], total_pages)
    } else {
        (&items[start..end], total_pages)
    }
}

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

    use typstify_core::test_fixtures::test_config;

    use super::*;

    #[test]
    fn test_paginate() {
        let items = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        let (page1, total) = paginate(&items, 1, 3);
        assert_eq!(page1, &[1, 2, 3]);
        assert_eq!(total, 4);

        let (page2, _) = paginate(&items, 2, 3);
        assert_eq!(page2, &[4, 5, 6]);

        let (page4, _) = paginate(&items, 4, 3);
        assert_eq!(page4, &[10]);

        let (page5, _) = paginate(&items, 5, 3);
        assert!(page5.is_empty());
    }

    #[test]
    fn test_taxonomy_index() {
        let mut index = TaxonomyIndex::default();
        index.tags.insert(
            "rust".to_string(),
            vec!["post1".to_string(), "post2".to_string()],
        );
        index
            .tags
            .insert("web".to_string(), vec!["post2".to_string()]);

        assert_eq!(index.tags.get("rust").unwrap().len(), 2);
        assert_eq!(index.tags.get("web").unwrap().len(), 1);
        assert!(!index.tags.contains_key("python"));
    }

    #[test]
    fn test_site_content_default() {
        let content = SiteContent::default();
        assert!(content.pages.is_empty());
        assert!(content.sections.is_empty());
        assert!(content.taxonomies.tags.is_empty());
    }

    fn testdata_dir() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("testdata")
            .join("content")
    }

    #[test]
    fn collect_returns_correct_page_count() {
        let config = test_config();
        let collector = ContentCollector::new(&config, testdata_dir());
        let content = collector.collect().expect("collect should succeed");

        // 5 content files in testdata: hello-world.md, hello-world.zh.md,
        // getting-started.md, about.md, technical-spec.typ
        assert_eq!(content.pages.len(), 5);
    }

    #[test]
    fn collect_assigns_pages_to_correct_sections() {
        let config = test_config();
        let collector = ContentCollector::new(&config, testdata_dir());
        let content = collector.collect().expect("collect should succeed");

        // "posts" section should have 2 default-language pages (hello-world, getting-started)
        let posts = content
            .sections
            .get("posts")
            .expect("posts section should exist");
        assert_eq!(posts.len(), 2);
        assert!(posts.contains(&"/posts/hello-world".to_string()));
        assert!(posts.contains(&"/posts/getting-started".to_string()));

        // The zh translation is filed under "zh" section (first URL path component)
        let zh = content.sections.get("zh").expect("zh section should exist");
        assert!(zh.contains(&"/zh/posts/hello-world".to_string()));

        // "about" section should have 1 page
        let about = content
            .sections
            .get("about")
            .expect("about section should exist");
        assert_eq!(about.len(), 1);
        assert!(about.contains(&"/about".to_string()));

        // "docs" section should have 1 page
        let docs = content
            .sections
            .get("docs")
            .expect("docs section should exist");
        assert_eq!(docs.len(), 1);
        assert!(docs.contains(&"/docs/technical-spec".to_string()));
    }

    #[test]
    fn collect_builds_translation_groups() {
        let config = test_config();
        let collector = ContentCollector::new(&config, testdata_dir());
        let content = collector.collect().expect("collect should succeed");

        // hello-world.md and hello-world.zh.md share canonical_id "posts/hello-world"
        let group = content
            .translations
            .get("posts/hello-world")
            .expect("translation group should exist");
        assert_eq!(group.len(), 2);
        assert!(group.contains(&"/posts/hello-world".to_string()));
        assert!(group.contains(&"/zh/posts/hello-world".to_string()));
    }

    #[test]
    fn collect_indexes_taxonomy_entries() {
        let config = test_config();
        let collector = ContentCollector::new(&config, testdata_dir());
        let content = collector.collect().expect("collect should succeed");

        // hello-world.md has tags ["intro", "welcome"]
        let intro_pages = content
            .taxonomies
            .tags
            .get("intro")
            .expect("'intro' tag should exist");
        assert!(intro_pages.contains(&"/posts/hello-world".to_string()));

        let welcome_pages = content
            .taxonomies
            .tags
            .get("welcome")
            .expect("'welcome' tag should exist");
        assert!(welcome_pages.contains(&"/posts/hello-world".to_string()));

        // getting-started.md has tags ["tutorial", "beginner"]
        assert!(content.taxonomies.tags.contains_key("tutorial"));
        assert!(content.taxonomies.tags.contains_key("beginner"));

        // technical-spec.typ has tags ["typst", "technical", "spec"]
        assert!(content.taxonomies.tags.contains_key("typst"));
        assert!(content.taxonomies.tags.contains_key("technical"));
        assert!(content.taxonomies.tags.contains_key("spec"));
    }

    #[test]
    fn collect_excludes_drafts_when_config_disallows() {
        let tmpdir = tempfile::tempdir().expect("create temp dir");
        let content_dir = tmpdir.path().join("content");
        let posts_dir = content_dir.join("posts");
        fs::create_dir_all(&posts_dir).expect("create posts dir");

        // Write a draft page
        let draft_content = "---\ntitle: \"Draft Post\"\ndate: 2024-01-01T00:00:00Z\ndraft: true\ntags: []\n---\nThis is a draft.\n";
        fs::write(posts_dir.join("draft-post.md"), draft_content).expect("write draft file");

        // Write a published page
        let published_content = "---\ntitle: \"Published Post\"\ndate: 2024-01-01T00:00:00Z\ndraft: false\ntags: []\n---\nThis is published.\n";
        fs::write(posts_dir.join("published-post.md"), published_content)
            .expect("write published file");

        // With drafts disabled (default)
        let config = test_config();
        let collector = ContentCollector::new(&config, &content_dir);
        let content = collector.collect().expect("collect should succeed");

        assert_eq!(
            content.pages.len(),
            1,
            "only published page should be collected"
        );
        assert!(
            content.pages.contains_key("/posts/published-post"),
            "published page should be present"
        );
        assert!(
            !content.pages.contains_key("/posts/draft-post"),
            "draft page should be excluded"
        );

        // With drafts enabled
        let config_with_drafts = Config::from_parts(
            typstify_core::config::SiteConfig {
                title: "Test Site".to_string(),
                host: "https://example.com".to_string(),
                base_path: String::new(),
                default_language: "en".to_string(),
                description: None,
                author: None,
            },
            typstify_core::config::BuildConfig {
                drafts: true,
                ..Default::default()
            },
            typstify_core::config::SearchConfig::default(),
            typstify_core::config::RssConfig::default(),
            typstify_core::config::RobotsConfig::default(),
            typstify_core::config::TaxonomyConfig::default(),
            HashMap::new(),
        );
        let collector_with_drafts = ContentCollector::new(&config_with_drafts, &content_dir);
        let content_with_drafts = collector_with_drafts
            .collect()
            .expect("collect should succeed");

        assert_eq!(
            content_with_drafts.pages.len(),
            2,
            "both pages should be collected when drafts enabled"
        );
        assert!(
            content_with_drafts.pages.contains_key("/posts/draft-post"),
            "draft page should be included when drafts enabled"
        );
    }
}