Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/nbsphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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().
Expand Down Expand Up @@ -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)
Expand Down
Loading