Configurable MathJax Support

This commit is contained in:
Konstantin Podsvirov 2022-11-03 01:47:45 +03:00
parent efb671aaf2
commit 48808bfb75
4 changed files with 59 additions and 1 deletions

View File

@ -8,11 +8,13 @@ language = "en"
edition = "2018" edition = "2018"
[output.html] [output.html]
mathjax-support = true
site-url = "/mdBook/" site-url = "/mdBook/"
git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide" 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}" edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
[output.html.mathjax]
enable = true
[output.html.playground] [output.html.playground]
editable = true editable = true
line-numbers = true line-numbers = true

View File

@ -490,6 +490,8 @@ pub struct HtmlConfig {
pub curly_quotes: bool, pub curly_quotes: bool,
/// Should mathjax be enabled? /// Should mathjax be enabled?
pub mathjax_support: bool, pub mathjax_support: bool,
/// MathJax settings.
pub mathjax: MathJax,
/// Whether to fonts.css and respective font files to the output directory. /// Whether to fonts.css and respective font files to the output directory.
pub copy_fonts: bool, pub copy_fonts: bool,
/// An optional google analytics code. /// An optional google analytics code.
@ -550,6 +552,7 @@ impl Default for HtmlConfig {
preferred_dark_theme: None, preferred_dark_theme: None,
curly_quotes: false, curly_quotes: false,
mathjax_support: false, mathjax_support: false,
mathjax: MathJax::default(),
copy_fonts: true, copy_fonts: true,
google_analytics: None, google_analytics: None,
additional_css: Vec::new(), 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<String>,
/// Configuration. Default: "tex-mml-chtml".
pub config: Option<String>,
}
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. /// Configuration for how to render the print icon, print.html, and print.css.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")] #[serde(default, rename_all = "kebab-case")]

View File

@ -679,6 +679,16 @@ fn make_data(
if html_config.mathjax_support { if html_config.mathjax_support {
data.insert("mathjax_support".to_owned(), json!(true)); 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. // This `matches!` checks for a non-empty file.

View File

@ -51,6 +51,24 @@
{{#if mathjax_support}} {{#if mathjax_support}}
<!-- MathJax --> <!-- MathJax -->
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{else}}
{{#if mathjax_enable}}
<!-- MathJax -->
{{#if is_print}}
<script>
MathJax = {
startup: {
pageReady: () => {
return MathJax.startup.defaultPageReady().then(() => {
window.setTimeout(window.print, 100);
});
}
}
};
</script>
{{/if}}
<script id="MathJax-script" async src="{{ mathjax_source }}/{{ mathjax_config }}.js"></script>
{{/if}}
{{/if}} {{/if}}
</head> </head>
<body> <body>
@ -304,6 +322,8 @@
}); });
</script> </script>
{{else}} {{else}}
{{#if mathjax_enable}}
{{else}}
<script> <script>
window.addEventListener('load', function() { window.addEventListener('load', function() {
window.setTimeout(window.print, 100); window.setTimeout(window.print, 100);
@ -311,6 +331,7 @@
</script> </script>
{{/if}} {{/if}}
{{/if}} {{/if}}
{{/if}}
</div> </div>
</body> </body>