Merge "links: show links in live previews and preserve fragments in links"

This commit is contained in:
jenkins-bot 2024-10-29 15:51:23 +00:00 committed by Gerrit Code Review
commit 02203d171c
2 changed files with 53 additions and 49 deletions

View file

@ -43,7 +43,7 @@ $( () => {
title = mw.Title.newFromText( pageName, 10 ); title = mw.Title.newFromText( pageName, 10 );
} }
if ( title ) { if ( title ) {
link.href = mw.util.getUrl( title.toText() ); link.href = title.getUrl();
link.title = title.toText(); link.title = title.toText();
} }
if ( link.href ) { if ( link.href ) {
@ -61,7 +61,9 @@ $( () => {
} }
const commentClasses = [ 'c', 'c1', 'cm' ]; const commentClasses = [ 'c', 'c1', 'cm' ];
Array.from( document.getElementsByClassName( 'mw-highlight' ) ).forEach( ( codeBlock ) => {
mw.hook( 'wikipage.content' ).add( ( $content ) => {
$content.find( '.mw-highlight' ).get().forEach( ( codeBlock ) => {
commentClasses.forEach( ( commentClass ) => { commentClasses.forEach( ( commentClass ) => {
Array.from( codeBlock.getElementsByClassName( commentClass ) ).forEach( ( node ) => { Array.from( codeBlock.getElementsByClassName( commentClass ) ).forEach( ( node ) => {
processComment( node.firstChild, node ); processComment( node.firstChild, node );
@ -70,3 +72,4 @@ $( () => {
} ); } );
} ); } );
} );

View file

@ -1,12 +1,8 @@
$( () => { $( () => {
function addLink( element, title ) {
const classes = {
singleQuoteString: 's1', doubleQuoteString: 's2'
};
function addLink( element, page ) {
const link = document.createElement( 'a' ); const link = document.createElement( 'a' );
link.href = mw.util.getUrl( page ); link.href = title.getUrl();
link.title = title.toText();
// put text node from element inside link // put text node from element inside link
const firstChild = element.firstChild; const firstChild = element.firstChild;
if ( !( firstChild instanceof Text ) ) { if ( !( firstChild instanceof Text ) ) {
@ -23,8 +19,12 @@ $( () => {
'mw.loadJsonData': () => true 'mw.loadJsonData': () => true
}; };
const stringNodes = Array.from( document.getElementsByClassName( classes.singleQuoteString ) ) mw.hook( 'wikipage.content' ).add( ( $content ) => {
.concat( Array.from( document.getElementsByClassName( classes.doubleQuoteString ) ) );
// s1 is the class applied by Pygments to single-quoted strings
// s2 is the class applied by Pygments to double-quoted strings
const stringNodes = $content.find( '.s1' ).get()
.concat( $content.find( '.s2' ).get() );
stringNodes.forEach( ( node ) => { stringNodes.forEach( ( node ) => {
if ( !node.nextElementSibling || if ( !node.nextElementSibling ||
@ -59,9 +59,10 @@ $( () => {
const condition = parametersToLink[ invocation ]; const condition = parametersToLink[ invocation ];
const title = mw.Title.newFromText( page ); const title = mw.Title.newFromText( page );
if ( title && condition( title ) ) { if ( title && condition( title ) ) {
addLink( node, title.toText() ); addLink( node, title );
} }
} ); } );
} ); } );
} ); } );
} );