typstify 0.1.3

A high-performance static site generator with Typst/Markdown support
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
//! Check command - validate configuration and content

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

use color_eyre::eyre::{Result, bail};
use typstify_core::Config;
use typstify_parser::ParserRegistry;

/// Validation result.
#[derive(Debug, Default)]
struct ValidationResult {
    errors: Vec<String>,
    warnings: Vec<String>,
}

impl ValidationResult {
    fn add_error(&mut self, msg: impl Into<String>) {
        self.errors.push(msg.into());
    }

    fn add_warning(&mut self, msg: impl Into<String>) {
        self.warnings.push(msg.into());
    }

    fn has_errors(&self) -> bool {
        !self.errors.is_empty()
    }

    fn has_warnings(&self) -> bool {
        !self.warnings.is_empty()
    }
}

/// Run the check command.
///
/// Validates configuration and all content files.
pub fn run(config_path: &Path, strict: bool) -> Result<()> {
    tracing::info!(?config_path, strict, "Checking configuration and content");

    let mut result = ValidationResult::default();

    // Validate configuration
    println!("Checking configuration...");
    let config = match Config::load(config_path) {
        Ok(c) => {
            println!("  ✓ Configuration valid");
            Some(c)
        }
        Err(e) => {
            result.add_error(format!("Configuration error: {e}"));
            println!("  ✗ Configuration invalid: {e}");
            None
        }
    };

    // Validate content files
    let content_dir = Path::new("content");
    if content_dir.exists() {
        println!("\nChecking content files...");
        validate_content_files(content_dir, &mut result)?;

        // Check for multi-language content completeness
        if let Some(ref cfg) = config {
            println!("\nChecking multi-language content...");
            validate_language_content(content_dir, cfg, &mut result)?;
        }
    } else {
        result.add_warning("Content directory does not exist");
    }

    // Check required directories
    println!("\nChecking directories...");
    check_directories(&mut result);

    // Check for common issues
    if let Some(ref cfg) = config {
        println!("\nChecking configuration values...");
        check_config_values(cfg, &mut result);
    }

    // Print summary
    println!();
    println!("Summary:");
    println!("  Errors:   {}", result.errors.len());
    println!("  Warnings: {}", result.warnings.len());

    if result.has_errors() {
        println!();
        println!("Errors:");
        for err in &result.errors {
            println!("{err}");
        }
    }

    if result.has_warnings() {
        println!();
        println!("Warnings:");
        for warn in &result.warnings {
            println!("{warn}");
        }
    }

    // Determine exit status
    if result.has_errors() {
        bail!("Validation failed with {} error(s)", result.errors.len());
    }

    if strict && result.has_warnings() {
        bail!(
            "Validation failed with {} warning(s) (strict mode)",
            result.warnings.len()
        );
    }

    println!();
    println!("✓ All checks passed");

    Ok(())
}

/// Quick validation for build/watch commands.
///
/// Returns warnings for missing language translations (non-fatal).
/// Call this before starting build/watch.
pub fn quick_validate(config: &Config) -> Vec<String> {
    let mut warnings = Vec::new();
    let content_dir = Path::new("content");

    if !content_dir.exists() {
        return warnings;
    }

    let all_langs = config.all_languages();
    if all_langs.len() <= 1 {
        return warnings;
    }

    let default_lang = &config.site.default_language;

    // Collect all content files and group by canonical name
    let mut files_by_canonical: HashMap<String, Vec<String>> = HashMap::new();

    if let Ok(entries) = std::fs::read_dir(content_dir) {
        for entry in entries.filter_map(|e| e.ok()) {
            let path = entry.path();
            if path.is_file() {
                let filename = path
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string();
                if let Some((canonical, lang)) =
                    parse_content_file(&filename, default_lang, &all_langs)
                {
                    files_by_canonical.entry(canonical).or_default().push(lang);
                }
            }
        }
    }

    // Check for missing translations of index page
    let canonical = "index";
    if let Some(langs) = files_by_canonical.get(canonical) {
        for lang in &all_langs {
            let lang_str = (*lang).to_string();
            if !langs.contains(&lang_str) {
                let expected_file = if *lang == default_lang.as_str() {
                    "index.md".to_string()
                } else {
                    format!("index.{lang}.md")
                };
                warnings.push(format!(
                    "Missing content/{expected_file} - visiting /{lang}/  will show 404",
                ));
            }
        }
    }

    warnings
}

/// Validate all content files in the given directory.
fn validate_content_files(dir: &Path, result: &mut ValidationResult) -> Result<()> {
    let registry = ParserRegistry::new();
    let mut checked = 0;
    let mut failed = 0;

    for entry in walkdir::WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
    {
        let path = entry.path();
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");

        // Skip non-content files
        if !matches!(ext, "md" | "typ") {
            continue;
        }

        checked += 1;

        // Try to parse the file
        let content = match std::fs::read_to_string(path) {
            Ok(c) => c,
            Err(e) => {
                result.add_error(format!("{}: Failed to read file: {e}", path.display()));
                failed += 1;
                continue;
            }
        };

        if let Err(e) = registry.parse(&content, path) {
            result.add_error(format!("{}: Parse error: {e}", path.display()));
            failed += 1;
        }
    }

    if failed == 0 {
        println!("  ✓ All {checked} content files valid");
    } else {
        println!("{failed}/{checked} content files have errors");
    }

    Ok(())
}

