MediaWiki:Gadget-vector-headanchor.js

/*! * Vector HeadAnchors gadget, from  * * Copyright 2013-2020 Timo Tijhof * @license MIT  */ // Revision as of 2021-01-29

mw.hook( 'wikipage.content' ).add( function ( $content ) { $content.find( 'span.mw-headline' ).each( function ( i, el ) { var wrap, anchor, id = el.id, heading = el.parentNode;

// If anchor is here already, skip. if ( !id || heading.querySelector( '.tpl-vheadanchor') ) { return; }

heading.classList.add('tpl-vheadanchor-heading');

// Move 'id' attribute to heading, needed for :target to work in CSS. el.removeAttribute('id'); heading.id = id;

// Insert anchor. anchor = document.createElement( 'a' ); anchor.href = '#' + id; anchor.className = 'tpl-vheadanchor'; anchor.title = 'Link to this section'; heading.appendChild(anchor); } ); } );