Skip to content

Commit

Permalink
morebits: Ignore file/category embeds when removing links (wikimedia-…
Browse files Browse the repository at this point in the history
…gadgets#239)

File: and Category: links (without a leading colon) should not be
removed. Fixes wikimedia-gadgets#231.
  • Loading branch information
kevinji committed Sep 4, 2018
1 parent cd45282 commit 83fff83
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions morebits.js
Expand Up @@ -3024,8 +3024,14 @@ Morebits.wikitext.page.prototype = {
removeLink: function( link_target ) {
var first_char = link_target.substr( 0, 1 );
var link_re_string = "[" + first_char.toUpperCase() + first_char.toLowerCase() + ']' + RegExp.escape( link_target.substr( 1 ), true );
var link_simple_re = new RegExp( "\\[\\[:?(" + link_re_string + ")\\]\\]", 'g' );
var link_named_re = new RegExp( "\\[\\[:?" + link_re_string + "\\|(.+?)\\]\\]", 'g' );

// Files and Categories become links with a leading colon.
// e.g. [[:File:Test.png]]
var special_ns_re = /^(?:[Ff]ile|[Ii]mage|[Cc]ategory):/;
var colon = special_ns_re.test( link_target ) ? ':' : '';

var link_simple_re = new RegExp( "\\[\\[" + colon + "(" + link_re_string + ")\\]\\]", 'g' );
var link_named_re = new RegExp( "\\[\\[" + colon + link_re_string + "\\|(.+?)\\]\\]", 'g' );
this.text = this.text.replace( link_simple_re, "$1" ).replace( link_named_re, "$1" );
},
commentOutImage: function( image, reason ) {
Expand Down

0 comments on commit 83fff83

Please sign in to comment.