From b0ae14a2c75ba86bcaac53b88fda3e901b01cb9c Mon Sep 17 00:00:00 2001 From: Flying-Toast <38232168+Flying-Toast@users.noreply.github.com> Date: Wed, 25 Sep 2019 18:23:54 -0400 Subject: [PATCH 1/4] Automatically use a dark theme according to 'prefers-color-scheme' --- src/config.rs | 5 +++++ src/renderer/html_handlebars/hbs_renderer.rs | 9 +++++++++ src/theme/index.hbs | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 17c941d2..21b5452e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -420,6 +420,11 @@ pub struct HtmlConfig { pub theme: Option, /// The default theme to use, defaults to 'light' pub default_theme: Option, + /// The default dark theme. + /// This theme will be used if the browser requests a dark theme + /// via the 'prefers-color-scheme' CSS media query. + /// Defaults to 'navy'. + pub preferred_dark_theme: Option, /// Use "smart quotes" instead of the usual `"` character. pub curly_quotes: bool, /// Should mathjax be enabled? diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 7a7f0ef6..e9f70225 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -407,6 +407,15 @@ fn make_data( }; data.insert("default_theme".to_owned(), json!(default_theme)); + let preferred_dark_theme = match html_config.preferred_dark_theme { + Some(ref theme) => theme, + None => "navy", + }; + data.insert( + "preferred_dark_theme".to_owned(), + json!(preferred_dark_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/theme/index.hbs b/src/theme/index.hbs index 4e29807c..26800ad5 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -43,7 +43,7 @@ From f0ac13e3e29d9f28718726e73e30396ccb9e3c9b Mon Sep 17 00:00:00 2001 From: Flying-Toast <38232168+Flying-Toast@users.noreply.github.com> Date: Thu, 26 Sep 2019 12:09:30 -0400 Subject: [PATCH 2/4] Document preferred-dark-theme config option --- book-example/src/format/config.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index d443911f..5d40d910 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -150,6 +150,8 @@ The following configuration options are available: 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`. +- **preferred-dark-theme:** The default theme to use if the browser + requests the dark version of the site. Defaults to `navy`. - **curly-quotes:** Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans. Defaults to `false`. - **mathjax-support:** Adds support for [MathJax](mathjax.md). Defaults to From 1ef94c2a7e15b5d0f0e085034863db0827ba16c2 Mon Sep 17 00:00:00 2001 From: Flying-Toast <38232168+Flying-Toast@users.noreply.github.com> Date: Thu, 26 Sep 2019 12:13:25 -0400 Subject: [PATCH 3/4] add preferred-dark-theme to book.toml example --- book-example/src/format/config.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index 5d40d910..257a6db1 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -218,6 +218,7 @@ description = "The example book covers examples." [output.html] theme = "my-theme" default-theme = "light" +preferred-dark-theme = "navy" curly-quotes = true mathjax-support = false google-analytics = "123456" From 9bdec5e7cca1dcf280bf9aa501cc040f749ef27c Mon Sep 17 00:00:00 2001 From: Flying-Toast <38232168+Flying-Toast@users.noreply.github.com> Date: Fri, 4 Oct 2019 19:32:03 -0400 Subject: [PATCH 4/4] preferred-dark-theme defaults to default-theme --- book-example/src/format/config.md | 6 ++++-- src/config.rs | 6 ++---- src/renderer/html_handlebars/hbs_renderer.rs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index 257a6db1..aacd40de 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -150,8 +150,10 @@ The following configuration options are available: 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`. -- **preferred-dark-theme:** The default theme to use if the browser - requests the dark version of the site. Defaults to `navy`. +- **preferred-dark-theme:** The default dark theme. This theme will be used if + the browser requests the dark version of the site via the + ['prefers-color-scheme'](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme) + CSS media query. Defaults to the same theme as `default-theme`. - **curly-quotes:** Convert straight quotes to curly quotes, except for those that occur in code blocks and code spans. Defaults to `false`. - **mathjax-support:** Adds support for [MathJax](mathjax.md). Defaults to diff --git a/src/config.rs b/src/config.rs index 21b5452e..772a03a9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -420,10 +420,8 @@ pub struct HtmlConfig { pub theme: Option, /// The default theme to use, defaults to 'light' pub default_theme: Option, - /// The default dark theme. - /// This theme will be used if the browser requests a dark theme - /// via the 'prefers-color-scheme' CSS media query. - /// Defaults to 'navy'. + /// The theme to use if the browser requests the dark version of the site. + /// Defaults to the same as 'default_theme' pub preferred_dark_theme: Option, /// Use "smart quotes" instead of the usual `"` character. pub curly_quotes: bool, diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index e9f70225..b2b5e53b 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -409,7 +409,7 @@ fn make_data( let preferred_dark_theme = match html_config.preferred_dark_theme { Some(ref theme) => theme, - None => "navy", + None => default_theme, }; data.insert( "preferred_dark_theme".to_owned(),