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] 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 @@