Source: sphinx-rtd-theme
Followup-For: Bug #1075914
X-Debbugs-Cc: hwans...@mailbox.org
Control: tags -1 patch

> I've considered attaching the event handler to any menu hyperlinks whose href
> attribute starts with a '#' character (CSS queries can
> prefix-match[2]).  However,
> currently some singlehtml menu hyperlinks contain the '.html' filename
> part.  That
> may change if https://github.com/sphinx-doc/sphinx/pull/12551 is
> merged upstream.

(apologies for my formatting there; I was manually editing within GMail to
try to fit within 80-char limits, but their editor doesn't provide monospaced
text as far as I'm aware.. and somehow I messed that up.  reportbug produces
differently-odd output from me; but I do find it easier to use a good text
editor when using it, so.. here I am)

The aforementioned Sphinx pull request has been merged, so from subsequent
versions of Sphinx onwards, it should be possible to reliably detect same-page
hyperlinks -- as needed to fix this event handler -- in the sidebar menu by
checking for anchor hrefs that begin with the '#' (query-string fragment)
character.

I've opened a pull request for that upstream with sphinx-rtd-theme[1] although
I expect that the Sphinx version dependency may be a concern for it.

[1] - https://github.com/readthedocs/sphinx_rtd_theme/pull/1588
commit 2e97557739984dcd026cb86610cbf322ce3e48dd
Author: James Addison <j...@jp-hosting.net>
Date:   Mon Aug 12 12:50:33 2024 +0100

    Attach sidebar-close handler to current-page links
    
    This is intended primarily to resolve a problem with `singlehtml`
    documentation projects when viewed on narrow (typically mobile)
    displays.
    
    Under those circumstances, the `.current` CSS selector does not
    match any sidebar menu elements, because there is no logical notion
    of a 'current' page in `singlehtml` -- every item is on the same
    page.
    
    Therefore this change proposes an alternative way to identify sidebar
    links that are 'current' -- it checks for anchors that have an `href`
    beginning with the query-string fragment identifier (`#`).
    
    This is intended to be compatible with both `singlehtml` and `html`
    project builds.  It depends upon sphinx-doc/sphinx#12551

diff --git a/src/theme.js b/src/theme.js
index db546b4..106c7a0 100644
--- a/src/theme.js
+++ b/src/theme.js
@@ -86,7 +86,7 @@ function ThemeNav () {
             })
 
             // Nav menu link click operations
-            .on('click', ".wy-menu-vertical .current ul li a", function() {
+            .on('click', ".wy-menu-vertical ul li a[href^='#']", function() {
                 var target = $(this);
                 // Close menu when you click a link.
                 $("[data-toggle='wy-nav-shift']").removeClass("shift");

Reply via email to