| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
$( () => { |
| 8 |
|
| 9 |
const URLRegExp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()[\]{};:'".,<>?«»“”‘’]))/i; |
| 10 |
|
| 11 |
const wikilinkRegExp = /\[\[([^|{}[\]\n]+)?(?:\|.*?)?]]/; |
| 12 |
const templateRegExp = /\{\{([^|{}[\]\n#]+)(?=\||}})/; |
| 13 |
|
| 14 |
function processComment( textNode, node ) { |
| 15 |
let wikilinkMatch, templateMatch, URLMatch; |
| 16 |
|
| 17 |
if ( |
| 18 |
( wikilinkMatch = wikilinkRegExp.exec( textNode.data ) ) || |
| 19 |
( templateMatch = templateRegExp.exec( textNode.data ) ) || |
| 20 |
( URLMatch = URLRegExp.exec( textNode.data ) ) |
| 21 |
) { |
| 22 |
const link = document.createElement( 'a' ); |
| 23 |
let start = ( wikilinkMatch || templateMatch || URLMatch ).index; |
| 24 |
let linkText, title; |
| 25 |
link.classList.add( 'code-link' ); |
| 26 |
|
| 27 |
if ( URLMatch ) { |
| 28 |
let url = URLMatch[ 0 ]; |
| 29 |
|
| 30 |
if ( !/^https?:/i.test( url ) ) { |
| 31 |
url = '//' + url; |
| 32 |
} |
| 33 |
|
| 34 |
link.href = url; |
| 35 |
linkText = URLMatch[ 0 ]; |
| 36 |
} else if ( wikilinkMatch && wikilinkMatch[ 1 ] ) { |
| 37 |
linkText = wikilinkMatch[ 0 ]; |
| 38 |
title = mw.Title.newFromText( wikilinkMatch[ 1 ] ); |
| 39 |
} else if ( templateMatch ) { |
| 40 |
const pageName = templateMatch[ 1 ]; |
| 41 |
start += 2; |
| 42 |
linkText = pageName; |
| 43 |
title = mw.Title.newFromText( pageName, 10 ); |
| 44 |
} |
| 45 |
if ( title ) { |
| 46 |
link.href = mw.util.getUrl( title.toText() ); |
| 47 |
link.title = title.toText(); |
| 48 |
} |
| 49 |
if ( link.href ) { |
| 50 |
const textBeforeLink = textNode.data.slice( 0, Math.max( 0, start ) ), |
| 51 |
textAfterLink = textNode.data.slice( Math.max( 0, start + linkText.length ) ); |
| 52 |
|
| 53 |
textNode.data = textAfterLink; |
| 54 |
link.appendChild( document.createTextNode( linkText ) ); |
| 55 |
node.insertBefore( link, textNode ); |
| 56 |
const beforeTextNode = node.insertBefore( document.createTextNode( textBeforeLink ), link ); |
| 57 |
processComment( beforeTextNode, node ); |
| 58 |
processComment( textNode, node ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
const commentClasses = [ 'c', 'c1', 'cm' ]; |
| 64 |
Array.from( document.getElementsByClassName( 'mw-highlight' ) ).forEach( ( codeBlock ) => { |
| 65 |
commentClasses.forEach( ( commentClass ) => { |
| 66 |
Array.from( codeBlock.getElementsByClassName( commentClass ) ).forEach( ( node ) => { |
| 67 |
processComment( node.firstChild, node ); |
| 68 |
} ); |
| 69 |
} ); |
| 70 |
} ); |
| 71 |
|
| 72 |
} ); |