User:Dlrohrer2003/source-code-wikilinks.js
Appearance
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)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/** Source Code Wikilinks ******************************************************
*
* Description: Create links from wiki-formatted text in comments on User and
* MediaWiki namespace script 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 linkifyText(text) {
// All links without piped titles or category sort keys
text = text.replace(/\[\[\s*([^\|\]]+)\s*\]\]/ig, '[[<a href="/wiki/$1" title="$1">$1</a>]]');
// Links with piped titles, not including category links with piped sort keys
text = text.replace(/\[\[\s*(?!Category:)([^\|\]]+)\s*\|\s*([^\]]+)\s*\]\]/ig, '[[<a href="/wiki/$1" title="$1">$2</a>]]');
// Category links with piped sort keys
text = text.replace(/\[\[\s*(Category:[^\|\]]+)\s*\|\s*([^\]]+)\s*\]\]/ig, '[[<a href="/wiki/$1" title="$1">$1</a>|$2]]');
// Template links without arguments
text = text.replace(/\{\{\s*([^\|\}]+)\s*\}\}/ig, '{{<a href="/wiki/Template:$1" title="Template:$1">$1</a>}}');
// Template links with arguments
text = text.replace(/\{\{\s*([^\|\}]+)\s*\|\s*([^\}]+)\s*\}\}/ig, '{{<a href="/wiki/Template:$1" title="Template:$1">$1</a>|$2}}');
// Replace all whitespace in the href attribute with underscores
while (text.match(/href=\"[^\"\s]+\s+[^\"]+\"/)) {
text = text.replace(/(href=\"[^\"\s]+)\s+([^\"]+\")/, '$1_$2');
}
return text;
}
$( function() {
var conf = mw.config.get( [ 'wgPageName', 'wgNamespaceNumber' ] );
if ( conf.wgPageName.match(/\.css|\.js/i) && ( conf.wgNamespaceNumber === 2 || conf.wgNamespaceNumber === 8 ) ) {
var comments = $("pre span.cm, pre span.c1");
if (comments) {
for (var i = 0; i < comments.length; i++) {
$(comments[i]).html( linkifyText($(comments[i]).html()) );
}
}
}
});