New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to defer loading of MathJax #9450
Comments
The global approach in 395 isn't really sustainable: it requires all-ways cooperation between all projects that want to customize MathJax. Additionally, when processing a MyST document without Sphinx, the MathJax configuration changes are not performed (part of executablebooks#347). And, of course, this approach of overriding the MathJax object causes issues down the line for projects that need to customize MathJax (the setting in Sphinx isn't sufficient, see sphinx-doc/sphinx#9450) The following two approaches would not cause these issues: 1. Add a custom script instead of touching the mathjax3_config variable; something like this, essentially: ```js app.add_js_file(None, priority=0, body=""" var MathJax = window.MathJax || MathJax; MathJax.options = MathJax.options || {}; MathJax.options.processHtmlClass = (MathJax.options.processHtmlClass || "") + "|math"; """) ``` - Don't touch MathJax_config at all; instead, add an explicit `mathjax_process` class on all math nodes, either by changing `docutils_renderer` (this PR) or by adding a Docutils transform to processes all math nodes: ```python class ActivateMathJaxTransform(Transform): default_priority = 800 @staticmethod def is_math(node): return isinstance(node, (math, math_block)) def apply(self, **kwargs): for node in self.document.traverse(self.is_math): node.attributes.setdefault("classes", []).append("mathjax_process") ``` This PR isn't ready for merging; it's just to start a discussion.
I'm not good at loading JS. Could you let me know the impact of changing |
I don't think it's an incompatible change. For MDN:
So changing |
I found an explanation for this topic:
I believe using defer option instead is a good alternative. |
Close #9450: mathjax: Load MathJax via "defer" strategy
Is there a way to have the option to choose async instead of defer? |
@raphaeltimbo Could you file a new issue, please? I'll fix it soon. |
@tk0miya, I have filed a new issue as requested. Let me know if you need additional information. |
Thanks! |
Is your feature request related to a problem? Please describe.
It is quite tricky to configure MathJax to work with Sphinx currently.
Sphinx loads MathJax asynchronously since #3606 and #5005. While this was fine for MathJax 2, because of the special kind of
<script>
blocks mentioned in #5616 , it doesn't work well with MathJax 3.Indeed, in MathJax 3, MathJax expect a config
<script>
block to be present before MathJax is loaded. Sphinx 4 addedmathjax3_config
parameter:This assumes that the
config
is a simple dictionary, which isn't sufficient: that configuration should be able to contain functions, for example.The only possibility at the moment is to add a separate script file containing a MathJax configuration and to load it with
app.add_js_file
.Describe the solution you'd like
There are three possibilities:
Allow arbitrary strings for mathjax3_config, and in that case don't JSON-serialize them.
Change
async
todefer
when loading MathJax.Make it possible for users to change
async
todefer
themselves. At the moment this isn't possible because theasync
flags is unconditionally added:The latter two are preferable because they would allow individual pages to use different MathJax config by using a
.. raw::
block to override the default MathJax configuration on a given page (the script in thatraw
block will run before MathJax loads thanks to thedefer
option).CC @jfbu , the author of #5616.
Thanks!
The text was updated successfully, but these errors were encountered: