Jump to content

MediaWiki:Common.js: Difference between revisions

From Knowledge Base
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
// Load Prism.js (ensure correct path)
// Add csharp class to all <pre> tags
importScriptURI('/prism.js'); // Adjust if necessary
document.querySelectorAll("pre").forEach(pre => pre.classList.add("csharp"));


// Load Prism.css (ensure correct path)
// Define C# keywords
var link = document.createElement('link');
const csharpKeywords = [
link.rel = 'stylesheet';
    "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked",
link.type = 'text/css';
    "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum",
link.href = '/prism.css';  // Adjust if necessary
    "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach",
document.head.appendChild(link);
    "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"
];


// Optional: log to check if Prism.js is loaded
// Highlight C# keywords
if (typeof Prism !== "undefined") {
document.querySelectorAll("pre.csharp").forEach(pre => {
     console.log("Prism.js loaded successfully.");
     let code = pre.innerHTML;
} else {
    const regex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    console.error("Prism.js is not loaded.");
    code = code.replace(regex, '<span class="csharp-keyword">$1</span>');
}
     pre.innerHTML = code;
 
// Initialize syntax highlighting after page load
$(document).ready(function() {
    if (typeof Prism !== "undefined") {
        Prism.highlightAll();
        console.log("Prism.js executed successfully on all code blocks.");
     } else {
        console.error("Prism.js not found when trying to highlight code.");
    }
});
});

Revision as of 11:00, 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
document.querySelectorAll("pre.csharp").forEach(pre => {
    let code = pre.innerHTML;
    const regex = new RegExp(`\\b(${csharpKeywords.join("|")})\\b`, "g");
    code = code.replace(regex, '<span class="csharp-keyword">$1</span>');
    pre.innerHTML = code;
});