ssg-core 0.0.56

Core compilation pipeline for SSG — no system dependencies, WASM-compatible.
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
#![forbid(unsafe_code)]
// Copyright © 2023 - 2026 Static Site Generator (SSG). All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! # ssg-core — Platform-independent SSG compilation pipeline
//!
//! This crate contains the pure-logic core of SSG, with no system
//! dependencies (`rayon`, `http-handle`). It compiles to
//! `wasm32-wasi` and `wasm32-unknown-unknown` (via `wasm-bindgen`).
//!
//! ## Features
//!
//! - Markdown → HTML compilation (pulldown-cmark with GFM extensions)
//! - Frontmatter parsing (TOML/JSON/YAML)
//! - Template rendering (when `minijinja` is enabled)
//! - Shortcode expansion
//! - SEO metadata generation
//! - Search index generation

pub mod content_provider;
pub mod isr_manifest;

pub use content_provider::{
    ContentProvider, FsContentProvider, MemoryContentProvider, ProviderError,
    ProviderResult,
};
pub use isr_manifest::{
    build_entry, hash_sources, CachePolicy, Manifest, ManifestEntry,
    DEFAULT_SWR, DEFAULT_S_MAXAGE, MANIFEST_VERSION,
};

use std::collections::HashMap;
use std::fmt;

/// The error type for ssg-core operations.
///
/// # Examples
///
/// ```
/// use ssg_core::Error;
///
/// let err = Error::InvalidSlug { input: "@@@".to_string() };
/// assert!(err.to_string().contains("Invalid slug input"));
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// TOML/YAML/JSON parsing failures.
    FrontmatterParse {
        /// The syntax format (e.g. "toml", "yaml", "json") or parse error detail.
        syntax: String,
    },
    /// Markdown rendering bugs.
    MarkdownCompile {
        /// Detail about what failed.
        source: String,
    },
    /// Slugification layout validation failures.
    InvalidSlug {
        /// The invalid input string.
        input: String,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::FrontmatterParse { syntax } => {
                write!(f, "Frontmatter parse error: {syntax}")
            }
            Self::MarkdownCompile { source } => {
                write!(f, "Markdown compilation error: {source}")
            }
            Self::InvalidSlug { input } => {
                write!(f, "Invalid slug input: {input}")
            }
        }
    }
}

impl std::error::Error for Error {}

/// Specialized Result type for ssg-core operations.
pub type Result<T> = std::result::Result<T, Error>;

/// Compile a Markdown string to HTML.
///
/// Supports GitHub Flavored Markdown: tables, strikethrough, task lists.
///
/// # Example
///
/// ```
/// let html = ssg_core::compile_markdown("# Hello\n\nWorld");
/// assert!(html.contains("<h1>Hello</h1>"));
/// assert!(html.contains("<p>World</p>"));
/// ```
#[must_use]
pub fn compile_markdown(input: &str) -> String {
    use pulldown_cmark::{html, Options, Parser};

    let options = Options::ENABLE_TABLES
        | Options::ENABLE_STRIKETHROUGH
        | Options::ENABLE_TASKLISTS;

    let parser = Parser::new_ext(input, options);
    let mut html_output = String::with_capacity(input.len() * 2);
    html::push_html(&mut html_output, parser);
    html_output
}

/// Parse frontmatter from a Markdown file.
///
/// Supports TOML (`+++`), YAML (`---`), and JSON (`{`) delimiters.
/// Returns `(frontmatter_map, body_without_frontmatter)`.
///
/// # Example
///
/// ```
/// let input = "---\ntitle: Hello\n---\n# Body";
/// let (fm, body) = ssg_core::parse_frontmatter(input);
/// assert_eq!(fm.get("title").and_then(|v| v.as_str()), Some("Hello"));
/// assert!(body.contains("# Body"));
/// ```
pub fn parse_frontmatter(
    input: &str,
) -> (HashMap<String, serde_json::Value>, String) {
    // Zero-copy core (issue #578, plan §4 3.1): the body is sliced out
    // of `input` exactly once and materialised exactly once here — no
    // per-branch `to_string()` and no metadata-map clone rebuilds.
    let (map, body) = parse_frontmatter_borrowed(input);
    (map, body.to_string())
}

