From 48808bfb75712fbaf49c5dfeeeb560725f0b5b62 Mon Sep 17 00:00:00 2001 From: Konstantin Podsvirov Date: Thu, 3 Nov 2022 01:47:45 +0300 Subject: [PATCH] Configurable MathJax Support --- guide/book.toml | 4 +++- src/config.rs | 25 ++++++++++++++++++++ src/renderer/html_handlebars/hbs_renderer.rs | 10 ++++++++ src/theme/index.hbs | 21 ++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/guide/book.toml b/guide/book.toml index 025efc0b..ca54cca9 100644 --- a/guide/book.toml +++ b/guide/book.toml @@ -8,11 +8,13 @@ language = "en" edition = "2018" [output.html] -mathjax-support = true site-url = "/mdBook/" git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide" edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}" +[output.html.mathjax] +enable = true + [output.html.playground] editable = true line-numbers = true diff --git a/src/config.rs b/src/config.rs index 0c367d84..8cecb663 100644 --- a/src/config.rs +++ b/src/config.rs @@ -490,6 +490,8 @@ pub struct HtmlConfig { pub curly_quotes: bool, /// Should mathjax be enabled? pub mathjax_support: bool, + /// MathJax settings. + pub mathjax: MathJax, /// Whether to fonts.css and respective font files to the output directory. pub copy_fonts: bool, /// An optional google analytics code. @@ -550,6 +552,7 @@ impl Default for HtmlConfig { preferred_dark_theme: None, curly_quotes: false, mathjax_support: false, + mathjax: MathJax::default(), copy_fonts: true, google_analytics: None, additional_css: Vec::new(), @@ -582,6 +585,28 @@ impl HtmlConfig { } } +/// Configuration for how to use MathJax. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "kebab-case")] +pub struct MathJax { + /// Whether MathJax support is enabled. + pub enable: bool, + /// Source. Default: "https://cdn.jsdelivr.net/npm/mathjax@3/es5". + pub source: Option, + /// Configuration. Default: "tex-mml-chtml". + pub config: Option, +} + +impl Default for MathJax { + fn default() -> Self { + Self { + enable: false, + source: Some(String::from("https://cdn.jsdelivr.net/npm/mathjax@3/es5")), + config: Some(String::from("tex-mml-chtml")), + } + } +} + /// Configuration for how to render the print icon, print.html, and print.css. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case")] diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index e170e2fc..227588e5 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -679,6 +679,16 @@ fn make_data( if html_config.mathjax_support { data.insert("mathjax_support".to_owned(), json!(true)); + } else if html_config.mathjax.enable { + data.insert("mathjax_enable".to_owned(), json!(true)); + data.insert( + "mathjax_source".to_owned(), + json!(html_config.mathjax.source), + ); + data.insert( + "mathjax_config".to_owned(), + json!(html_config.mathjax.config), + ); } // This `matches!` checks for a non-empty file. diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 6f3948c6..b5ed08f7 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -51,6 +51,24 @@ {{#if mathjax_support}} + {{else}} + {{#if mathjax_enable}} + + {{#if is_print}} + + {{/if}} + + {{/if}} {{/if}} @@ -304,6 +322,8 @@ }); {{else}} + {{#if mathjax_enable}} + {{else}} {{/if}} {{/if}} + {{/if}}