MediaWiki:Common.js

From Rest of What I Know
Revision as of 03:53, 23 September 2025 by Roshan (talk | contribs) (Fix the matching for blog rewriting)

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
$(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');

    // Filter out Template:BlogPreload using title/text normalized
    $items = $items.filter(function() {
        var $a = $(this).find('a').first();
        var full = ($a.attr('title') || $a.text() || '').replace(/_/g, ' ');
        return full !== 'Template:BlogPreload';
    });

    // Rewrite anchor text: "Blog/YYYY-MM-DD/Title" -> "YYYY-MM-DD Title"
    $items.each(function() {
        var $a = $(this).find('a').first();
        var raw = ($a.attr('title') || $a.text() || '').replace(/_/g, ' ');
        var m = raw.match(/^Blog\/(\d{4}-\d{2}-\d{2})\/(.+)$/);
        if (m) $a.text(m[1] + ' ' + m[2]); // else leave original text unchanged
    });

    // Sort by anchor 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);
});