Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collapsing toc when clicking on a nested page #149

Merged
merged 2 commits into from
Jan 7, 2025

Conversation

reakaleek
Copy link
Member

@reakaleek reakaleek commented Jan 7, 2025

Closes #139

Changes

  • Expands all collapsed parent sections in the navigation tree that contain the current page link.
  • This ensures the current page is visible in the navigation by opening all ancestor collapsible sections.
  • The function works by:
    1. Finding the navigation link matching the current page URL
    2. Walking up the DOM tree from that link
    3. Expanding any collapsed parent sections by toggling their CSS classes
Screen.Recording.2025-01-07.at.21.47.29.mov

Notes

This is a hacky fix since the source code for this functionality is minified and just copied here.
Doing this properly should be handled after the next major release.

@reakaleek reakaleek requested a review from Mpdreamz January 7, 2025 20:55
@reakaleek reakaleek self-assigned this Jan 7, 2025
@reakaleek reakaleek requested a review from bmorelli25 January 7, 2025 20:57
@reakaleek reakaleek marked this pull request as ready for review January 7, 2025 20:57
@reakaleek reakaleek mentioned this pull request Jan 7, 2025
Comment on lines +18 to +41
/**
* Expands all collapsed parent sections in the navigation tree that contain the current page link.
* This ensures the current page is visible in the navigation by opening all ancestor collapsible sections.
* The function works by:
* 1. Finding the navigation link matching the current page URL
* 2. Walking up the DOM tree from that link
* 3. Expanding any collapsed parent sections by toggling their CSS classes
*/
function expandCurrentPage() {
const currentPathname = window.location.pathname;
const currentLink = document.querySelector(`a[href="${currentPathname}"]`);
if (currentLink) {
let parent = currentLink.parentElement;
while (parent) {
if (parent.classList && parent.classList.contains('_collapse')) {
parent.classList.remove('_collapse');
parent.classList.add('_expand');
}
parent = parent.parentElement;
}
}
}

expandCurrentPage();
Copy link
Member Author

@reakaleek reakaleek Jan 7, 2025

Choose a reason for hiding this comment

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

It seems like the GH UI does not render this due to its content.

Adding a comment here to see the diff.

Copy link
Member

@Mpdreamz Mpdreamz left a comment

Choose a reason for hiding this comment

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

LGMT! 💯

Once we get new design we can focus on the css an javascript from scratch. We currently include way too much.

@Mpdreamz Mpdreamz merged commit b587de7 into main Jan 7, 2025
3 checks passed
@Mpdreamz Mpdreamz deleted the feature/fix-nested-toc-bug branch January 7, 2025 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested TOC bug
2 participants