Skip to content

Commit 123c2b6

Browse files
committed
Configurable MathJax Support
1 parent 3a24f10 commit 123c2b6

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
@@ -487,6 +487,8 @@ pub struct HtmlConfig {
487487
pub curly_quotes: bool,
488488
/// Should mathjax be enabled?
489489
pub mathjax_support: bool,
490+
/// MathJax settings.
491+
pub mathjax: MathJax,
490492
/// Whether to fonts.css and respective font files to the output directory.
491493
pub copy_fonts: bool,
492494
/// An optional google analytics code.
@@ -548,6 +550,7 @@ impl Default for HtmlConfig {
548550
preferred_dark_theme: None,
549551
curly_quotes: false,
550552
mathjax_support: false,
553+
mathjax: MathJax::default(),
551554
copy_fonts: true,
552555
google_analytics: None,
553556
additional_css: Vec::new(),
@@ -580,6 +583,28 @@ impl HtmlConfig {
580583
}
581584
}
582585

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

653653
if html_config.mathjax_support {
654654
data.insert("mathjax_support".to_owned(), json!(true));
655+
} else if html_config.mathjax.enable {
656+
data.insert("mathjax_enable".to_owned(), json!(true));
657+
data.insert(
658+
"mathjax_source".to_owned(),
659+
json!(html_config.mathjax.source),
660+
);
661+
data.insert(
662+
"mathjax_config".to_owned(),
663+
json!(html_config.mathjax.config),
664+
);
655665
}
656666

657667
if html_config.copy_fonts {

src/theme/index.hbs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@
5252
{{#if mathjax_support}}
5353
<!-- MathJax -->
5454
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
55+
{{else}}
56+
{{#if mathjax_enable}}
57+
<!-- MathJax -->
58+
{{#if is_print}}
59+
<script>
60+
MathJax = {
61+
startup: {
62+
pageReady: () => {
63+
return MathJax.startup.defaultPageReady().then(() => {
64+
window.setTimeout(window.print, 100);
65+
});
66+
}
67+
}
68+
};
69+
</script>
70+
{{/if}}
71+
<script id="MathJax-script" async src="{{ mathjax_source }}/{{ mathjax_config }}.js"></script>
72+
{{/if}}
5573
{{/if}}
5674
</head>
5775
<body>
@@ -302,13 +320,16 @@
302320
});
303321
</script>
304322
{{else}}
323+
{{#if mathjax_enable}}
324+
{{else}}
305325
<script>
306326
window.addEventListener('load', function() {
307327
window.setTimeout(window.print, 100);
308328
});
309329
</script>
310330
{{/if}}
311331
{{/if}}
332+
{{/if}}
312333

313334
</body>
314335
</html>

0 commit comments

Comments
 (0)