MediaWiki:Gadget-sectionAnchors.js

From Old School Near-Reality Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
 1 /**
 2  * Vector HeadAnchors
 3  * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-vector-headanchor.js
 4  * @revision 2015-03-07
 5  *
 6  * @license http://krinkle.mit-license.org/
 7  * @author Timo Tijhof, 2013–2015
 8  * 
 9  * See also:
10  * https://www.mediawiki.org/wiki/Requests_for_comment/Clickable_section_anchors
11  * https://phabricator.wikimedia.org/T18691
12  */
13 $( function () {
14     
15     if ( mw.config.get('wgIsMainPage') ) {
16         // Don't run on the main page, where we use section headings extensively
17         return
18     }
19 
20     $( '.mw-headline' ).each( function ( i, el ) {
21         var id = el.id,
22             $headline = $( el ),
23             $header = $headline.parent();
24 
25         // If MediaWiki core's anchor is here, or if ours is here already, skip.
26         if ( !id || $header.find( '.mw-headline-anchor,.mw-headline-headanchor').length ) {
27             return;
28         }
29 
30         $headline.removeAttr('id')
31         $header.addClass( 'mw-header' ).prepend(
32             $( '<a>' ).prop({
33                 id: id,
34                 href: '#' + id,
35                 title: 'Link to this section',
36                 className: 'mw-headline-headanchor'
37             })
38         );
39     } );
40 
41 })