/// Borrowed-body core of [`parse_frontmatter`].
///
/// Returns the metadata map by *moving* parsed entries (never cloning
/// them) and the body as a slice of `input`, leaving the single owned
/// materialisation to the public wrapper (issue #578, plan §4 3.1).
fn parse_frontmatter_borrowed(
    input: &str,
) -> (HashMap<String, serde_json::Value>, &str) {
    let trimmed = input.trim_start();

    // TOML frontmatter: +++...+++
    if let Some(after) = trimmed.strip_prefix("+++") {
        if let Some(end) = after.find("+++") {
            let fm_str = &after[..end];
            let body = &after[end + 3..];
            if let Ok(serde_json::Value::Object(map)) =
                toml::from_str::<serde_json::Value>(fm_str)
            {
                // Move the parsed entries into the final map — the
                // previous `(k.clone(), v.clone())` rebuild is gone.
                return (map.into_iter().collect(), body);
            }
            return (HashMap::new(), body);
        }
    }

    // YAML frontmatter: ---...---
    if let Some(after) = trimmed.strip_prefix("---") {
        if let Some(end) = after.find("---") {
            let fm_str = &after[..end];
            let body = &after[end + 3..];
            match noyalib::from_str::<serde_json::Value>(fm_str) {
                Ok(serde_json::Value::Object(map)) => {
                    return (map.into_iter().collect(), body);
                }
                Ok(_) => {
                    // Top-level non-mapping (e.g. a bare list or scalar)
                    // — preserve the body but emit no globals.
                    return (HashMap::new(), body);
                }
                Err(e) => {
                    log::warn!("YAML frontmatter parse error: {e}");
                    return (HashMap::new(), body);
                }
            }
        }
    }

    // JSON frontmatter: {...}
    if trimmed.starts_with('{') {
        // Find matching closing brace
        let mut depth = 0;
        let mut end = None;
        for (i, c) in trimmed.char_indices() {
            match c {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        end = Some(i + 1);
                        break;
                    }
                }
                _ => {}
            }
        }
        if let Some(end_pos) = end {
            let fm_str = &trimmed[..end_pos];
            let body = &trimmed[end_pos..];
            if let Ok(map) = serde_json::from_str::<
                HashMap<String, serde_json::Value>,
            >(fm_str)
            {
                return (map, body);
            }
        }
    }

    (HashMap::new(), input)
}

/// Compile a complete page: parse frontmatter, render Markdown to HTML.
///
/// Returns `(frontmatter, html_body)`.
///
/// # Errors
/// Currently infallible — returns `Ok` for every input. The `Result`
/// signature is preserved so that future stricter validation can
/// surface failures without a breaking API change.
///
/// # Examples
///
/// ```
/// let input = "---\ntitle: Test\n---\n# Heading";
/// let (fm, html) = ssg_core::compile_page(input).unwrap();
/// assert_eq!(fm.get("title").and_then(|v| v.as_str()), Some("Test"));
/// assert!(html.contains("<h1>Heading</h1>"));
/// ```
pub fn compile_page(
    input: &str,
) -> Result<(HashMap<String, serde_json::Value>, String)> {
    let (frontmatter, body) = parse_frontmatter(input);
    let html = compile_markdown(&body);
    Ok((frontmatter, html))
}

/// Generate a search index entry from HTML content.
///
/// # Examples
///
/// ```
/// let entry = ssg_core::SearchEntry {
///     title: "Hi".to_string(),
///     url: "/".to_string(),
///     content: "hello".to_string(),
/// };
/// let json = serde_json::to_string(&entry).unwrap();
/// assert!(json.contains("\"title\":\"Hi\""));
/// ```
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SearchEntry {
    /// Page title.
    pub title: String,
    /// Page URL.
    pub url: String,
    /// Plain text content for search matching.
    pub content: String,
}

/// Strip HTML tags from a string (simple implementation).
///
/// # Examples
///
/// ```
/// let plain = ssg_core::strip_html_tags("<p>Hello <b>world</b></p>");
/// assert_eq!(plain, "Hello world");
/// ```
#[must_use]
pub fn strip_html_tags(html: &str) -> String {
    let mut result = String::with_capacity(html.len());
    let mut in_tag = false;

    for c in html.chars() {
        match c {
            '<' => in_tag = true,
            '>' => in_tag = false,
            _ if !in_tag => result.push(c),
            _ => {}
        }
    }

    result
}

/// Build a search index entry from HTML content and metadata.
///
/// # Examples
///
/// ```
/// let entry = ssg_core::build_search_entry(
///     "Welcome",
///     "/index.html",
///     "<p>Hello <b>world</b></p>",
/// );
/// assert_eq!(entry.title, "Welcome");
/// assert_eq!(entry.url, "/index.html");
/// assert_eq!(entry.content, "Hello world");
/// ```
#[must_use]
pub fn build_search_entry(title: &str, url: &str, html: &str) -> SearchEntry {
    let content = strip_html_tags(html);
    // Collapse whitespace for compact index
    let content: String =
        content.split_whitespace().collect::<Vec<_>>().join(" ");
    SearchEntry {
        title: title.to_string(),
        url: url.to_string(),
        content,
    }
}

