Skip to content

Commit

Permalink
fix(core): 馃悰 make ToC tracking work for non-content pages
Browse files Browse the repository at this point in the history
Since MW 1.40, ToC has been enabled on all pages including non-content page.
By generating a list of elements to track through the ToC instead of targeting
specific classes, it should be more resilient and will work for non-content pages.
  • Loading branch information
alistair3149 committed Apr 20, 2024
1 parent 0a0a663 commit cd21fd0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions resources/skins.citizen.scripts/tableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ function changeActiveSection( id ) {
* @return {void}
*/
function init( bodyContent ) {
if ( !document.getElementById( 'mw-panel-toc' ) ) {
const tocEl = document.getElementById( 'mw-panel-toc' );
if ( !tocEl ) {
return;
}

const getElements = () => {
/* T13555 */
return bodyContent.querySelectorAll( '.mw-headline' ).length > 0 ? bodyContent.querySelectorAll( '.mw-headline' ) :
bodyContent.querySelectorAll( '.mw-heading' );
const getHeadlineElements = () => {
const getHeadlineIds = () => {
const headlineIds = [];
// Nodelist.forEach is forbidden by mediawiki/no-nodelist-unsupported-methods
Array.from( tocEl.querySelectorAll( '.citizen-toc__listItem' ) ).forEach( ( tocListEl ) => {
// Remove 'toc-' prefix from ID
headlineIds.push( '#' + tocListEl.id.slice( 4 ) );
} );
return headlineIds.join( ',' );
};
return bodyContent.querySelectorAll( getHeadlineIds() );
};

// We use scroll-padding-top to handle scrolling with fixed header
Expand All @@ -64,7 +72,7 @@ function init( bodyContent ) {
) + 20;
};

const headlines = getElements();
const headlines = getHeadlineElements();

// Do not continue if there are no headlines
// TODO: Need to revamp the selector so that it works better with MW 1.40,
Expand Down

0 comments on commit cd21fd0

Please sign in to comment.