/* Source Code Wikilinks
* =============================================================================
* Description: Create links from wiki-formatted text in comments on User,
* MediaWiki and Template namespace script and style pages.
* Idea originally proposed on
* [[WP:Village_pump_(technical)/Archive_84#Script_for_wikilinks_in_CSS.2FJS_pages]].
*
* Author: [[User:Dlrohrer2003]]
*
* Categories: [[Category:Wikipedia scripts]]
*/
( function ( mw, $ ) {
'use strict';
const conf = mw.config.get( [ 'wgPageContentModel' ] );
// If not on a source code page, return.
if ( conf.wgPageContentModel !== 'css' && conf.wgPageContentModel !== 'sanitized-css' && conf.wgPageContentModel !== 'javascript' ) {
return false;
}
const reLink = /\[\[\s*([^\|\]]+)\s*\]\]/ig;
const reLinkPiped = /\[\[\s*([^\|\]]+)\s*\|\s*([^\]]+)\s*\]\]/ig;
const reTemplate = /\{\{\s*(?!subst:|#invoke:)([^\|\}]+)\s*\}\}/ig;
const reTemplateSubst = /\{\{\s*(subst:)([^\|\}]+)\s*\}\}/ig;
const reModuleInvoke = /\{\{\s*(#invoke:)([^\|\}]+)\s*\}\}/ig;
const reTemplateArg = /\{\{\s*(?!subst:|#invoke:)([^\|\}]+)\s*\|\s*([^\}]+)\s*\}\}/ig;
const reTemplateArgSubst = /\{\{\s*(subst:)([^\|\}]+)\s*\|\s*([^\}]+)\s*\}\}/ig;
const reModuleArgInvoke = /\{\{\s*(#invoke:)([^\|\}]+)\s*\|\s*([^\}]+)\s*\}\}/ig;
const reReplaceWhitespace = /(href=\"[^\"\s]+)\s+([^\"]+\")/;
function linkifyText( comment ) {
let text = comment.innerHTML;
// All links without piped titles or category sort keys
text = text.replace( reLink, '[[<a href="/wiki/$1" title="$1">$1</a>]]' );
// Links with piped titles and category links with piped sort keys
text = text.replace( reLinkPiped, '[[<a href="/wiki/$1" title="$1">$1</a>|$2]]' );
// Template links without arguments
text = text.replace( reTemplate, '{{<a href="/wiki/Template:$1" title="Template:$1">$1</a>}}' );
text = text.replace( reTemplateSubst, '{{$1<a href="/wiki/Template:$2" title="Template:$2">$2</a>}}' );
text = text.replace( reModuleInvoke, '{{$1<a href="/wiki/Module:$2" title="Module:$2">$2</a>}}' );
// Template links with arguments
text = text.replace( reTemplateArg, '{{<a href="/wiki/Template:$1" title="Template:$1">$1</a>|$2}}' );
text = text.replace( reTemplateArgSubst, '{{$1<a href="/wiki/Template:$2" title="Template:$2">$2</a>|$3}}' );
text = text.replace( reModuleArgInvoke, '{{$1<a href="/wiki/Module:$2" title="Module:$2">$2</a>|$3}}' );
// Replace all whitespace in the href attribute with underscores
while ( text.match( reReplaceWhitespace ) ) {
text = text.replace( reReplaceWhitespace, '$1_$2' );
}
comment.innerHTML = text;
}
document
.querySelectorAll( 'pre .cm, pre .c1, pre .c' )
.forEach( linkifyText );
return true;
})( mediaWiki, jQuery );