/// Estimates reading time in minutes from text content.
///
/// Uses 200 words-per-minute average, with a minimum of 1 minute.
///
/// # Examples
///
/// ```
/// assert_eq!(ssg_core::reading_time("a short article"), 1);
/// let long = "word ".repeat(600);
/// assert_eq!(ssg_core::reading_time(&long), 3);
/// ```
#[must_use]
pub fn reading_time(text: &str) -> usize {
    (text.split_whitespace().count() / 200).max(1)
}

/// Separators recognised when splitting a front-matter term list.
///
/// ASCII `,` plus the comma each writing system actually uses. A locale
/// post written in Arabic separates its tags with `،` (U+060C) and one in
/// Japanese with `、` (U+3001); splitting on ASCII alone collapses the whole
/// list into a single term, which then slugifies into one enormous path
/// component. Recognising the others costs nothing for ASCII input and makes
/// a multilingual corpus behave the way its authors wrote it.
const TERM_SEPARATORS: [char; 5] = [
    ',',        // ASCII
    '\u{060C}', // ، Arabic comma
    '\u{FF0C}', // , fullwidth comma (CJK)
    '\u{3001}', // 、 ideographic comma (CJK enumeration)
    ';',        // occasionally used in hand-authored lists
];

/// Splits a front-matter term list into trimmed, non-empty terms.
///
/// Accepts a comma or semicolon, plus the Arabic comma (`،`), fullwidth
/// comma (`,`) and ideographic comma (`、`), so a tag list keeps its terms
/// whatever script it was written in.
///
/// # Examples
///
/// ```
/// assert_eq!(ssg_core::split_terms("a, b,c"), vec!["a", "b", "c"]);
/// // Arabic comma — one list of three, not one term.
/// assert_eq!(ssg_core::split_terms("أ، ب، ج").len(), 3);
/// assert!(ssg_core::split_terms(" , ,").is_empty());
/// ```
#[must_use]
pub fn split_terms(input: &str) -> Vec<String> {
    input
        .split(TERM_SEPARATORS)
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(ToOwned::to_owned)
        .collect()
}

/// Maximum slug length in **bytes**.
///
/// Slugs become path components, and the common Linux filesystems (ext4,
/// btrfs, xfs) cap a single component at 255 *bytes*. `is_alphanumeric` is
/// Unicode-aware, so non-Latin scripts survive slugification at 2-4 bytes per
/// character: a 230-character Arabic term is 348 bytes and the build dies with
/// ENAMETOOLONG. macOS (APFS) counts *characters*, so the same input succeeds
/// there — which is how this reaches CI without any contributor seeing it.
///
/// 200 leaves headroom for the extensions and suffixes callers append.
const MAX_SLUG_BYTES: usize = 200;

