Open main menu

Snippets/Hide prefix in category

< Snippets
How to use Snippets
List of Snippets
Crystal Clear action run.png
Hide prefix in category
Language(s): JavaScript
Compatible with: MediaWiki 1.17+ (Vector)

DescriptionEdit

Hide a prefix in the pagenames of category members. Use a Template containing something similar to this:

<div id="mw-cat-hideprefix" style="display: none;">{{{1|}}}</div>

And include it like {{MyTemplate|your prefix here}}. If the first parameter is left blank, the script will use the title of the category as prefix (useful for categories like Category:Snippets with JavaScript). You may want to redefine that as for your needs.

CodeEdit

/**
 * Hide prefix in category
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
 * @rev 2019-03-23
 */
$( function () {
    var prefix = ( $( '#mw-cat-hideprefix' ).text().trim() || mw.config.get( 'wgTitle' ) + '/' );
    $( '#mw-pages a' ).text( function ( i, val ) {
        return val.slice( 0, prefix.length ) === prefix ? val.slice( prefix.length ) : val;
    } );
} );