Jump to content

MediaWiki:Common.js

From Knowledge Base
Revision as of 13:39, 18 January 2025 by Chr1ss (talk | contribs)

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.
// Add 'csharp' class to all <pre> tags for easier targeting
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));

// Highlight comments FIRST
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;

    // Regex for matching multi-line comments (/* */)
    const regexMultiLineComments = /\/\*[\s\S]*?\*\//g;
    code = code.replace(regexMultiLineComments, match => {
        return `<span class="csharp-comment" data-protected="true">${match}</span>`;
    });

    // Regex for matching single-line comments (//)
    const regexSingleLineComments = /\/\/[^\n]*/g;
    code = code.replace(regexSingleLineComments, match => {
        return `<span class="csharp-comment" data-protected="true">${match}</span>`;
    });

    pre.innerHTML = code;
});

// Highlight keywords, types, and other patterns AFTER comments are protected
const csharpKeywords = [
    "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
    "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", 
    "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", 
    "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", 
    "new", "null", "object", "operator", "out", "override", "params", "private", "protected", 
    "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", 
    "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", 
    "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while"
];

document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;

    // Highlight keywords
    const regexKeywords = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    code = code.replace(regexKeywords, '<span class="csharp-keyword">$1</span>');

    // Highlight types
    const csharpTypes = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
    const regexTypes = new RegExp(`\\b(${csharpTypes.join("|")})\\b`, "g");
    code = code.replace(regexTypes, '<span class="csharp-type">$1</span>');

    // Highlight method names
    const regexMethods = /\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?=\()/g;
    code = code.replace(regexMethods, '<span class="csharp-method">$1</span>');

    // Highlight class and property names
    const regexClasses = /\b([A-Z][a-zA-Z0-9_]*)\b/g;
    code = code.replace(regexClasses, '<span class="csharp-class">$1</span>');

    pre.innerHTML = code;
});

// Ensure protected spans (comments) are skipped in all transformations
document.querySelectorAll('[data-protected="true"]').forEach(element => {
    element.removeAttribute('data-protected');
});