|
3 | 3 | This fixes two issues: |
4 | 4 |
|
5 | 5 | 1. Mathjax should not search for ``$`` delimiters, nor LaTeX amsmath environments, |
6 | | - since we already achieve this with the dollarmath and amsmath mrakdown-it-py plugins |
| 6 | + since we already achieve this with the dollarmath and amsmath markdown-it-py plugins |
7 | 7 | 2. amsmath math blocks should be wrapped in mathjax delimiters (default ``\\[...\\]``), |
8 | 8 | and assigned an equation number |
9 | 9 |
|
|
20 | 20 | logger = logging.getLogger(__name__) |
21 | 21 |
|
22 | 22 |
|
23 | | -def log_override_warning(app: Sphinx, version: int, current: str, new: str) -> None: |
| 23 | +def log_override_warning(app: Sphinx, config_name: str, current: str, new: str) -> None: |
24 | 24 | """Log a warning if MathJax configuration being overridden.""" |
25 | 25 | if logging.is_suppressed_warning("myst", "mathjax", app.config.suppress_warnings): |
26 | 26 | return |
27 | | - config_name = ( |
28 | | - "mathjax3_config['options']['processHtmlClass']" |
29 | | - if version == 3 |
30 | | - else "mathjax_config['tex2jax']['processClass']" |
31 | | - ) |
32 | 27 | logger.warning( |
33 | | - f"`{config_name}` is being overridden by myst-parser: '{current}' -> '{new}'. " |
| 28 | + f"`{config_name}['options']['processHtmlClass']` " |
| 29 | + f"is being overridden by myst-parser: '{current}' -> '{new}'. " |
34 | 30 | "Set `suppress_warnings=['myst.mathjax']` to ignore this warning, or " |
35 | 31 | "`myst_update_mathjax=False` if this is undesirable." |
36 | 32 | ) |
@@ -59,37 +55,35 @@ def override_mathjax(app: Sphinx): |
59 | 55 |
|
60 | 56 | mjax_classes = app.env.myst_config.mathjax_classes # type: ignore[attr-defined] |
61 | 57 |
|
62 | | - if "mathjax3_config" in app.config: |
63 | | - # sphinx 4 + mathjax 3 |
64 | | - app.config.mathjax3_config = app.config.mathjax3_config or {} |
65 | | - app.config.mathjax3_config.setdefault("options", {}) |
66 | | - if ( |
67 | | - "processHtmlClass" in app.config.mathjax3_config["options"] |
68 | | - and app.config.mathjax3_config["options"]["processHtmlClass"] |
69 | | - != mjax_classes |
70 | | - ): |
71 | | - log_override_warning( |
72 | | - app, |
73 | | - 3, |
74 | | - app.config.mathjax3_config["options"]["processHtmlClass"], |
75 | | - mjax_classes, |
76 | | - ) |
77 | | - app.config.mathjax3_config["options"]["processHtmlClass"] = mjax_classes |
78 | | - elif "mathjax_config" in app.config: |
79 | | - # sphinx 3 + mathjax 2 |
80 | | - app.config.mathjax_config = app.config.mathjax_config or {} |
81 | | - app.config.mathjax_config.setdefault("tex2jax", {}) |
82 | | - if ( |
83 | | - "processClass" in app.config.mathjax_config["tex2jax"] |
84 | | - and app.config.mathjax_config["tex2jax"]["processClass"] != mjax_classes |
85 | | - ): |
86 | | - log_override_warning( |
87 | | - app, |
88 | | - 2, |
89 | | - app.config.mathjax_config["tex2jax"]["processClass"], |
90 | | - mjax_classes, |
91 | | - ) |
92 | | - app.config.mathjax_config["tex2jax"]["processClass"] = mjax_classes |
| 58 | + # Determine which mathjax config to modify: |
| 59 | + # prefer whichever the user has explicitly set, falling back to |
| 60 | + # the Sphinx version's default (mathjax4_config for Sphinx 9+, mathjax3_config for 8). |
| 61 | + # Note: if a user sets both mathjax3_config and mathjax4_config, we only update |
| 62 | + # the v4 config (Sphinx emits both but v4 clobbers v3 at runtime anyway). |
| 63 | + has_mjax4 = hasattr(app.config, "mathjax4_config") |
| 64 | + if has_mjax4 and app.config.mathjax4_config: |
| 65 | + config_attr = "mathjax4_config" |
| 66 | + elif app.config.mathjax3_config: |
| 67 | + config_attr = "mathjax3_config" |
| 68 | + elif has_mjax4: |
| 69 | + config_attr = "mathjax4_config" |
| 70 | + else: |
| 71 | + config_attr = "mathjax3_config" |
| 72 | + |
| 73 | + config: dict = getattr(app.config, config_attr) or {} # type: ignore[type-arg] |
| 74 | + config.setdefault("options", {}) |
| 75 | + if ( |
| 76 | + "processHtmlClass" in config["options"] |
| 77 | + and config["options"]["processHtmlClass"] != mjax_classes |
| 78 | + ): |
| 79 | + log_override_warning( |
| 80 | + app, |
| 81 | + config_attr, |
| 82 | + config["options"]["processHtmlClass"], |
| 83 | + mjax_classes, |
| 84 | + ) |
| 85 | + config["options"]["processHtmlClass"] = mjax_classes |
| 86 | + setattr(app.config, config_attr, config) |
93 | 87 |
|
94 | 88 |
|
95 | 89 | def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None: |
|
0 commit comments