dioxus-docs-kit 0.4.1

Reusable documentation site shell for Dioxus applications
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
//! Blog content registry.

use crate::blog::config::BlogConfig;
use crate::blog::types::{
    Author, BlogManifest, BlogPost, BlogSearchEntry, calculate_reading_time,
    extract_blog_frontmatter,
};
use crate::config::ThemeConfig;
use dioxus_mdx::{get_raw_markdown, parse_mdx};
use std::collections::HashMap;

/// Central blog registry holding all parsed content.
///
/// Created via [`BlogConfig`] builder and typically stored in a `LazyLock<BlogRegistry>` static.
pub struct BlogRegistry {
    /// All parsed blog posts, sorted by date (newest first).
    posts: Vec<BlogPost>,
    /// Author definitions from `_blog.json`.
    authors: HashMap<String, Author>,
    /// All unique tags across all posts, sorted alphabetically.
    all_tags: Vec<String>,
    /// Prebuilt search index.
    search_index: Vec<BlogSearchEntry>,
    /// Indices into `posts` for featured posts, preserving date order.
    featured_indices: Vec<usize>,
    /// Posts per page for pagination.
    pub posts_per_page: usize,
    /// Date display format string.
    pub date_format: String,
    /// Optional theme configuration.
    pub theme: Option<ThemeConfig>,
}

impl BlogRegistry {
    pub(crate) fn from_config(config: BlogConfig) -> Self {
        let manifest: BlogManifest =
            serde_json::from_str(config.manifest_json()).expect("Failed to parse _blog.json");

        let mut posts: Vec<BlogPost> = config
            .content_map()
            .iter()
            .filter(|(key, _)| **key != "__manifest__")
            .filter_map(|(&slug, &content)| {
                let (frontmatter, remaining) = extract_blog_frontmatter(content)?;

                if frontmatter.draft {
                    return None;
                }

                let nodes = parse_mdx(remaining);
                let raw_markdown = get_raw_markdown(&nodes);
                let reading_time_minutes = calculate_reading_time(&raw_markdown);

                Some(BlogPost {
                    slug: slug.to_string(),
                    frontmatter,
                    content: nodes,
                    raw_markdown,
                    reading_time_minutes,
                })
            })
            .collect();

        posts.sort_by(|a, b| b.frontmatter.date.cmp(&a.frontmatter.date));

        let mut tag_set: Vec<String> = posts
            .iter()
            .flat_map(|p| p.frontmatter.tags.iter().cloned())
            .collect();
        tag_set.sort();
        tag_set.dedup();

        let featured_indices: Vec<usize> = posts
            .iter()
            .enumerate()
            .filter(|(_, p)| p.frontmatter.featured)
            .map(|(i, _)| i)
            .collect();

        let search_index = Self::build_search_index(&posts);

        let posts_per_page = config.posts_per_page();
        let date_format = config.date_format().to_string();
        let theme = config.theme_config().cloned();

        Self {
            posts,
            authors: manifest.authors,
            all_tags: tag_set,
            featured_indices,
            search_index,
            posts_per_page,
            date_format,
            theme,
        }
    }

    // ── Post access ──────────────────────────────────────────────────────

    pub fn get_post(&self, slug: &str) -> Option<&BlogPost> {
        self.posts.iter().find(|p| p.slug == slug)
    }

    pub fn all_posts(&self) -> &[BlogPost] {
        &self.posts
    }

    /// Get all featured/pinned posts, sorted by date (newest first).
    pub fn featured_posts(&self) -> Vec<&BlogPost> {
        self.featured_indices
            .iter()
            .map(|&i| &self.posts[i])
            .collect()
    }

    /// Check if there are any featured posts.
    pub fn has_featured(&self) -> bool {
        !self.featured_indices.is_empty()
    }

    pub fn posts_by_tag(&self, tag: &str) -> Vec<&BlogPost> {
        self.posts
            .iter()
            .filter(|p| p.frontmatter.tags.iter().any(|t| t == tag))
            .collect()
    }

    /// Get a page of non-featured posts for the main blog index.
    pub fn non_featured_posts_page(&self, page: usize) -> Vec<&BlogPost> {
        let filtered: Vec<&BlogPost> = self
            .posts
            .iter()
            .filter(|p| !p.frontmatter.featured)
            .collect();
        let start = page * self.posts_per_page;
        let end = (start + self.posts_per_page).min(filtered.len());
        if start >= filtered.len() {
            return Vec::new();
        }
        filtered[start..end].to_vec()
    }

    /// Total number of pages for the main blog index, excluding featured posts.
    pub fn non_featured_total_pages(&self) -> usize {
        let count = self
            .posts
            .iter()
            .filter(|p| !p.frontmatter.featured)
            .count();
        if count == 0 {
            return 1;
        }
        count.div_ceil(self.posts_per_page)
    }

    /// Find posts related to the given slug by tag overlap.
    ///
    /// Returns up to `max` posts sorted by number of overlapping tags (descending),
    /// then by date (newest first). Excludes the current post.
    pub fn related_posts(&self, slug: &str, max: usize) -> Vec<&BlogPost> {
        let current = match self.get_post(slug) {
            Some(p) => p,
            None => return Vec::new(),
        };
        let current_tags: std::collections::HashSet<&str> = current
            .frontmatter
            .tags
            .iter()
            .map(|t| t.as_str())
            .collect();

        if current_tags.is_empty() {
            return Vec::new();
        }

        let mut scored: Vec<(usize, &BlogPost)> = self
            .posts
            .iter()
            .filter(|p| p.slug != slug)
            .filter_map(|p| {
                let overlap = p
                    .frontmatter
                    .tags
                    .iter()
                    .filter(|t| current_tags.contains(t.as_str()))
                    .count();
                if overlap > 0 {
                    Some((overlap, p))
                } else {
                    None
                }
            })
            .collect();

        scored.sort_by(|a, b| {
            b.0.cmp(&a.0)
                .then_with(|| b.1.frontmatter.date.cmp(&a.1.frontmatter.date))
        });
        scored.into_iter().take(max).map(|(_, p)| p).collect()
    }

    pub fn posts_page(&self, page: usize) -> &[BlogPost] {
        let start = page * self.posts_per_page;
        let end = (start + self.posts_per_page).min(self.posts.len());
        if start >= self.posts.len() {
            return &[];
        }
        &self.posts[start..end]
    }

    pub fn posts_page_by_tag(&self, tag: &str, page: usize) -> Vec<&BlogPost> {
        let filtered = self.posts_by_tag(tag);
        let start = page * self.posts_per_page;
        let end = (start + self.posts_per_page).min(filtered.len());
        if start >= filtered.len() {
            return Vec::new();
        }
        filtered[start..end].to_vec()
    }

    pub fn total_pages(&self) -> usize {
        if self.posts.is_empty() {
            return 1;
        }
        self.posts.len().div_ceil(self.posts_per_page)
    }

    pub fn total_pages_for_tag(&self, tag: &str) -> usize {
        let count = self.posts_by_tag(tag).len();
        if count == 0 {
            return 1;
        }
        count.div_ceil(self.posts_per_page)
    }

    // ── Navigation ───────────────────────────────────────────────────────

    /// Get the previous post (older) relative to the given slug.
    pub fn prev_post(&self, slug: &str) -> Option<&BlogPost> {
        let idx = self.posts.iter().position(|p| p.slug == slug)?;
        if idx + 1 < self.posts.len() {
            Some(&self.posts[idx + 1])
        } else {
            None
        }
    }

    /// Get the next post (newer) relative to the given slug.
    pub fn next_post(&self, slug: &str) -> Option<&BlogPost> {
        let idx = self.posts.iter().position(|p| p.slug == slug)?;
        if idx > 0 {
            Some(&self.posts[idx - 1])
        } else {
            None
        }
    }

    // ── Metadata ─────────────────────────────────────────────────────────

    pub fn all_tags(&self) -> &[String] {
        &self.all_tags
    }

    pub fn tag_count(&self, tag: &str) -> usize {
        self.posts
            .iter()
            .filter(|p| p.frontmatter.tags.iter().any(|t| t == tag))
            .count()
    }

    pub fn get_author(&self, id: &str) -> Option<&Author> {
        self.authors.get(id)
    }

    // ── Search ───────────────────────────────────────────────────────────

