MediaWiki:Common.js: Difference between revisions

From Rest of What I Know
Also do not display Template:BlogPreload
Rewrite to make listing look better
Line 1: Line 1:
// Reverse sorting for Category:Blog and hide Template:BlogPreload
// Reverse sorting for Category:Blog and hide Template:BlogPreload
$(document).ready(function() {
$(document).ready(function() {
     if (mw.config.get('wgCanonicalNamespace') === 'Category' &&  
     if (mw.config.get('wgCanonicalNamespace') === 'Category' &&
         mw.config.get('wgTitle') === 'Blog') {
         mw.config.get('wgTitle') === 'Blog') {
          
          
Line 7: Line 7:
         if ($categoryList.length) {
         if ($categoryList.length) {
             var $items = $categoryList.find('li');
             var $items = $categoryList.find('li');
           
 
             // Filter out Template:BlogPreload from display
             // Filter out Template:BlogPreload from display
             $items = $items.filter(function() {
             $items = $items.filter(function() {
                 var linkText = $(this).find('a').first().text();
                 var $a = $(this).find('a').first();
                var linkText = $a.text();
                 return linkText !== 'Template:BlogPreload';
                 return linkText !== 'Template:BlogPreload';
             });
             });
              
 
             // Sort remaining items in reverse order
            // 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) {
             $items.detach().sort(function(a, b) {
                 return $(b).text().localeCompare($(a).text());
                 return $(b).text().localeCompare($(a).text());
             });
             });
           
 
             // Replace the category list with filtered, sorted items
             // Replace the category list with filtered, formatted, sorted items
             $categoryList.empty().append($items);
             $categoryList.empty().append($items);
         }
         }
     }
     }
});
});

Revision as of 03:51, 23 September 2025

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