/// Check that required directories exist.
fn check_directories(result: &mut ValidationResult) {
    let dirs = [
        ("content", true),
        ("templates", false),
        ("style", false),
        ("assets", false),
    ];

    for (dir, required) in dirs {
        let path = Path::new(dir);
        if path.exists() {
            println!("{dir}/ exists");
        } else if required {
            result.add_error(format!("Required directory missing: {dir}/"));
            println!("{dir}/ missing (required)");
        } else {
            result.add_warning(format!("Optional directory missing: {dir}/"));
            println!("{dir}/ missing (optional)");
        }
    }
}

/// Check configuration values for common issues.
fn check_config_values(config: &Config, result: &mut ValidationResult) {
    // Check base_url
    if config.site.base_url.is_empty() {
        result.add_warning("site.base_url is empty");
    } else if !config.site.base_url.starts_with("http") {
        result.add_warning("site.base_url should start with http:// or https://");
    }

    // Check title
    if config.site.title.is_empty() {
        result.add_warning("site.title is empty");
    }

    // Check output directory
    let output = Path::new(&config.build.output_dir);
    if output.exists() && !output.is_dir() {
        result.add_error(format!(
            "Output path exists but is not a directory: {}",
            config.build.output_dir
        ));
    }

    // Check for conflicting language settings
    let all_langs = config.all_languages();
    if all_langs.len() > 1 {
        // Check if default language is in the languages map
        if !config.languages.contains_key(&config.site.default_language)
            && config.site.default_language != "en"
        {
            result.add_warning(format!(
                "Default language '{}' not explicitly configured in [languages] section",
                config.site.default_language
            ));
        }
    }

    println!("  ✓ Configuration values checked");
}

/// Validate multi-language content completeness.
///
/// Checks that important content files exist for all configured languages.
fn validate_language_content(
    content_dir: &Path,
    config: &Config,
    result: &mut ValidationResult,
) -> Result<()> {
    let all_langs = config.all_languages();

    // Only check if multiple languages are configured
    if all_langs.len() <= 1 {
        println!("  ✓ Single language configured, skipping multi-language checks");
        return Ok(());
    }

    let default_lang = &config.site.default_language;

    // Collect all content files and group by canonical name
    let mut files_by_canonical: HashMap<String, Vec<String>> = HashMap::new();

    for entry in walkdir::WalkDir::new(content_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
    {
        let path = entry.path();
        let relative = path
            .strip_prefix(content_dir)
            .unwrap_or(path)
            .to_string_lossy()
            .to_string();

        // Parse the filename to extract language suffix
        // e.g., "index.md" (default), "index.zh.md" (zh), "posts/hello.md", "posts/hello.zh.md"
        if let Some((canonical, lang)) = parse_content_file(&relative, default_lang, &all_langs) {
            files_by_canonical.entry(canonical).or_default().push(lang);
        }
    }

    // Check for missing translations of important pages
    let mut missing_count = 0;
    let important_pages = ["index.md", "about.md"];

    for page in important_pages {
        let canonical = page.replace(".md", "");
        if let Some(langs) = files_by_canonical.get(&canonical) {
            for lang in &all_langs {
                let lang_str = (*lang).to_string();
                if !langs.contains(&lang_str) {
                    let expected_file = if *lang == default_lang.as_str() {
                        page.to_string()
                    } else {
                        page.replace(".md", &format!(".{lang}.md"))
                    };
                    result.add_warning(format!(
                        "Missing translation: content/{expected_file} (language: {lang})",
                    ));
                    missing_count += 1;
                }
            }
        }
    }

    // Report summary
    let total_pages = files_by_canonical.len();
    let mut fully_translated = 0;
    let mut partially_translated = 0;

    for langs in files_by_canonical.values() {
        if langs.len() == all_langs.len() {
            fully_translated += 1;
        } else if langs.len() > 1 {
            partially_translated += 1;
        }
    }

    if missing_count == 0 {
        println!(
            "  ✓ All important pages have translations ({} languages)",
            all_langs.len()
        );
    } else {
        println!("{missing_count} missing translation(s) for important pages");
    }

    println!(
        "  ℹ Content summary: {total_pages} pages, {fully_translated} fully translated, {partially_translated} partially translated"
    );

    Ok(())
}

/// Parse a content file path to extract canonical name and language.
///
/// Returns (canonical_name, language_code)
fn parse_content_file(
    path: &str,
    default_lang: &str,
    all_langs: &[&str],
) -> Option<(String, String)> {
    let ext = if path.ends_with(".md") {
        ".md"
    } else if path.ends_with(".typ") {
        ".typ"
    } else {
        return None;
    };

    let without_ext = path.strip_suffix(ext)?;

    // Check for language suffix like ".zh" or ".ja"
    for lang in all_langs {
        if *lang != default_lang {
            let suffix = format!(".{lang}");
            if without_ext.ends_with(&suffix) {
                let canonical = without_ext.strip_suffix(&suffix)?.to_string();
                return Some((canonical, (*lang).to_string()));
            }
        }
    }

    // No language suffix means default language
    Some((without_ext.to_string(), default_lang.to_string()))
}