diff --git a/guide/src/format/configuration/renderers.md b/guide/src/format/configuration/renderers.md index a827d2936f..0a37179567 100644 --- a/guide/src/format/configuration/renderers.md +++ b/guide/src/format/configuration/renderers.md @@ -100,6 +100,7 @@ preferred-dark-theme = "navy" smart-punctuation = true mathjax-support = false copy-fonts = true +additional-themes = [{ name = "Midnight Blue", class = "midnight" }] additional-css = ["custom.css", "custom2.css"] additional-js = ["custom.js"] no-section-label = false @@ -134,6 +135,11 @@ The following configuration options are available: create a `theme/fonts/fonts.css` file and store the fonts in the `theme/fonts/` directory. - **google-analytics:** This field has been deprecated and will be removed in a future release. Use the `theme/head.hbs` file to add the appropriate Google Analytics code instead. +- **additional-themes:** If you supply user-defined themes in `additional-css`, include them + here so they are listed in the theme dropdown. Each theme is specified as an object with two keys: + - **name:** The human-readable name of the theme. + - **class:** The css class to be applied to the body element while the theme is active. + This should correspond to selectors in one of the `additional-css` sheets. - **additional-css:** If you need to slightly change the appearance of your book without overwriting the whole style, you can specify a set of stylesheets that will be loaded after the default ones where you can surgically change the diff --git a/src/config.rs b/src/config.rs index 905020a39c..c2ece7909f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -539,6 +539,8 @@ pub struct HtmlConfig { pub copy_fonts: bool, /// An optional google analytics code. pub google_analytics: Option, + /// Additional themes to list as options. + pub additional_themes: Vec, /// Additional CSS stylesheets to include in the rendered page's ``. pub additional_css: Vec, /// Additional JS scripts to include at the bottom of the rendered page's @@ -592,6 +594,16 @@ pub struct HtmlConfig { pub hash_files: bool, } +/// Configuration for a custom theme. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct UserTheme { + /// The human-readable name of the theme. + pub name: String, + /// The class to apply to the `` when this theme is active. + pub class: String, +} + impl Default for HtmlConfig { fn default() -> HtmlConfig { HtmlConfig { @@ -603,6 +615,7 @@ impl Default for HtmlConfig { mathjax_support: false, copy_fonts: true, google_analytics: None, + additional_themes: Vec::new(), additional_css: Vec::new(), additional_js: Vec::new(), fold: Fold::default(), diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 02951c2559..196d444e2c 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -1,5 +1,5 @@ use crate::book::{Book, BookItem}; -use crate::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition}; +use crate::config::{BookConfig, Code, Config, HtmlConfig, Playground, RustEdition, UserTheme}; use crate::errors::*; use crate::renderer::html_handlebars::helpers; use crate::renderer::html_handlebars::StaticFiles; @@ -554,6 +554,20 @@ fn make_data( data.insert("copy_fonts".to_owned(), json!(true)); } + // Build a list of themes. + { + const BUILTIN_THEMES: &[&str] = &["Light", "Rust", "Coal", "Navy", "Ayu"]; + let themes: Vec<_> = BUILTIN_THEMES + .into_iter() + .map(|&name| UserTheme { + name: name.to_owned(), + class: name.to_lowercase(), + }) + .chain(html_config.additional_themes.iter().cloned()) + .collect(); + data.insert("themes".to_owned(), json!(themes)); + } + // Add check to see if there is an additional style if !html_config.additional_css.is_empty() { let mut css = Vec::new(); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index b9d37f3128..42bd6df8e4 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -132,11 +132,9 @@ {{#if search_enabled}}