Skip to content
This repository was archived by the owner on Apr 9, 2025. It is now read-only.

jQuery: inject the CDN version if Sphinx>=6.x #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions hoverxref/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,27 @@ def deprecated_configs_warning(app, exception):
app.config.hoverxref_api_host = app.config.hoverxref_tooltip_api_host


def setup_jquery(app, exception):
"""
Inject jQuery if Sphinx>=6.x

Staring on Sphinx 6.0, jQuery is not included with it anymore.
As this extension depends on jQuery, we are including it when Sphinx>=6.x
"""

if sphinx.version_info >= (6, 0, 0):
# https://jquery.com/download/#using-jquery-with-a-cdn
jquery_cdn_url = "https://code.jquery.com/jquery-3.6.0.min.js"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noted similar on our theme, it's better to include dependencies via package dependencies for a few reasons:

readthedocs/sphinx_rtd_theme#1299 (comment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this dependency can be brought in without webpack, though a longer term goal would be to use webpack, or more likely to remove jQuery entirely.

To copy the jQuery release into the package, install jQuery via package.json and copy the JS minified file from node_modules/ into the package.

Of course the issues you noted with jQuery versions and conflicting versions between extensions is going to be a problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To copy the jQuery release into the package, install jQuery via package.json and copy the JS minified file from node_modules/ into the package.

is this better than just grabbing the official minified version from https://jquery.com/download/?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. It's much easier to keep up to date and we're using npm to manage these dependencies on our other JS projects.

html_js_files = getattr(app.config, "html_js_files", [])
html_js_files.append((
jquery_cdn_url,
{
'integrity': 'sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=',
'crossorigin': 'anonymous'
}
))
app.config.html_js_files = html_js_files


def setup(app):
"""Setup ``hoverxref`` Sphinx extension."""
Expand Down Expand Up @@ -364,6 +385,7 @@ def setup(app):
app.add_config_value('hoverxref_modal_prefix_title', '📝 ', 'env')

app.connect('config-inited', deprecated_configs_warning)
app.connect('config-inited', setup_jquery)

app.connect('config-inited', setup_domains)
app.connect('config-inited', setup_sphinx_tabs)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_htmltag.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,21 @@ def test_intersphinx_all_mappings(app, status, warning):

for chunk in chunks_regex:
assert re.search(chunk, content)


@pytest.mark.sphinx(
srcdir=srcdir,
)
def test_jquery_cdn_injection(app, status, warning):
"""The extension should not change the output if not configured."""
app.build()
path = app.outdir / 'index.html'
assert path.exists() is True
content = open(path).read()

chunk = '<script crossorigin="anonymous" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>'

if sphinx.version_info >= (6, 0, 0):
assert chunk in content
else:
assert chunk not in content