
This is a script thatmakes syntax stand out colorfully in the edit box. Unlike other syntax highlighter scripts such aswikEd,AceWikiEditor, andCodeMirror, this one:
Please be sure to read the "§ Known issues" section below before installing the script.
If the syntax highlighter is available as agadget on your wiki, go to your preferences and enable it.
If installation as a gadget is not available, add these lines to your common.js page:
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');
For example, if the English Wikipedia did not provide the syntax highlighter as a gadget, you would add it to
https://en.wikipedia.org/wiki/User:Your_Username/common.js
On your wiki create the pageMediaWiki:Gadget-DotsSyntaxHighlighter.js with the code
mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');
Then add to the pageMediaWiki:Gadgets-definition a new line
*DotsSyntaxHighlighter[ResourceLoader]|DotsSyntaxHighlighter.js
and create the pageMediaWiki:Gadget-DotsSyntaxHighlighter with the text
[[mw:User:Remember the dot/Syntax highlighter|Syntax highlighter]]: Make syntax stand out colourfully in the edit box.
On your wiki's MediaWiki:Common.js page add the lines
//syntax highlighter mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');add to the pageMediaWiki:Gadgets-definition a new line
* DotsSyntaxHighlighter[ResourceLoader]|DotsSyntaxHighlighter.js
and create the pageMediaWiki:Gadget-DotsSyntaxHighlighter with the text
[https://www.mediawiki.org/wiki/User:Remember_the_dot/Syntax_highlighter Syntax highlighter]: Make syntax stand out colourfully in the edit box.
MediaWiki 1.22 or later is required.
<p> tag you end it with</p>, and use<br/> instead of<br>.'''apostrophe italic'' or''italic apostrophe'''—it considers them invalid syntax. I suggest using'<i>apostrophe italic</i> and<i>italic apostrophe'</i> instead.<pre> tags created by putting a space at the beginning of a line are not highlighted. This is because the highlighter is not smart enough to tell when a space at the beginning of a line is simply whitespace inside a template.__NOTOC__ are not highlighted.{{fullurl:}} or templates are not highlighted. You will see[{{fullurl:My page}} external link] instead of[{{fullurl:My page}} external link].It's easy to change the highlighter to use different colors or to not highlight certain syntaxes. The following color customizations are available:
| defaultColor | for recognized syntax elements that don't have a specific color defined |
| boldOrItalicColor | '''bold'''''italic'' |
| wikilinkColor | [[wikilink]] |
| externalLinkColor | [http:// named external link], [http://numbered-external-link.com], http://bare-external-link.com |
| headingColor | ==Heading==, ;Small heading |
| listOrIndentColor | * unordered list, # ordered list, : indent |
| signatureColor | ~~~~ |
| tableColor | {| table |} |
| templateColor | {{template}} |
| parameterColor | {{{template parameter}}} |
| hrColor | ---- |
| tagColor | <tag>...</tag>, <tag/> |
| commentColor | <!-- comment --> |
| entityColor | &entity; |
| backgroundColor | background color of the editing textbox |
| foregroundColor | color of text in the editing textbox |
For example, to make wikilinks cyan and external links orange, put the following in your common.js (if youinstalled the highlighter as a gadget, omit the first two lines or your web browser will freeze):
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');syntaxHighlighterConfig={externalLinkColor:"#FFCC66",//orangewikilinkColor:"#E6FFFF",//cyan}
To not highlight a syntax, set its color to"". For example, to disable external link highlighting:
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');syntaxHighlighterConfig={externalLinkColor:"",}
To not highlight any syntax except those you explicitly want, setdefaultColor to"" and set the color of each syntax you want to highlight. If you just want the usual color, set the color to"normal". For example, to only highlight tags:
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');syntaxHighlighterConfig={defaultColor:"",tagColor:"normal",}
You can specify atimeout that replaces the default 20ms timeout. For example, if you're OK with sluggishness as you type and you want to disable highlighting only if it takes more than 30ms, put the following in your common.js:
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');syntaxHighlighterConfig={timeout:30,}
If youinstalled the highlighter as a gadget, omit the first two lines or your web browser will freeze.
Several tags, such as<math> and<source>, do not use wikitext and consequently, no syntax is highlighted inside of these tags. You can customize the list of source-like tags by settingsourceTags to an array of tag names. For example, to disable syntax highlighting within the hypothetical<foo> tag in addition to the defaults, put the following in your common.js:
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');syntaxHighlighterConfig={sourceTags:["math","syntaxhighlight","source","timeline","hiero","foo"],}
Other tags, such as<pre> and<nowiki>, do not use wikitext but do process character entities ( for example). The list of nowiki-like tags is controlled by thenowikiTags property. For example, to highlight only character entities within the<abbr> tag, put the following in your common.js:
//syntax highlightermw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');syntaxHighlighterConfig={nowikiTags:["nowiki","pre","abbr"],}
In both examples, if youinstalled the highlighter as a gadget, omit the first two lines or your web browser will freeze.
To ignore unclosed<br> and<hr> tags that would otherwise turn the rest of the page pink, you can add the following to your common.js file. This will affect performance; the author recommends using the explicitly closed forms<br/> and<hr/> instead.
syntaxHighlighterConfig={voidTags:["br","hr","BR","HR","wbr","WBR"],}
All properties that can be specified in thesyntaxHighlighterConfig variable can also be specified in thesyntaxHighlighterSiteConfig variable.syntaxHighlighterSiteConfig is intended for site defaults andsyntaxHighlighterConfig for user preferences. Properties ofsyntaxHighlighterConfig take precendence over properties ofsyntaxHighlighterSiteConfig, and properties ofsyntaxHighlighterSiteConfig take precedence over builtin defaults.
For example, if your site does not have any MediaWiki extensions installed, you might want to remove support for source-like tags. To do so, add the following to the end ofMediaWiki:Gadget-DotsSyntaxHighlighter:
syntaxHighlighterSiteConfig={sourceTags:[],}
Whenreporting bugs to me, please include:
To reduce download time and because theResourceLoader does not automaticallyminify scripts imported from other wikis,MediaWiki:Gadget-DotsSyntaxHighlighter.js itself is kept in minified form (by pasting the source code intojscompress.com and copying the result). The actual source code is available atUser:Remember the dot/Syntax highlighter.js.
This script creates a background div, named wpTextbox0, that is inserted behind wpTextbox1, the editing textbox. wpTextbox0 and wpTextbox1 are synchronized in style, and the background of wpTextbox1 is made transparent so that wpTextbox0 shows through. Then, blocks of text are added to wpTextbox0 as span elements. The text on the blocks is transparent, but the backgrounds of the blocks are colored. By using the same text in wpTextbox0 as wpTextbox1, any positioning anomalies from dynamically composed characters are eliminated. Because wpTextbox0 and wpTextbox1 are (theoretically) perfectly synchronized, the colored backgrounds appear to the user as though they have been added directly to wpTextbox1.
The text in wpTextbox0 is not actually added to the textContents of the span elements. Instead, it is added to the ::after pseudo-elements of each span using a dynamically generated CSS stylesheet. This avoids problems when trying to use the browser's find-in-page feature, because if textContent were used instead of CSS content, the browser would perceive both the real text in wpTextbox1 and the transparent text in wpTextbox0 to be on the page.