Skip to content

Commit 48808bf

Browse files
committed
Configurable MathJax Support
1 parent efb671a commit 48808bf

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

guide/book.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ language = "en"
88
edition = "2018"
99

1010
[output.html]
11-
mathjax-support = true
1211
site-url = "/mdBook/"
1312
git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide"
1413
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
1514

15+
[output.html.mathjax]
16+
enable = true
17+
1618
[output.html.playground]
1719
editable = true
1820
line-numbers = true

src/config.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ pub struct HtmlConfig {
490490
pub curly_quotes: bool,
491491
/// Should mathjax be enabled?
492492
pub mathjax_support: bool,
493+
/// MathJax settings.
494+
pub mathjax: MathJax,
493495
/// Whether to fonts.css and respective font files to the output directory.
494496
pub copy_fonts: bool,
495497
/// An optional google analytics code.
@@ -550,6 +552,7 @@ impl Default for HtmlConfig {
550552
preferred_dark_theme: None,
551553
curly_quotes: false,
552554
mathjax_support: false,
555+
mathjax: MathJax::default(),
553556
copy_fonts: true,
554557
google_analytics: None,
555558
additional_css: Vec::new(),
@@ -582,6 +585,28 @@ impl HtmlConfig {
582585
}
583586
}
584587

588+
/// Configuration for how to use MathJax.
589+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
590+
#[serde(default, rename_all = "kebab-case")]
591+
pub struct MathJax {
592+
/// Whether MathJax support is enabled.
593+
pub enable: bool,
594+
/// Source. Default: "https://cdn.jsdelivr.net/npm/mathjax@3/es5".
595+
pub source: Option<String>,
596+
/// Configuration. Default: "tex-mml-chtml".
597+
pub config: Option<String>,
598+
}
599+
600+
impl Default for MathJax {
601+
fn default() -> Self {
602+
Self {
603+
enable: false,
604+
source: Some(String::from("https://cdn.jsdelivr.net/npm/mathjax@3/es5")),
605+
config: Some(String::from("tex-mml-chtml")),
606+
}
607+
}
608+
}
609+
585610
/// Configuration for how to render the print icon, print.html, and print.css.
586611
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
587612
#[serde(default, rename_all = "kebab-case")]

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,16 @@ fn make_data(
679679

680680
if html_config.mathjax_support {
681681
data.insert("mathjax_support".to_owned(), json!(true));
682+
} else if html_config.mathjax.enable {
683+
data.insert("mathjax_enable".to_owned(), json!(true));
684+
data.insert(
685+
"mathjax_source".to_owned(),
686+
json!(html_config.mathjax.source),
687+
);
688+
data.insert(
689+
"mathjax_config".to_owned(),
690+
json!(html_config.mathjax.config),
691+
);
682692
}
683693

684694
// This `matches!` checks for a non-empty file.

src/theme/index.hbs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@
5151
{{#if mathjax_support}}
5252
<!-- MathJax -->
5353
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
54+
{{else}}
55+
{{#if mathjax_enable}}
56+
<!-- MathJax -->
57+
{{#if is_print}}
58+
<script>
59+
MathJax = {
60+
startup: {
61+
pageReady: () => {
62+
return MathJax.startup.defaultPageReady().then(() => {
63+
window.setTimeout(window.print, 100);
64+
});
65+
}
66+
}
67+
};
68+
</script>
69+
{{/if}}
70+
<script id="MathJax-script" async src="{{ mathjax_source }}/{{ mathjax_config }}.js"></script>
71+
{{/if}}
5472
{{/if}}
5573
</head>
5674
<body>
@@ -304,13 +322,16 @@
304322
});
305323
</script>
306324
{{else}}
325+
{{#if mathjax_enable}}
326+
{{else}}
307327
<script>
308328
window.addEventListener('load', function() {
309329
window.setTimeout(window.print, 100);
310330
});
311331
</script>
312332
{{/if}}
313333
{{/if}}
334+
{{/if}}
314335

315336
</div>
316337
</body>

0 commit comments

Comments
 (0)