Snippets/Hide prefix in category

From MediaWiki.org
Jump to navigation Jump to search
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)

Description[edit]

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|{{PAGENAME}}/}}}</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.

Code[edit]

/**
 * Hide prefix in category
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Hide_prefix_in_category
 * @rev 2020-04-28
 */
$( function () {
    var prefix = $( '#mw-cat-hideprefix' ).text().trim();
    if ( prefix ) {
        $( '#mw-pages a' ).text( function ( i, val ) {
            return val.slice( 0, prefix.length ) === prefix ? val.slice( prefix.length ) : val;
        } );
    }
} );