MediaWiki:Common.js

From Rest of What I Know

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// Reverse sorting for Category:Blog; hide Template:BlogPreload; pretty link text; skip non-Mainspace
$(function() {
    if (mw.config.get('wgCanonicalNamespace') !== 'Category' ||
        mw.config.get('wgTitle') !== 'Blog') return;

    var $categoryList = $('#mw-pages');
    if (!$categoryList.length) return;

    var $items = $categoryList.find('li');

    $items = $items.filter(function() {
        var $a = $(this).find('a').first();
        var titleAttr = ($a.attr('title') || '').replace(/_/g, ' ');

        // Explicitly drop Template:BlogPreload
        if (titleAttr === 'Template:BlogPreload') return false;

        // Ensure it’s a mainspace page: ns=0
        // Use MediaWiki’s title parser
        var ns = mw.Title.newFromText(titleAttr).namespace;
        return ns === 0;
    });

    // Rewrite anchor text if matches Blog/YYYY-MM-DD/Title
    $items.each(function() {
        var $a = $(this).find('a').first();
        var titleAttr = ($a.attr('title') || $a.text() || '').replace(/_/g, ' ');
        var m = titleAttr.match(/^Blog\/(\d{4}-\d{2}-\d{2})\/(.+)$/);
        if (m) {
            $a.text(m[1] + ' ' + m[2]);
        }
        // otherwise leave as-is
    });

    // Sort by display text, reverse
    $items.detach().sort(function(a, b) {
        var ta = $(a).find('a').first().text();
        var tb = $(b).find('a').first().text();
        return tb.localeCompare(ta);
    });

    $categoryList.empty().append($items);
});