From 9eca0e0f20256fd5ebdccc6dd54cd30b1a7fdca7 Mon Sep 17 00:00:00 2001 From: expikr <77922942+expikr@users.noreply.github.com> Date: Thu, 27 Feb 2025 15:41:13 +0800 Subject: [PATCH] Additional theme --- src/config.rs | 7 ++++ src/renderer/html_handlebars/hbs_renderer.rs | 12 ++++++ src/theme/book.js | 30 ++++++++++----- src/theme/index.hbs | 10 ++++- test_book/book.toml | 2 + test_book/orange.css | 39 ++++++++++++++++++++ 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 test_book/orange.css diff --git a/src/config.rs b/src/config.rs index 905020a39c..f979b963e5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -541,6 +541,8 @@ pub struct HtmlConfig { pub google_analytics: Option, /// Additional CSS stylesheets to include in the rendered page's ``. pub additional_css: Vec, + /// Additional theme. + pub additional_theme: Vec, /// Additional JS scripts to include at the bottom of the rendered page's /// ``. pub additional_js: Vec, @@ -604,6 +606,7 @@ impl Default for HtmlConfig { copy_fonts: true, google_analytics: None, additional_css: Vec::new(), + additional_theme: Vec::new(), additional_js: Vec::new(), fold: Fold::default(), playground: Playground::default(), @@ -822,6 +825,7 @@ mod tests { smart-punctuation = true google-analytics = "123456" additional-css = ["./foo/bar/baz.css"] + additional-theme = ["foobar"] git-repository-url = "https://foo.com/" git-repository-icon = "fa-code-fork" @@ -869,6 +873,7 @@ mod tests { smart_punctuation: true, google_analytics: Some(String::from("123456")), additional_css: vec![PathBuf::from("./foo/bar/baz.css")], + additional_theme: vec![PathBuf::from("foobar")], theme: Some(PathBuf::from("./themedir")), default_theme: Some(String::from("rust")), playground: playground_should_be, @@ -1049,6 +1054,7 @@ mod tests { smart-punctuation = true google-analytics = "123456" additional-css = ["custom.css", "custom2.css"] + additional-theme = ["barfoo"] additional-js = ["custom.js"] "#; @@ -1074,6 +1080,7 @@ mod tests { smart_punctuation: true, google_analytics: Some(String::from("123456")), additional_css: vec![PathBuf::from("custom.css"), PathBuf::from("custom2.css")], + additional_theme: vec![PathBuf::from("barfoo")], additional_js: vec![PathBuf::from("custom.js")], ..Default::default() }; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 02951c2559..2657b3c941 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -566,6 +566,18 @@ fn make_data( data.insert("additional_css".to_owned(), json!(css)); } + // Add check to see if there are additional themes + if !html_config.additional_theme.is_empty() { + let mut theme = Vec::new(); + for name in &html_config.additional_theme { + match name.strip_prefix(root) { + Ok(p) => theme.push(p.to_str().expect("Could not convert to str")), + Err(_) => theme.push(name.to_str().expect("Could not convert to str")), + } + } + data.insert("additional_theme".to_owned(), json!(theme)); + } + // Add check to see if there is an additional script if !html_config.additional_js.is_empty() { let mut js = Vec::new(); diff --git a/src/theme/book.js b/src/theme/book.js index 089bd1296b..af50e0acda 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -309,7 +309,9 @@ function playground_text(playground, hidden = true) { themePopup.querySelectorAll('.theme-selected').forEach(function (el) { el.classList.remove('theme-selected'); }); - themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected'); + try { + themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected'); + } catch (e) { } } function hideThemes() { @@ -322,9 +324,9 @@ function playground_text(playground, hidden = true) { var theme; try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { } if (theme === null || theme === undefined || !themeIds.includes(theme)) { - return default_theme; + return default_theme.replace(/\W+/g, '_').toLowerCase(); } else { - return theme; + return theme.replace(/\W+/g, '_').toLowerCase(); } } @@ -335,13 +337,17 @@ function playground_text(playground, hidden = true) { stylesheets.ayuHighlight.disabled = true; stylesheets.tomorrowNight.disabled = false; stylesheets.highlight.disabled = true; - ace_theme = "ace/theme/tomorrow_night"; } else if (theme == 'ayu') { stylesheets.ayuHighlight.disabled = false; stylesheets.tomorrowNight.disabled = true; stylesheets.highlight.disabled = true; ace_theme = "ace/theme/tomorrow_night"; + } else if (theme == 'rust' || theme == 'light') { + stylesheets.ayuHighlight.disabled = true; + stylesheets.tomorrowNight.disabled = true; + stylesheets.highlight.disabled = false; + ace_theme = "ace/theme/dawn"; } else { stylesheets.ayuHighlight.disabled = true; stylesheets.tomorrowNight.disabled = true; @@ -359,17 +365,23 @@ function playground_text(playground, hidden = true) { }); } - var previousTheme = get_theme(); - + var previousTheme = get_theme().replace(/\W+/g, '_').toLowerCase(); + var selectedTheme = theme.replace(/\W+/g, '_').toLowerCase(); if (store) { - try { localStorage.setItem('mdbook-theme', theme); } catch (e) { } + try { localStorage.setItem('mdbook-theme', selectedTheme); } catch (e) { } } - html.classList.remove(previousTheme); - html.classList.add(theme); + try { + html.classList.remove( previousTheme ); + html.classList.add( selectedTheme ); + } catch (e) { } + updateThemeSelected(); } + // Sanitize theme id names + themePopup.querySelectorAll("button").forEach(e=>{e.id=e.id.replace(/\W+/g, '_').toLowerCase();}); + // Set theme var theme = get_theme(); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index b9d37f3128..6426f73a11 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -57,6 +57,7 @@ @@ -85,8 +86,10 @@ try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } if (theme === null || theme === undefined) { theme = default_theme; } const html = document.documentElement; - html.classList.remove('{{ default_theme }}') - html.classList.add(theme); + try { + html.classList.remove('{{ default_theme }}'); + html.classList.add(theme.replace(/\W+/g, '_').toLowerCase()); + } catch(e) { } html.classList.add("js"); @@ -137,6 +140,9 @@
  • + {{#each additional_theme}} +
  • + {{/each}} {{#if search_enabled}}