From 8b1b3aaadd4cdf594ee3afebf930424b2efb4dd0 Mon Sep 17 00:00:00 2001 From: gabalafou Date: Wed, 6 Aug 2025 02:06:34 +0200 Subject: [PATCH] Fix nbsphinx_assume_equations to work with newer versions of Sphinx --- src/nbsphinx/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/nbsphinx/__init__.py b/src/nbsphinx/__init__.py index e597aed4..8bfd7d30 100644 --- a/src/nbsphinx/__init__.py +++ b/src/nbsphinx/__init__.py @@ -1490,6 +1490,9 @@ class ForceEquations(docutils.transforms.Transform): def apply(self): env = self.document.settings.env if env.config.nbsphinx_assume_equations: + # sphinx >= 8.2 + self.document['nbsphinx_assume_equations'] = True + # sphinx < 8.2 env.get_domain('math').data['has_equations'][env.docname] = True @@ -1700,6 +1703,16 @@ def env_purge_doc(app, env, docname): env.nbsphinx_widgets.discard(docname) +def set_flag_to_add_mathjax(app, pagename, templatename, context, doctree): + """Tell Sphinx to load MathJax for pages created by this extension. + + Unless ``nbsphinx_assume_equations`` config variable was set to false. + """ + if doctree and doctree.get('nbsphinx_assume_equations'): + # This context variable is used by Sphinx starting in version 8.2 + context['has_maths_elements'] = True + + def html_page_context(app, pagename, templatename, context, doctree): """Add CSS files for code cells and galleries.""" # NB: the CSS files are copied in html_collect_pages(). @@ -2095,6 +2108,10 @@ def setup(app): app.connect('builder-inited', builder_inited) app.connect('config-inited', config_inited) app.connect('html-page-context', html_page_context) + # Give our handler higher priority so that it executes before MathJax's. We + # need to write the context variable `has_maths_elements` before the MathJax + # Sphinx extension reads it. + app.connect('html-page-context', set_flag_to_add_mathjax, priority=100) app.connect('html-collect-pages', html_collect_pages) app.connect('env-purge-doc', env_purge_doc) app.connect('env-updated', env_updated)