    pub fn search_posts(&self, query: &str) -> Vec<&BlogSearchEntry> {
        let query = query.trim();
        if query.is_empty() {
            return Vec::new();
        }
        let q = query.to_lowercase();

        let mut title_matches: Vec<&BlogSearchEntry> = Vec::new();
        let mut desc_matches: Vec<&BlogSearchEntry> = Vec::new();
        let mut content_matches: Vec<&BlogSearchEntry> = Vec::new();

        for entry in &self.search_index {
            if entry.title.to_lowercase().contains(&q) {
                title_matches.push(entry);
            } else if entry.description.to_lowercase().contains(&q) {
                desc_matches.push(entry);
            } else if entry.content_preview.to_lowercase().contains(&q) {
                content_matches.push(entry);
            }
        }

        title_matches.extend(desc_matches);
        title_matches.extend(content_matches);
        title_matches
    }

    fn build_search_index(posts: &[BlogPost]) -> Vec<BlogSearchEntry> {
        posts
            .iter()
            .map(|post| {
                let preview: String = post.raw_markdown.chars().take(200).collect();
                BlogSearchEntry {
                    slug: post.slug.clone(),
                    title: post.frontmatter.title.clone(),
                    description: post.frontmatter.description.clone().unwrap_or_default(),
                    content_preview: preview,
                    date: post.frontmatter.date.clone(),
                    tags: post.frontmatter.tags.clone(),
                }
            })
            .collect()
    }

    // ── RSS ──────────────────────────────────────────────────────────────

    pub fn generate_rss(&self, site_title: &str, site_url: &str, blog_path: &str) -> String {
        let mut rss = format!(
            r#"<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{site_title}</title>
<link>{site_url}{blog_path}</link>
<description>{site_title} RSS Feed</description>
<atom:link href="{site_url}{blog_path}/rss.xml" rel="self" type="application/rss+xml"/>
"#
        );

        for post in &self.posts {
            let title = &post.frontmatter.title;
            let desc = post.frontmatter.description.as_deref().unwrap_or_default();
            let link = format!("{site_url}{blog_path}/{}", post.slug);
            rss.push_str(&format!(
                "<item>\n<title>{title}</title>\n<link>{link}</link>\n<description>{desc}</description>\n<pubDate>{}</pubDate>\n<guid>{link}</guid>\n</item>\n",
                post.frontmatter.date
            ));
        }

        rss.push_str("</channel>\n</rss>\n");
        rss
    }

    pub fn generate_llms_txt(
        &self,
        site_title: &str,
        site_description: &str,
        base_url: &str,
        blog_path: &str,
    ) -> String {
        let mut out = format!("# {site_title}\n\n> {site_description}\n\n");

        for post in &self.posts {
            let title = &post.frontmatter.title;
            let desc = post.frontmatter.description.as_deref().unwrap_or_default();
            let url = format!("{base_url}{blog_path}/{}", post.slug);
            if desc.is_empty() {
                out.push_str(&format!("- [{title}]({url})\n"));
            } else {
                out.push_str(&format!("- [{title}]({url}): {desc}\n"));
            }
        }

        out
    }

    // ── Sitemap ──────────────────────────────────────────────────────────

