karaty_template/
lib.rs

1use dioxus::prelude::*;
2use karaty_blueprint::{TemplateDataType, TemplateProps, Templates};
3
4mod blog;
5mod docs;
6
7const AVAILABLE_STYLE_SETTINGS: [&str; 26] = [
8    "headings",
9    "lead",
10    "h1",
11    "h2",
12    "h3",
13    "h4",
14    "p",
15    "a",
16    "blockquote",
17    "figure",
18    "figcaption",
19    "strong",
20    "em",
21    "code",
22    "pre",
23    "ol",
24    "ul",
25    "li",
26    "table",
27    "thead",
28    "tr",
29    "th",
30    "td",
31    "img",
32    "video",
33    "hr",
34];
35
36pub fn generate_prose_class(config: toml::map::Map<String, toml::Value>) -> String {
37    let mut res = String::from("prose prose-sm sm:prose-base dark:prose-invert");
38    for i in AVAILABLE_STYLE_SETTINGS {
39        if let Some(toml::Value::String(v)) = config.get(i) {
40            let list = v.split(" ").collect::<Vec<&str>>();
41            if !list.is_empty() {
42                res.push_str(&format!(" prose-{i}:{}", list.first().unwrap()))
43            } else {
44                res.push_str(&format!("{} ", list.join(&format!(" prose-{i}:"))));
45            }
46        }
47    }
48    res
49}
50
51#[allow(non_snake_case)]
52pub fn centered_display(cx: Scope<TemplateProps>) -> Element {
53    let config = &cx.props.config;
54
55    let Navbar = cx.props.utility.navbar;
56    let Footer = cx.props.utility.footer;
57    let Markdown = *cx.props.utility.renderers.get("markdown").unwrap();
58
59    let content = cx.props.data.text();
60
61    let metadata = markdown_meta_parser::MetaData::new(&content);
62    let content = if let Ok(v) = metadata.parse() {
63        v.1
64    } else {
65        content
66    };
67
68    let class = if let Some(toml::Value::Table(t)) = config.get("style") {
69        generate_prose_class(t.clone())
70    } else {
71        "prose prose-sm sm:prose-base dark:prose-invert".to_string()
72    };
73
74    let hide_navbar = if let Some(toml::Value::Boolean(b)) = config.get("hide-navbar") {
75        *b
76    } else {
77        false
78    };
79
80    let hide_footer = if let Some(toml::Value::Boolean(b)) = config.get("hide-footer") {
81        *b
82    } else {
83        false
84    };
85
86    cx.render(rsx! {
87        section { class: "bg-cover bg-white dark:bg-gray-900",
88            if !hide_navbar {
89                rsx! { Navbar {} }
90            }
91            div { class: "flex w-full items-center justify-center container mx-auto px-8",
92                div { class: "text-center",
93                    div { class: "{class}", Markdown { content: content, config: Default::default() } }
94                    if !hide_footer {
95                        rsx! { Footer {} }
96                    }
97                }
98            }
99        }
100    })
101}
102
103pub fn export() -> Templates {
104    let mut list = Templates::new();
105
106    list.template("center", vec![TemplateDataType::Markdown], centered_display);
107
108    list.template(
109        "docs",
110        vec![TemplateDataType::DirectoryData],
111        docs::DocsPreset,
112    );
113    list.sub_module("blog", blog::export());
114
115    list
116}