User:Majr/codeLinks.js

$( function { 'use strict';

var interwikiTable; // Don't want to delay adding to the hook, otherwise it might be called // on some new content before this even runs, and the old content would // miss out on links, so we'll use a manual deferred. var interwikiRequest = $.Deferred; mw.hook( 'wikipage.content' ).add( function( $content ) {	var $code = $content.find( '.mw-code' );	if ( window.mcw && mcw.onlyLinkComment ) {		$code = $code.find( '.comment' );	}	if ( !$code.length ) {		return;	};	if ( !interwikiTable ) {		mw.loader.using( 'mediawiki.api', function { new mw.Api.get( {				action:'query',				meta:'siteinfo',				siprop:'interwikimap'			} ).done( function( data ) {				interwikiTable = data.query.interwikimap.reduce( function( obj, val ) { obj[val.prefix] = val.url; return obj; }, {} );				interwikiRequest.resolve;			} ); } );	}	interwikiRequest.done( function { $code.html( function( _, html ) {			return html.replace( /(?:\[\[([^\[\]#<>\|{}\n]*?)\]\]|((\w*?:)?(mailto:|MAILTO:|\/\/)[\w\-\._~@&]+\.[\w\-\._~@&]+(:\d+)?(\/[\w\-\._~\/&:]*)?(\?[\w\-\._~=&:\/;]*)?(#[\w\-\._~&:]*)?))/g, function( m, wiki, ext ) { if ( wiki ) { var parts = wiki.split( ':' ), url = '/' + wiki; parts[1] = parts.slice( 1 ).join( ':' ); if ( interwikiTable[parts[0].toLowerCase] ) { url = interwikiTable[parts[0].toLowerCase].replace( /\$1/, parts[1] ); }						return '' + mw.html.escape( wiki ) + ''; } else if ( ext ) { return '' + ext + ''; }			} );		} );	} ); } );

} );