User:Awesome Aasim/doctaghighlighter.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(function () {
	mw.loader.load("https://en.wikipedia.org/wiki/User:Awesome_Aasim/doctaghighlighter.css?action=raw&ctype=text/css", "text/css");
	var doctags = {
		"jsdoc": ['abstract', 'virtual', 'access', 'alias', 'async', 'augments', 'extends', 'author', 'borrows', 'class', 'constructor', 'classdesc', 'constant', 'const', 'constructs', 'copyright', 'default', 'defaultvalue', 'deprecated', 'description', 'desc', 'enum', 'event', 'example', 'exports', 'external', 'host', 'file', 'fileoverview', 'overview', 'fires', 'emits', 'function', 'func', 'method', 'generator', 'global', 'hideconstructor', 'ignore', 'implements', 'inheritdoc', 'inner', 'instance', 'interface', 'kind', 'lends', 'license', 'listens', 'member', 'var', 'memberof', 'mixes', 'mixin', 'module', 'name', 'namespace', 'override', 'package', 'param', 'arg', 'argument', 'private', 'property', 'prop', 'protected', 'public', 'readonly', 'requires', 'returns', 'return', 'see', 'since', 'static', 'summary', 'this', 'throws', 'exception', 'todo', 'tutorial', 'type', 'typedef', 'variation', 'version', 'yields', 'yield', 'link', 'linkcode', 'linkplain'],
		"ldoc": ['module', 'classmod', 'submodule', 'script', 'author', 'license', 'release', 'function', 'lfunction', 'param', 'return', 'raise', 'local', 'see', 'usage', 'field', 'section', 'type', 'within', 'fixme', 'todo', 'warning', 'tparam', 'treturn', 'alias', 'export', 'set', 'class', 'name', 'charset']
	};
	var tagRegex = {
		"jsdoc": new RegExp("@(" + doctags["jsdoc"].join("|") + ")", "g"),
		"ldoc": new RegExp("(?<=(--|---)\\s*)@(" + doctags["ldoc"].join("|") + ")", "s")
	}
	$(document).ready(function () {
		// For JS color only in multiline (/**/) comments
		$(".mw-highlight-lang-js").find(".cm").each(function () {
			let txt = $(this).html();
			txt = txt.replace(tagRegex["jsdoc"], function(match) {
				return `<span class="doctag">${match}</span>`;
			});
			$(this).html(txt);
		});
		// For LDoc color only in single line (--) comments
		$(".mw-highlight-lang-lua").find(".c1").each(function () {
			let txt = $(this).html();
			txt = txt.replace(tagRegex["ldoc"], function(match) {
				return `<span class="doctag">${match}</span>`;
			});
			$(this).html(txt);
		});
	});
});