    /// Generate a sitemap.xml for all blog posts.
    pub fn generate_sitemap(&self, site_url: &str, blog_path: &str) -> String {
        let mut xml = String::from(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
             <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n",
        );

        // Blog index page
        xml.push_str(&format!(
            "<url>\n<loc>{site_url}{blog_path}</loc>\n<changefreq>weekly</changefreq>\n<priority>0.8</priority>\n</url>\n"
        ));

        // Individual posts
        for post in &self.posts {
            let loc = format!("{site_url}{blog_path}/{}", post.slug);
            let lastmod = &post.frontmatter.date;
            xml.push_str(&format!(
                "<url>\n<loc>{loc}</loc>\n<lastmod>{lastmod}</lastmod>\n<changefreq>monthly</changefreq>\n<priority>0.6</priority>\n</url>\n"
            ));
        }

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

    // ── Date formatting ──────────────────────────────────────────────────

    pub fn format_date(&self, date: &str) -> String {
        format_date_with(date, &self.date_format)
    }
}

/// Format an ISO 8601 date string (YYYY-MM-DD) with a simple format pattern.
pub fn format_date_with(date: &str, fmt: &str) -> String {
    let parts: Vec<&str> = date.split('-').collect();
    if parts.len() != 3 {
        return date.to_string();
    }

    let year = parts[0];
    let month = parts[1];
    let day = parts[2];

    let month_name = match month {
        "01" => "January",
        "02" => "February",
        "03" => "March",
        "04" => "April",
        "05" => "May",
        "06" => "June",
        "07" => "July",
        "08" => "August",
        "09" => "September",
        "10" => "October",
        "11" => "November",
        "12" => "December",
        _ => month,
    };

    fmt.replace("%Y", year)
        .replace("%m", month)
        .replace("%d", day)
        .replace("%B", month_name)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::blog::config::BlogConfig;
    use std::collections::HashMap;

    fn build_registry(posts_per_page: usize) -> BlogRegistry {
        let manifest = r#"{
            "authors": {
                "author": { "name": "Author" }
            },
            "posts": ["featured", "regular-1", "regular-2", "regular-3", "rust-new", "rust-old", "misc"]
        }"#;

        let mut content_map = HashMap::new();
        content_map.insert(
            "featured",
            r#"---
title: "Featured"
date: "2026-03-21"
author: "author"
tags: ["announcement"]
featured: true
---
Featured post
"#,
        );
        content_map.insert(
            "regular-1",
            r#"---
title: "Regular 1"
date: "2026-03-20"
author: "author"
tags: ["announcement"]
---
Regular one
"#,
        );
        content_map.insert(
            "regular-2",
            r#"---
title: "Regular 2"
date: "2026-03-19"
author: "author"
tags: ["announcement"]
---
Regular two
"#,
        );
        content_map.insert(
            "regular-3",
            r#"---
title: "Regular 3"
date: "2026-03-18"
author: "author"
tags: ["announcement"]
---
Regular three
"#,
        );
        content_map.insert(
            "rust-new",
            r#"---
title: "Rust New"
date: "2026-03-17"
author: "author"
tags: ["rust", "web", "async"]
---
Rust new
"#,
        );
        content_map.insert(
            "rust-old",
            r#"---
title: "Rust Old"
date: "2026-03-16"
author: "author"
tags: ["rust", "web"]
---
Rust old
"#,
        );
        content_map.insert(
            "misc",
            r#"---
title: "Misc"
date: "2026-03-15"
author: "author"
tags: ["rust"]
---
Misc
"#,
        );

        BlogConfig::new(manifest, content_map)
            .with_posts_per_page(posts_per_page)
            .build()
    }

    #[test]
    fn unfiltered_pagination_excludes_featured_posts() {
        let registry = build_registry(2);

        let page_1: Vec<_> = registry
            .non_featured_posts_page(0)
            .into_iter()
            .map(|post| post.slug.as_str())
            .collect();
        let page_2: Vec<_> = registry
            .non_featured_posts_page(1)
            .into_iter()
            .map(|post| post.slug.as_str())
            .collect();
        let page_3: Vec<_> = registry
            .non_featured_posts_page(2)
            .into_iter()
            .map(|post| post.slug.as_str())
            .collect();
        let page_4 = registry.non_featured_posts_page(3);

        assert_eq!(page_1, vec!["regular-1", "regular-2"]);
        assert_eq!(page_2, vec!["regular-3", "rust-new"]);
        assert_eq!(page_3, vec!["rust-old", "misc"]);
        assert!(page_4.is_empty());
        assert_eq!(registry.non_featured_total_pages(), 3);
    }

    #[test]
    fn tag_pagination_still_includes_featured_posts() {
        let registry = build_registry(2);

        let page: Vec<_> = registry
            .posts_page_by_tag("announcement", 0)
            .into_iter()
            .map(|post| post.slug.as_str())
            .collect();

        assert_eq!(page, vec!["featured", "regular-1"]);
        assert_eq!(registry.total_pages_for_tag("announcement"), 2);
    }

    #[test]
    fn related_posts_tie_break_on_date() {
        let registry = build_registry(10);

        let related: Vec<_> = registry
            .related_posts("misc", 3)
            .into_iter()
            .map(|post| post.slug.as_str())
            .collect();

        assert_eq!(related, vec!["rust-new", "rust-old"]);
    }
}