Merge "Enable clicking on wikilinks and external links in highlighted code"

This commit is contained in:
jenkins-bot 2024-09-09 13:29:52 +00:00 committed by Gerrit Code Review
commit f2d0dde88c
3 changed files with 66 additions and 7 deletions

View file

@ -38,9 +38,10 @@
"pygments.wrapper.less"
]
},
"ext.pygments.linenumbers": {
"ext.pygments.view": {
"scripts": [
"pygments.linenumbers.js"
"pygments.linenumbers.js",
"pygments.links.js"
],
"dependencies": [
"mediawiki.util"

View file

@ -197,10 +197,7 @@ class SyntaxHighlight extends ExtensionTagHandler implements
// Register CSS
$parser->getOutput()->addModuleStyles( self::getModuleStyles() );
if ( !empty( $args['linelinks'] ) ) {
$parser->getOutput()->addModules( [ 'ext.pygments.linenumbers' ] );
}
$parser->getOutput()->addModules( [ 'ext.pygments.view' ] );
return $result['html'];
}
@ -598,7 +595,7 @@ class SyntaxHighlight extends ExtensionTagHandler implements
$out = $status->getValue();
$parserOutput->addModuleStyles( self::getModuleStyles() );
$parserOutput->addModules( [ 'ext.pygments.linenumbers' ] );
$parserOutput->addModules( [ 'ext.pygments.view' ] );
$parserOutput->setText( $out );
// Inform MediaWiki that we have parsed this page and it shouldn't mess with it.

61
modules/pygments.links.js Normal file
View file

@ -0,0 +1,61 @@
/**
* Adapted from https://en.wiktionary.org/wiki/MediaWiki:Gadget-CodeLinks.js
* Original authors: Kephir, Erutuon
* License: CC-BY-SA 4.0
*/
$( () => {
// by John Gruber, from https://daringfireball.net/2010/07/improved_regex_for_matching_urls
const URLRegExp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’]))/i;
function processComment( node ) {
let wikilinkMatch, templateMatch, URLMatch;
const textNode = node.firstChild; // always a text node.
while (
( wikilinkMatch = /\[\[([^|{}[\]\n]+)?(?:\|.*?)?]]/.exec( textNode.data ) ) ||
( templateMatch = /\{\{([^|{}[\]\n#]+)(?=\||}})/i.exec( textNode.data ) ) ||
( URLMatch = URLRegExp.exec( textNode.data ) )
) {
const link = document.createElement( 'a' );
let start = ( wikilinkMatch || templateMatch || URLMatch ).index;
let linkText;
link.classList.add( 'code-link' );
if ( URLMatch ) {
const URL = URLMatch[ 0 ];
link.href = URL;
linkText = URL;
} else {
let fullPageName;
if ( wikilinkMatch ) {
linkText = wikilinkMatch[ 0 ];
fullPageName = wikilinkMatch[ 1 ];
} else if ( templateMatch ) {
const pageName = templateMatch[ 1 ];
linkText = pageName;
fullPageName = mw.config.get( 'wgFormattedNamespaces' )[ 10 ] + ':' + pageName;
link.title = fullPageName;
start += 2; // opening braces "{{"
}
link.href = mw.util.getUrl( fullPageName );
}
const beforeLink = textNode.data.slice( 0, Math.max( 0, start ) ),
afterLink = textNode.data.slice( Math.max( 0, start + linkText.length ) );
textNode.data = afterLink;
link.appendChild( document.createTextNode( linkText ) );
node.insertBefore( link, textNode );
node.insertBefore( document.createTextNode( beforeLink ), link );
}
}
const commentClasses = [ 'c', 'c1', 'cm' ];
Array.from( document.getElementsByClassName( 'mw-highlight' ) ).forEach( ( codeBlock ) => {
commentClasses.forEach( ( commentClass ) => {
Array.from( codeBlock.getElementsByClassName( commentClass ) ).forEach( processComment );
} );
} );
} );