diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index 44af728e..bcc4122a 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -146,6 +146,8 @@ The following configuration options are available: - **theme:** mdBook comes with a default theme and all the resource files needed for it. But if this option is set, mdBook will selectively overwrite the theme files with the ones found in the specified folder. +- **default-theme:** The theme color scheme to select by default in the + 'Change Theme' dropdown. Defaults to `light`. - **curly-quotes:** Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans. Defaults to `false`. - **google-analytics:** If you use Google Analytics, this option lets you enable diff --git a/src/config.rs b/src/config.rs index 55643917..71591ace 100644 --- a/src/config.rs +++ b/src/config.rs @@ -415,6 +415,8 @@ impl Default for BuildConfig { pub struct HtmlConfig { /// The theme directory, if specified. pub theme: Option, + /// The default theme to use, defaults to 'light' + pub default_theme: Option, /// Use "smart quotes" instead of the usual `"` character. pub curly_quotes: bool, /// Should mathjax be enabled? @@ -573,6 +575,7 @@ mod tests { [output.html] theme = "./themedir" + default-theme = "rust" curly-quotes = true google-analytics = "123456" additional-css = ["./foo/bar/baz.css"] @@ -614,6 +617,7 @@ mod tests { google_analytics: Some(String::from("123456")), additional_css: vec![PathBuf::from("./foo/bar/baz.css")], theme: Some(PathBuf::from("./themedir")), + default_theme: Some(String::from("rust")), playpen: playpen_should_be, git_repository_url: Some(String::from("https://foo.com/")), git_repository_icon: Some(String::from("fa-code-fork")), diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index ff28f9da..6ef12f3c 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -214,6 +214,7 @@ impl HtmlHandlebars { ); handlebars.register_helper("previous", Box::new(helpers::navigation::previous)); handlebars.register_helper("next", Box::new(helpers::navigation::next)); + handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option)); } /// Copy across any additional CSS and JavaScript files which the book @@ -395,6 +396,12 @@ fn make_data( data.insert("livereload".to_owned(), json!(livereload)); } + let default_theme = match html_config.default_theme { + Some(ref theme) => theme, + None => "light", + }; + data.insert("default_theme".to_owned(), json!(default_theme)); + // Add google analytics tag if let Some(ref ga) = config.html_config().and_then(|html| html.google_analytics) { data.insert("google_analytics".to_owned(), json!(ga)); diff --git a/src/renderer/html_handlebars/helpers/mod.rs b/src/renderer/html_handlebars/helpers/mod.rs index 62fc6149..e9a8f932 100644 --- a/src/renderer/html_handlebars/helpers/mod.rs +++ b/src/renderer/html_handlebars/helpers/mod.rs @@ -1,2 +1,3 @@ pub mod navigation; pub mod toc; +pub mod theme; \ No newline at end of file diff --git a/src/renderer/html_handlebars/helpers/theme.rs b/src/renderer/html_handlebars/helpers/theme.rs new file mode 100644 index 00000000..24c0dda1 --- /dev/null +++ b/src/renderer/html_handlebars/helpers/theme.rs @@ -0,0 +1,30 @@ +use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError}; + +pub fn theme_option( + h: &Helper, + _r: &Handlebars, + ctx: &Context, + rc: &mut RenderContext, + out: &mut Output, +) -> Result<(), RenderError> { + trace!("theme_option (handlebars helper)"); + + let param = h + .param(0) + .and_then(|v| v.value().as_str()) + .ok_or(RenderError::new( + "Param 0 with String type is required for theme_option helper.", + ))?; + + let theme_name = rc + .evaluate_absolute(ctx, "default_theme", true)? + .as_str() + .ok_or_else(|| RenderError::new("Type error for `default_theme`, string expected"))?; + + out.write(param)?; + if param.to_lowercase() == theme_name.to_lowercase() { + out.write(" (default)")?; + } + + Ok(()) +} diff --git a/src/theme/book.js b/src/theme/book.js index c9c2f4d6..4640e8fc 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -352,7 +352,7 @@ function playpen_text(playpen) { var previousTheme; try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { } - if (previousTheme === null || previousTheme === undefined) { previousTheme = 'light'; } + if (previousTheme === null || previousTheme === undefined) { previousTheme = default_theme; } try { localStorage.setItem('mdbook-theme', theme); } catch (e) { } @@ -364,7 +364,7 @@ function playpen_text(playpen) { // Set theme var theme; try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } - if (theme === null || theme === undefined) { theme = 'light'; } + if (theme === null || theme === undefined) { theme = default_theme; } set_theme(theme); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index e2577e03..bfe4283a 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -35,9 +35,12 @@ {{/if}} - + - + @@ -94,11 +97,11 @@ {{#if search_enabled}}