From 34126021d9cdff90acd73131e9d7d7132c7f1eaa Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Wed, 13 Jun 2018 23:24:14 +0900 Subject: [PATCH] Close #3784: mathjax: Add :confval:`mathjax_options` --- CHANGES | 2 ++ doc/ext/math.rst | 10 ++++++++++ sphinx/ext/mathjax.py | 5 ++++- tests/test_ext_math.py | 12 ++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index c2c0bd45c..a4beb3c7d 100644 --- a/CHANGES +++ b/CHANGES @@ -137,6 +137,8 @@ Features added * html: Output ``canonical_url`` metadata if :confval:`html_baseurl` set (refs: #4193) * #5029: autosummary: expose ``inherited_members`` to template +* #3784: mathjax: Add :confval:`mathjax_options` to give options to script tag + for mathjax Bugs fixed ---------- diff --git a/doc/ext/math.rst b/doc/ext/math.rst index c00713a3d..c299b7bee 100644 --- a/doc/ext/math.rst +++ b/doc/ext/math.rst @@ -260,6 +260,16 @@ Sphinx. You can also give a full ``https://`` URL different from the CDN URL. +.. confval:: mathjax_options + + The options to script tag for mathjax. For example, you can set integrity + option with following setting:: + + mathjax_options = { + 'integrity': 'sha384-......', + } + + The default is empty (``{}``). :mod:`sphinx.ext.jsmath` -- Render math via JavaScript ------------------------------------------------------ diff --git a/sphinx/ext/mathjax.py b/sphinx/ext/mathjax.py index 8aeafefc7..0d73f3d3d 100644 --- a/sphinx/ext/mathjax.py +++ b/sphinx/ext/mathjax.py @@ -74,6 +74,8 @@ def builder_inited(app): 'mathjax extension to work') if app.builder.format == 'html': options = {'async': 'async'} + if app.config.mathjax_options: + options.update(app.config.mathjax_options) app.builder.add_js_file(app.config.mathjax_path, **options) # type: ignore @@ -88,7 +90,8 @@ def setup(app): # https://docs.mathjax.org/en/latest/start.html#secure-access-to-the-cdn app.add_config_value('mathjax_path', 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?' - 'config=TeX-AMS-MML_HTMLorMML', False) + 'config=TeX-AMS-MML_HTMLorMML', 'html') + app.add_config_value('mathjax_options', {}, 'html') app.add_config_value('mathjax_inline', [r'\(', r'\)'], 'html') app.add_config_value('mathjax_display', [r'\[', r'\]'], 'html') app.connect('builder-inited', builder_inited) diff --git a/tests/test_ext_math.py b/tests/test_ext_math.py index 47465f07e..99940977b 100644 --- a/tests/test_ext_math.py +++ b/tests/test_ext_math.py @@ -89,6 +89,18 @@ def test_imgmath_svg(app, status, warning): assert re.search(html, content, re.S) +@pytest.mark.sphinx('html', testroot='ext-math', + confoverrides={'extensions': ['sphinx.ext.mathjax'], + 'mathjax_options': {'integrity': 'sha384-0123456789'}}) +def test_mathjax_options(app, status, warning): + app.builder.build_all() + + content = (app.outdir / 'index.html').text() + assert ('' in content) + + @pytest.mark.sphinx('html', testroot='ext-math', confoverrides={'extensions': ['sphinx.ext.mathjax']}) def test_mathjax_align(app, status, warning):