dioxus_docs_kit/blog/
config.rs1use crate::blog::registry::BlogRegistry;
4use crate::config::ThemeConfig;
5use std::collections::HashMap;
6
7pub 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 category_base_path: Option<String>,
24}
25
26impl BlogConfig {
27 pub fn new(manifest_json: &str, content_map: HashMap<&'static str, &'static str>) -> Self {
29 Self {
30 manifest_json: manifest_json.to_string(),
31 content_map,
32 posts_per_page: 9,
33 date_format: "%B %d, %Y".to_string(),
34 theme: None,
35 category_base_path: None,
36 }
37 }
38
39 pub fn with_posts_per_page(mut self, n: usize) -> Self {
41 self.posts_per_page = n;
42 self
43 }
44
45 pub fn with_category_base_path(mut self, path: &str) -> Self {
52 self.category_base_path = Some(path.trim_end_matches('/').to_string());
53 self
54 }
55
56 pub(crate) fn category_base_path(&self) -> Option<&str> {
57 self.category_base_path.as_deref()
58 }
59
60 pub fn with_date_format(mut self, fmt: &str) -> Self {
62 self.date_format = fmt.to_string();
63 self
64 }
65
66 pub fn with_theme(mut self, theme: &str) -> Self {
68 self.theme = Some(ThemeConfig {
69 default_theme: theme.to_string(),
70 toggle_themes: None,
71 storage_key: "docs-theme".to_string(),
72 });
73 self
74 }
75
76 pub fn with_theme_toggle(mut self, light: &str, dark: &str, default: &str) -> Self {
78 self.theme = Some(ThemeConfig {
79 default_theme: default.to_string(),
80 toggle_themes: Some((light.to_string(), dark.to_string())),
81 storage_key: "docs-theme".to_string(),
82 });
83 self
84 }
85
86 pub fn build(self) -> BlogRegistry {
93 self.try_build()
94 .unwrap_or_else(|e| panic!("dioxus-docs-kit: {e}"))
95 }
96
97 pub fn try_build(self) -> Result<BlogRegistry, crate::error::DocsKitError> {
100 BlogRegistry::try_from_config(self)
101 }
102
103 pub(crate) fn manifest_json(&self) -> &str {
104 &self.manifest_json
105 }
106
107 pub(crate) fn content_map(&self) -> &HashMap<&'static str, &'static str> {
108 &self.content_map
109 }
110
111 pub(crate) fn posts_per_page(&self) -> usize {
112 self.posts_per_page
113 }
114
115 pub(crate) fn date_format(&self) -> &str {
116 &self.date_format
117 }
118
119 pub(crate) fn theme_config(&self) -> Option<&ThemeConfig> {
120 self.theme.as_ref()
121 }
122}