Jump to content

MediaWiki:Common.js: Difference between revisions

From Knowledge Base
No edit summary
No edit summary
Tag: Reverted
Line 14: Line 14:
];
];


// Highlight C# keywords
// Highlight C# keywords and add more features (types, variables, comments, strings)
document.querySelectorAll("pre.csharp").forEach(pre => {
document.querySelectorAll("pre.csharp").forEach(pre => {
     let code = pre.innerHTML;
     let code = pre.innerHTML;
     const regex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
   
     code = code.replace(regex, '<span class="csharp-keyword">$1</span>');
    // Highlight C# keywords
     const keywordRegex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
     code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>');
   
    // Highlight types (int, double, string, etc.)
    const typeRegex = /\b(int|double|string|bool|float|long|char|decimal)\b/g;
    code = code.replace(typeRegex, '<span class="csharp-type">$1</span>');
   
    // Highlight variable names (anything that is not a keyword or type)
    const variableRegex = /\b([a-zA-Z_]\w*)\b/g;
    code = code.replace(variableRegex, function(match, group1) {
        if (!csharpKeywords.includes(group1) && !["int", "double", "string", "bool", "float", "long", "char", "decimal"].includes(group1)) {
            return '<span class="csharp-variable">' + group1 + '</span>';
        }
        return match;
    });
   
    // Highlight single-line comments (//)
    const singleCommentRegex = /\/\/[^\n]*/g;
    code = code.replace(singleCommentRegex, '<span class="csharp-comment">$&</span>');
   
    // Highlight multi-line comments (/* */)
    const multiCommentRegex = /\/\*[\s\S]*?\*\//g;
    code = code.replace(multiCommentRegex, '<span class="csharp-comment">$&</span>');
   
    // Highlight strings (inside double quotes)
    const stringRegex = /"([^"]*)"/g;
    code = code.replace(stringRegex, '<span class="csharp-string">"$1"</span>');
   
     pre.innerHTML = code;
     pre.innerHTML = code;
});
});

Revision as of 11:05, 18 January 2025

// Add csharp class to all <pre> tags
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));

// Define C# keywords
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"
];

// Highlight C# keywords and add more features (types, variables, comments, strings)
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;
    
    // Highlight C# keywords
    const keywordRegex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>');
    
    // Highlight types (int, double, string, etc.)
    const typeRegex = /\b(int|double|string|bool|float|long|char|decimal)\b/g;
    code = code.replace(typeRegex, '<span class="csharp-type">$1</span>');
    
    // Highlight variable names (anything that is not a keyword or type)
    const variableRegex = /\b([a-zA-Z_]\w*)\b/g;
    code = code.replace(variableRegex, function(match, group1) {
        if (!csharpKeywords.includes(group1) && !["int", "double", "string", "bool", "float", "long", "char", "decimal"].includes(group1)) {
            return '<span class="csharp-variable">' + group1 + '</span>';
        }
        return match;
    });
    
    // Highlight single-line comments (//)
    const singleCommentRegex = /\/\/[^\n]*/g;
    code = code.replace(singleCommentRegex, '<span class="csharp-comment">$&</span>');
    
    // Highlight multi-line comments (/* */)
    const multiCommentRegex = /\/\*[\s\S]*?\*\//g;
    code = code.replace(multiCommentRegex, '<span class="csharp-comment">$&</span>');
    
    // Highlight strings (inside double quotes)
    const stringRegex = /"([^"]*)"/g;
    code = code.replace(stringRegex, '<span class="csharp-string">"$1"</span>');
    
    pre.innerHTML = code;
});