diff --git a/my-blog/config.toml b/my-blog/config.toml index a41461f..eb52713 100644 --- a/my-blog/config.toml +++ b/my-blog/config.toml @@ -7,6 +7,7 @@ author = "syui" [build] highlight_code = true +highlight_theme = "Monokai" minify = false [ai] diff --git a/src/config.rs b/src/config.rs index 6511fa1..54ca603 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,7 @@ pub struct SiteConfig { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct BuildConfig { pub highlight_code: bool, + pub highlight_theme: Option, pub minify: bool, } @@ -146,6 +147,7 @@ impl Default for Config { }, build: BuildConfig { highlight_code: true, + highlight_theme: Some("Monokai".to_string()), minify: false, }, ai: Some(AiConfig { diff --git a/src/generator.rs b/src/generator.rs index 6b15118..1d5e5b1 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -18,7 +18,7 @@ pub struct Generator { impl Generator { pub fn new(base_path: PathBuf, config: Config) -> Result { - let markdown_processor = MarkdownProcessor::new(config.build.highlight_code); + let markdown_processor = MarkdownProcessor::new(config.build.highlight_code, config.build.highlight_theme.clone()); let template_engine = TemplateEngine::new(base_path.join("templates"))?; let ai_manager = if let Some(ref ai_config) = config.ai { diff --git a/src/markdown.rs b/src/markdown.rs index 62370cd..b030a7c 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -9,14 +9,16 @@ use serde_json::Value; pub struct MarkdownProcessor { highlight_code: bool, + highlight_theme: String, syntax_set: SyntaxSet, theme_set: ThemeSet, } impl MarkdownProcessor { - pub fn new(highlight_code: bool) -> Self { + pub fn new(highlight_code: bool, highlight_theme: Option) -> Self { Self { highlight_code, + highlight_theme: highlight_theme.unwrap_or_else(|| "Monokai".to_string()), syntax_set: SyntaxSet::load_defaults_newlines(), theme_set: ThemeSet::load_defaults(), } @@ -86,7 +88,11 @@ impl MarkdownProcessor { let parser = Parser::new_ext(content, options); let mut html_output = String::new(); let mut code_block = None; - let theme = &self.theme_set.themes["base16-ocean.dark"]; + let theme = self.theme_set.themes.get(&self.highlight_theme) + .or_else(|| self.theme_set.themes.get("Monokai")) + .or_else(|| self.theme_set.themes.get("InspiredGitHub")) + .or_else(|| self.theme_set.themes.get("base16-ocean.dark")) + .unwrap_or_else(|| self.theme_set.themes.values().next().unwrap()); let mut events = Vec::new(); for event in parser {