/// Converts a string to a URL-safe slug.
///
/// Lowercases ASCII letters, replaces non-alphanumeric runs with a
/// single `-`, and trims leading/trailing separators. The result is
/// truncated to 200 bytes on a character boundary, so a slug is always a
/// legal path component on Linux filesystems.
///
/// # Examples
///
/// ```
/// assert_eq!(ssg_core::slugify("Hello World!"), "hello-world");
/// assert_eq!(ssg_core::slugify("Rust & Web"), "rust-web");
/// assert_eq!(ssg_core::slugify("--leading--"), "leading");
/// // Long terms are capped in bytes, not characters.
/// assert!(ssg_core::slugify(&"ا".repeat(400)).len() <= 200);
/// ```
#[must_use]
pub fn slugify(input: &str) -> String {
    let slug = input
        .to_lowercase()
        .chars()
        .map(|c| if c.is_alphanumeric() { c } else { '-' })
        .collect::<String>()
        .split('-')
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("-");

    if slug.len() <= MAX_SLUG_BYTES {
        return slug;
    }

    // Truncate on a char boundary — byte-slicing a multi-byte sequence
    // panics — then trim any separator the cut leaves dangling.
    let mut end = MAX_SLUG_BYTES;
    while end > 0 && !slug.is_char_boundary(end) {
        end -= 1;
    }
    slug[..end].trim_end_matches('-').to_owned()
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn slugify_caps_length_in_bytes_not_characters() {
        // The regression: a real Arabic tag list collapsed into one term is
        // 230 characters but 348 UTF-8 bytes. ext4 caps a path component at
        // 255 bytes, so the build died with ENAMETOOLONG; APFS counts
        // characters, so macOS never saw it.
        let arabic = "\u{0622}\u{0641}\u{0627}\u{0642} ".repeat(60);
        let slug = slugify(&arabic);
        assert!(
            slug.len() <= MAX_SLUG_BYTES,
            "slug is {} bytes, over the {MAX_SLUG_BYTES}-byte cap",
            slug.len()
        );
        // Still a usable slug, not an empty string.
        assert!(!slug.is_empty());
        assert!(!slug.ends_with('-'), "cut left a dangling separator");
    }

    #[test]
    fn slugify_truncates_on_a_char_boundary() {
        // Byte-slicing a multi-byte sequence panics; the cut must land on a
        // boundary for every offset a long multi-byte input can produce.
        for n in 90..140 {
            let slug = slugify(&"\u{3042}".repeat(n)); // hiragana A, 3 bytes
            assert!(slug.len() <= MAX_SLUG_BYTES);
            assert!(std::str::from_utf8(slug.as_bytes()).is_ok());
        }
    }

    #[test]
    fn slugify_leaves_short_slugs_untouched() {
        assert_eq!(slugify("Hello World!"), "hello-world");
        assert_eq!(slugify("Rust & Web"), "rust-web");
    }

    #[test]
    fn split_terms_handles_non_ascii_separators() {
        // Each script's own comma. Splitting on ASCII alone yields one term.
        assert_eq!(split_terms("a, b, c").len(), 3);
        assert_eq!(
            split_terms("\u{0623}\u{060C} \u{0628}\u{060C} \u{062C}").len(),
            3
        );
        assert_eq!(
            split_terms("\u{3042}\u{3001}\u{3044}\u{3001}\u{3046}").len(),
            3
        );
        assert_eq!(split_terms("\u{7532}\u{FF0C}\u{4E59}").len(), 2);
        assert_eq!(split_terms("a; b").len(), 2);
    }

    #[test]
    fn split_terms_trims_and_drops_empties() {
        assert_eq!(split_terms("  a  ,,  b  ,"), vec!["a", "b"]);
        assert!(split_terms(" , , ").is_empty());
        assert!(split_terms("").is_empty());
    }

    #[test]
    fn split_terms_then_slugify_stays_within_the_byte_cap() {
        // The two fixes together: the real corpus shape. Each term is short,
        // so nothing is truncated and no path component can overflow.
        let list = "\u{0623}\u{0644}\u{0623}\u{0639}\u{0645}\u{0627}\u{0644}\u{060C} \u{0627}\u{0644}\u{062A}\u{062C}\u{0627}\u{0631}\u{0629}\u{060C} DORA";
        let slugs: Vec<String> =
            split_terms(list).iter().map(|t| slugify(t)).collect();
        assert_eq!(slugs.len(), 3);
        for s in &slugs {
            assert!(s.len() <= MAX_SLUG_BYTES);
            assert!(!s.is_empty());
        }
    }

    #[test]
    fn compile_markdown_basic() {
        let html = compile_markdown("# Hello\n\nParagraph.");
        assert!(html.contains("<h1>Hello</h1>"));
        assert!(html.contains("<p>Paragraph.</p>"));
    }

    #[test]
    fn compile_markdown_gfm_tables() {
        let input = "| A | B |\n|---|---|\n| 1 | 2 |";
        let html = compile_markdown(input);
        assert!(html.contains("<table>"));
    }

    #[test]
    fn compile_markdown_strikethrough() {
        let html = compile_markdown("~~deleted~~");
        assert!(html.contains("<del>deleted</del>"));
    }

    #[test]
    fn parse_frontmatter_yaml() {
        let (fm, body) = parse_frontmatter(
            "---\ntitle: Hello\ndate: 2026-01-01\n---\n# Body",
        );
        assert_eq!(fm.get("title").and_then(|v| v.as_str()), Some("Hello"));
        assert!(body.contains("# Body"));
    }

    #[test]
    fn parse_frontmatter_toml() {
        let (fm, body) =
            parse_frontmatter("+++\ntitle = \"Hello\"\n+++\n# Body");
        assert_eq!(fm.get("title").and_then(|v| v.as_str()), Some("Hello"));
        assert!(body.contains("# Body"));
    }

    #[test]
    fn parse_frontmatter_json() {
        let (fm, body) = parse_frontmatter("{\"title\": \"Hello\"}\n# Body");
        assert_eq!(fm.get("title").and_then(|v| v.as_str()), Some("Hello"));
        assert!(body.contains("# Body"));
    }

    #[test]
    fn parse_frontmatter_none() {
        let (fm, body) = parse_frontmatter("Just content");
        assert!(fm.is_empty());
        assert_eq!(body, "Just content");
    }

    #[test]
    fn compile_page_full() {
        let input = "---\ntitle: Test\n---\n# Hello\n\nWorld";
        let (fm, html) = compile_page(input).unwrap();
        assert_eq!(fm.get("title").and_then(|v| v.as_str()), Some("Test"));
        assert!(html.contains("<h1>Hello</h1>"));
    }

    #[test]
    fn strip_html_tags_basic() {
        assert_eq!(strip_html_tags("<p>Hello <b>world</b></p>"), "Hello world");
    }

    #[test]
    fn strip_html_tags_empty() {
        assert_eq!(strip_html_tags(""), "");
    }

    #[test]
    fn build_search_entry_strips_tags() {
        let entry =
            build_search_entry("Title", "/page", "<p>Hello <b>world</b></p>");
        assert_eq!(entry.title, "Title");
        assert_eq!(entry.content, "Hello world");
    }

    #[test]
    fn reading_time_short() {
        assert_eq!(reading_time("one two three"), 1);
    }

    #[test]
    fn reading_time_long() {
        let text = "word ".repeat(600);
        assert_eq!(reading_time(&text), 3);
    }

    #[test]
    fn slugify_basic() {
        assert_eq!(slugify("Hello World!"), "hello-world");
        assert_eq!(slugify("Rust & Web"), "rust-web");
    }

    #[test]
    fn error_display_frontmatter_parse_variant() {
        let e = Error::FrontmatterParse {
            syntax: "yaml mismatch".to_string(),
        };
        let s = format!("{e}");
        assert!(s.contains("Frontmatter parse error"));
        assert!(s.contains("yaml mismatch"));
    }

    #[test]
    fn error_display_markdown_compile_variant() {
        let e = Error::MarkdownCompile {
            source: "broken markdown".to_string(),
        };
        let s = format!("{e}");
        assert!(s.contains("Markdown compilation error"));
        assert!(s.contains("broken markdown"));
    }

    #[test]
    fn error_display_invalid_slug_variant() {
        let e = Error::InvalidSlug {
            input: "@@@".to_string(),
        };
        let s = format!("{e}");
        assert!(s.contains("Invalid slug input"));
        assert!(s.contains("@@@"));
    }

    #[test]
    fn error_is_std_error_trait_object() {
        // Smoke-tests the `impl std::error::Error for Error {}` block.
        let e: Box<dyn std::error::Error> = Box::new(Error::InvalidSlug {
            input: "x".to_string(),
        });
        assert!(!e.to_string().is_empty());
        // No source by default.
        assert!(std::error::Error::source(&*e).is_none());
    }

    #[test]
    fn error_debug_impl_executes_for_each_variant() {
        let e1 = Error::FrontmatterParse {
            syntax: "a".to_string(),
        };
        let e2 = Error::MarkdownCompile {
            source: "b".to_string(),
        };
        let e3 = Error::InvalidSlug {
            input: "c".to_string(),
        };
        for e in [&e1, &e2, &e3] {
            let s = format!("{e:?}");
            assert!(!s.is_empty());
        }
    }

    #[test]
    fn search_entry_serialization_roundtrip() {
        let e = SearchEntry {
            title: "T".to_string(),
            url: "/u".to_string(),
            content: "C".to_string(),
        };
        let json = serde_json::to_string(&e).unwrap();
        assert!(json.contains("\"title\":\"T\""));
        let back: SearchEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(back.url, "/u");
        assert_eq!(back.content, "C");
        // Debug + Clone are derived; exercise them.
        let _ = format!("{back:?}");
        let _ = back.clone();
    }

    #[test]
    fn compile_page_yields_empty_frontmatter_when_absent() {
        let (fm, html) = compile_page("# Heading\n\nBody").unwrap();
        assert!(fm.is_empty());
        assert!(html.contains("<h1>Heading</h1>"));
    }

    #[test]
    fn slugify_collapses_consecutive_separators() {
        assert_eq!(slugify("foo!!!bar"), "foo-bar");
        assert_eq!(slugify("--leading--"), "leading");
    }

    #[test]
    fn slugify_empty_input_yields_empty() {
        assert_eq!(slugify(""), "");
        assert_eq!(slugify("???"), "");
    }
}