MediaWiki:Common.js: Difference between revisions
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
document.querySelectorAll("pre").forEach(pre => { | document.querySelectorAll("pre").forEach(pre => { | ||
pre.classList.add("csharp"); // Add 'csharp' class for | pre.classList.add("csharp"); // Add the 'csharp' class for targeting | ||
// Get the | // Get the raw code content | ||
let code = pre. | let code = pre.textContent; | ||
// Highlight single-line comments (//) | // Highlight single-line comments (//) | ||
| Line 14: | Line 14: | ||
const keywords = [ | const keywords = [ | ||
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", | "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", | ||
"class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", | "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", | ||
"enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", | "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", | ||
"foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", | "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", | ||
"long", "namespace", "new", "null", "object", "operator", "out", "override", "params", | "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", | ||
"private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", | "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", | ||
"sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", | "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", | ||
"try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", | "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", | ||
"void", "volatile", "while" | "void", "volatile", "while" | ||
]; | ]; | ||
| Line 26: | Line 26: | ||
code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>'); | code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>'); | ||
// Highlight types | // Highlight data types | ||
const types = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"]; | const types = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"]; | ||
const typeRegex = new RegExp(`\\b(${types.join("|")})\\b`, "g"); | const typeRegex = new RegExp(`\\b(${types.join("|")})\\b`, "g"); | ||
| Line 32: | Line 32: | ||
// Highlight method names (e.g., "MyMethod()") | // Highlight method names (e.g., "MyMethod()") | ||
code = code.replace(/(\b[a-zA-Z_][a-zA-Z0-9_]*)(?=\()/g, '<span class="csharp-method">$1</span>'); | code = code.replace(/(\b[a-zA-Z_][a-zA-Z0-9_]*)(?=\s*\()/g, '<span class="csharp-method">$1</span>'); | ||
// Highlight class names and properties (start with uppercase letters) | // Highlight class names and properties (start with uppercase letters) | ||
code = code.replace(/\b([A-Z][a-zA-Z0-9_]*)\b/g, '<span class="csharp-class">$1</span>'); | code = code.replace(/\b([A-Z][a-zA-Z0-9_]*)\b/g, '<span class="csharp-class">$1</span>'); | ||
// | // Safely apply the modified HTML back to the <pre> element | ||
pre.innerHTML = code; | pre.innerHTML = code; | ||
}); | }); | ||
Revision as of 13:41, 18 January 2025
document.querySelectorAll("pre").forEach(pre => {
pre.classList.add("csharp"); // Add the 'csharp' class for targeting
// Get the raw code content
let code = pre.textContent;
// Highlight single-line comments (//)
code = code.replace(/(\/\/.*?$)/gm, '<span class="csharp-comment">$1</span>');
// Highlight multi-line comments (/* */)
code = code.replace(/(\/\*[\s\S]*?\*\/)/g, '<span class="csharp-comment">$1</span>');
// Highlight keywords
const keywords = [
"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"
];
const keywordRegex = new RegExp(`\\b(${keywords.join("|")})\\b`, "g");
code = code.replace(keywordRegex, '<span class="csharp-keyword">$1</span>');
// Highlight data types
const types = ["int", "long", "float", "double", "decimal", "char", "string", "bool", "byte", "object", "void"];
const typeRegex = new RegExp(`\\b(${types.join("|")})\\b`, "g");
code = code.replace(typeRegex, '<span class="csharp-type">$1</span>');
// Highlight method names (e.g., "MyMethod()")
code = code.replace(/(\b[a-zA-Z_][a-zA-Z0-9_]*)(?=\s*\()/g, '<span class="csharp-method">$1</span>');
// Highlight class names and properties (start with uppercase letters)
code = code.replace(/\b([A-Z][a-zA-Z0-9_]*)\b/g, '<span class="csharp-class">$1</span>');
// Safely apply the modified HTML back to the <pre> element
pre.innerHTML = code;
});