User:Dlrohrer2003/source-code-wikilinks.js

/* 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. * *     Author: User:Dlrohrer2003 * * Categories: */

( 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, '$1' );

// Links with piped titles and category links with piped sort keys text = text.replace( reLinkPiped, '$2' );

// Template links without arguments text = text.replace( reTemplate, '' ); text = text.replace( reTemplateSubst, '' ); text = text.replace( reModuleInvoke, '' );

// Template links with arguments text = text.replace( reTemplateArg, '' ); text = text.replace( reTemplateArgSubst, '' ); text = text.replace( reModuleArgInvoke, '' );

// 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 );