User:Remember the dot/Syntax highlighter/zh

Jump to navigation Jump to search
Dot's Syntax Highlighter 2012-12-06.png


  • 在输入时即时自动高亮更新;
  • 不会影响撤销和还原按键;
  • 不会干扰拼写检查;
  • 和大多编辑框相关脚本兼容。







//syntax highlighter








[[mw:User:Remember the dot/Syntax highlighter|语法高亮]]:高亮编辑框中的维基语法。


在贵wiki创建页面MediaWiki:Gadget-DotsSyntaxHighlighter.js,将User:Remember the dot/Syntax highlighter.js的源代码复制进去。接着在MediaWiki:Gadgets-definition新起一行写上



[ 语法高亮]:高亮编辑框中的维基语法。

MediaWiki 1.22 or later is required.



  1. 高亮工具在最新版Firefox上运行效果最佳。
  2. 高亮工具在最新版ChromeSafariOpera上基本能够运行,但对zh:泰文藏文等文字未必能正确运行。
  3. 高亮工具不支持Internet Explorer(错误太严重)。若使用Internet Explorer,该脚本会拒绝执行。
  4. 该脚本和某些编辑框相关脚本不兼容。


  1. 考量到性能,该高亮工具要求所有标签符合XML规范。比如以<p>开始的标签必须用</p>关闭,要写<br/>而非<br>
  2. 考量到性能,高亮语法不处理'''撇号接斜体''''斜体撇号'''——它们会视作无效语法。我推荐用'<i>撇号接斜体</i><i>斜体撇号'</i>表示。
  3. 由在段首放置一个空格产生的<pre>标签不会高亮。这是因为该高亮工具还没有智能到可以判断这个空格是否是用于模板中的一个普通的空格。
  4. __NOTOC__行为开关不会高亮。
  5. {{fullurl:}}与模板生成的外部链接不会高亮。所见效果为[{{fullurl:My page}} 外部链接]而非[{{fullurl:My page}} 外部链接].


  1. 高亮工具在长页面中效果不佳,若延时超过50ms会自动禁用。If this happens, a message appears explaining what happened and suggesting how to work around it or increase the timeout.
  2. 高亮工具在上传文件时不会运行。
  3. 高亮工具可能会盖掉您的编辑框用户样式。




defaultColor 用于没有识别出定义了特殊颜色的语法元素
boldOrItalicColor '''bold''' ''italic''
wikilinkColor [[wikilink]]
externalLinkColor [http:// named external link], [],
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;


//syntax highlighter

syntaxHighlighterConfig = {
    externalLinkColor: "#FFCC66", //orange
    wikilinkColor: "#E6FFFF", //cyan


//syntax highlighter

syntaxHighlighterConfig = {
    externalLinkColor: "",

To not highlight any syntax except those you explicitly want, set defaultColor 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 highlighter

syntaxHighlighterConfig = {
    defaultColor: "",
    tagColor: "normal",


You can specify a timeout 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 highlighter

syntaxHighlighterConfig = {
    timeout: 30,

If you installed the highlighter as a gadget, omit the first two lines.

Non-wikitext tags[edit]

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 setting sourceTags 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 highlighter

syntaxHighlighterConfig = {
    sourceTags: ["math", "syntaxhighlight", "source", "timeline", "hiero", "foo"],

Other tags, such as <pre> and <nowiki>, do not use wikitext but do process character entities (&nbsp; for example). The list of nowiki-like tags is controlled by the nowikiTags property. For example, to highlight only character entities within the <abbr> tag, put the following in your common.js:

//syntax highlighter

syntaxHighlighterConfig = {
    nowikiTags: ["nowiki", "pre", "abbr"],

In both examples, if you installed the highlighter as a gadget, omit the first two lines.

Site defaults[edit]

All properties that can be specified in the syntaxHighlighterConfig variable can also be specified in the syntaxHighlighterSiteConfig variable. syntaxHighlighterSiteConfig is intended for site defaults and syntaxHighlighterConfig for user preferences. Properties of syntaxHighlighterConfig take precendence over properties of syntaxHighlighterSiteConfig, and properties of syntaxHighlighterSiteConfig 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 of MediaWiki:Gadget-DotsSyntaxHighlighter‎:

syntaxHighlighterSiteConfig = {
    sourceTags: [],

Reporting bugs[edit]

Note: Remember to check whether the bug you want to report is already a known issue.

When reporting bugs to me, please include:

Source code[edit]

To reduce download time and because the ResourceLoader does not automatically minify scripts imported from other wikis, MediaWiki:Gadget-DotsSyntaxHighlighter.js itself is kept in minified form. The actual source code is available at User:Remember the dot/Syntax highlighter.js.

Overview of approach[edit]

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 :before and :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.