MediaWiki:Common.js

From Rest of What I Know
Revision as of 03:51, 23 September 2025 by Roshan (talk | contribs) (Rewrite to make listing look better)

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 and hide Template:BlogPreload
$(document).ready(function() {
    if (mw.config.get('wgCanonicalNamespace') === 'Category' &&
        mw.config.get('wgTitle') === 'Blog') {
        
        var $categoryList = $('#mw-pages');
        if ($categoryList.length) {
            var $items = $categoryList.find('li');

            // Filter out Template:BlogPreload from display
            $items = $items.filter(function() {
                var $a = $(this).find('a').first();
                var linkText = $a.text();
                return linkText !== 'Template:BlogPreload';
            });

            // Rewrite link text from "Blog/2025-09-25/Whatever The Page Is"
            // to "2025-09-25 Whatever The Page Is"
            $items.each(function() {
                var $a = $(this).find('a').first();
                var text = $a.text(); // e.g. "Blog/2025-09-25/Whatever The Page Is"
                var match = text.match(/^Blog\/(\d{4}-\d{2}-\d{2})\/(.+)$/);
                if (match) {
                    $a.text(match[1] + ' ' + match[2]);
                }
            });

            // Sort remaining items in reverse order by the new text
            $items.detach().sort(function(a, b) {
                return $(b).text().localeCompare($(a).text());
            });

            // Replace the category list with filtered, formatted, sorted items
            $categoryList.empty().append($items);
        }
    }
});