Skip to main content

dioxus_docs_kit/blog/
config.rs

1//! Builder for constructing a `BlogRegistry`.
2
3use crate::blog::registry::BlogRegistry;
4use crate::config::ThemeConfig;
5use std::collections::HashMap;
6
7/// Builder for constructing a [`BlogRegistry`].
8///
9/// # Example
10///
11/// ```rust,ignore
12/// let registry = BlogConfig::new(include_str!("../blog/_blog.json"), blog_content_map())
13///     .with_posts_per_page(9)
14///     .with_theme_toggle("light", "dark", "dark")
15///     .build();
16/// ```
17pub struct BlogConfig {
18    manifest_json: String,
19    content_map: HashMap<&'static str, &'static str>,
20    posts_per_page: usize,
21    date_format: String,
22    theme: Option<ThemeConfig>,
23}
24
25impl BlogConfig {
26    /// Create a new builder from a `_blog.json` string and a content map.
27    pub fn new(manifest_json: &str, content_map: HashMap<&'static str, &'static str>) -> Self {
28        Self {
29            manifest_json: manifest_json.to_string(),
30            content_map,
31            posts_per_page: 9,
32            date_format: "%B %d, %Y".to_string(),
33            theme: None,
34        }
35    }
36
37    /// Set the number of posts per page for pagination (default: 9).
38    pub fn with_posts_per_page(mut self, n: usize) -> Self {
39        self.posts_per_page = n;
40        self
41    }
42
43    /// Set the date display format (default: "%B %d, %Y").
44    pub fn with_date_format(mut self, fmt: &str) -> Self {
45        self.date_format = fmt.to_string();
46        self
47    }
48
49    /// Set a single theme (no toggle button).
50    pub fn with_theme(mut self, theme: &str) -> Self {
51        self.theme = Some(ThemeConfig {
52            default_theme: theme.to_string(),
53            toggle_themes: None,
54            storage_key: "docs-theme".to_string(),
55        });
56        self
57    }
58
59    /// Enable a light/dark theme toggle.
60    pub fn with_theme_toggle(mut self, light: &str, dark: &str, default: &str) -> Self {
61        self.theme = Some(ThemeConfig {
62            default_theme: default.to_string(),
63            toggle_themes: Some((light.to_string(), dark.to_string())),
64            storage_key: "docs-theme".to_string(),
65        });
66        self
67    }
68
69    /// Build the [`BlogRegistry`].
70    pub fn build(self) -> BlogRegistry {
71        BlogRegistry::from_config(self)
72    }
73
74    pub(crate) fn manifest_json(&self) -> &str {
75        &self.manifest_json
76    }
77
78    pub(crate) fn content_map(&self) -> &HashMap<&'static str, &'static str> {
79        &self.content_map
80    }
81
82    pub(crate) fn posts_per_page(&self) -> usize {
83        self.posts_per_page
84    }
85
86    pub(crate) fn date_format(&self) -> &str {
87        &self.date_format
88    }
89
90    pub(crate) fn theme_config(&self) -> Option<&ThemeConfig> {
91        self.theme.as_ref()
92    }
93}