From 2b074fd4cdb2550af6d89cb919ee94e4b4adf63d Mon Sep 17 00:00:00 2001 From: Ombretta Strafforello Date: Fri, 10 Jul 2026 19:10:49 +0200 Subject: [PATCH] Pin jQuery version in generated docs pages Add an mkdocs on_post_page hook that normalises the jQuery version referenced by rendered notebook pages (injected by the mknotebooks widget renderer) to a single current, pinned release (3.7.1). The hook lives outside docs/ so it is not published as a static asset and runs on every mkdocs build. --- hooks/pin_jquery_version.py | 46 +++++++++++++++++++++++++++++++++++++ mkdocs.yml | 6 +++++ 2 files changed, 52 insertions(+) create mode 100644 hooks/pin_jquery_version.py diff --git a/hooks/pin_jquery_version.py b/hooks/pin_jquery_version.py new file mode 100644 index 0000000..1aa052f --- /dev/null +++ b/hooks/pin_jquery_version.py @@ -0,0 +1,46 @@ +"""MkDocs hook: pin the jQuery version used by rendered notebook pages. + +The ``mknotebooks`` plugin injects a Jupyter-widgets renderer snippet into every +rendered notebook page. That snippet references an old jQuery release from a CDN. +This hook normalises any older jQuery reference in the generated HTML to a single +current, pinned release so the documentation site loads a consistent version. +""" + +from __future__ import annotations + +import re + +# Minimum jQuery version to keep; anything below is bumped to PINNED_VERSION. +MIN_VERSION = (3, 5, 0) +PINNED_VERSION = "3.7.1" + +# Matches CDN references such as: +# https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js +# https://code.jquery.com/jquery-1.12.4.min.js +_CDNJS_RE = re.compile(r"(ajax/libs/jquery/)(\d+)\.(\d+)\.(\d+)(/jquery)") +_JQUERY_COM_RE = re.compile(r"(jquery-)(\d+)\.(\d+)\.(\d+)(\.min\.js)") + + +def _below_min(major: int, minor: int, patch: int) -> bool: + return (major, minor, patch) < MIN_VERSION + + +def _bump_cdnjs(match: re.Match) -> str: + major, minor, patch = (int(match.group(i)) for i in (2, 3, 4)) + if _below_min(major, minor, patch): + return f"{match.group(1)}{PINNED_VERSION}{match.group(5)}" + return match.group(0) + + +def _bump_jquery_com(match: re.Match) -> str: + major, minor, patch = (int(match.group(i)) for i in (2, 3, 4)) + if _below_min(major, minor, patch): + return f"{match.group(1)}{PINNED_VERSION}{match.group(5)}" + return match.group(0) + + +def on_post_page(output: str, page, config) -> str: # noqa: ANN001, ARG001 + """Pin older jQuery references in a rendered page's HTML.""" + patched = _CDNJS_RE.sub(_bump_cdnjs, output) + patched = _JQUERY_COM_RE.sub(_bump_jquery_com, patched) + return patched diff --git a/mkdocs.yml b/mkdocs.yml index b219b1a..e816591 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -38,6 +38,12 @@ markdown_extensions: pygments_style: vs noclasses: true +hooks: + # Pins the jQuery version referenced by rendered notebook pages to a single + # current release. Kept outside docs/ so it is not published as a static + # asset. See hooks/pin_jquery_version.py. + - hooks/pin_jquery_version.py + plugins: - search - mkdocstrings