User:Dlrohrer2003/source-code-wikilinks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Dlrohrer2003/source-code-wikilinks. |
/* 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]]
*/
/* *** NOTICE ***
This script is deprecated and will no longer be maintained. I am leaving it up for historical reference.
see: [[phab:T368166]]
*/
( 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 );