This project provides client-side syntax highlighting for a number of languages, and is more or less a direct port of Ivan Sagalaev’s Highlight.js engine, contributed under CLA.

To use the Highlight engine, require it into your page, along with any appropriate language packs:


Provided Languages

There are several pre-defined language packs. Shipped with Dojo include:

  • C++ (cpp)
  • CSS (css)
  • Delphi (delphi)
  • Django (django)
  • HTML (html)
  • JavaScript (javascript)
  • Python (python)
  • SQL (sql)
  • XML (xml)

The rollups _all (all languages), _dynamic, _static, and _www provide layers of common groups of languages.

There are additional definitions for Pygments-based highlighting:

  • CSS (pygments.css)
  • HTML (pygments.html)
  • JavaScript (pygments.javascript)
  • XML (pygments.xml)

The rollups _www and _html are provided as layers, for related code inclusion.

The name in parenthesis indicate that which you should dojo.require() to load the definitions. eg:

dojo.require("dojox.highlight.languages.pygments.css"); // add CSS rules to the engine

Highlight Styles

Highlight is entirely CSS based, so you will need the appropriate CSS file. The default system css is available in the resources/ folder by the name of highlight.css:

<link rel="stylesheet" href="/js/dojox/highlight/resources/highlight.css" />

Additionally, the Pygments CSS files are located in resources/pygments/ and are described by a theme name. Simply load in the appropriate theme you choose:

<link rel="stylesheet" href="/js/dojox/highlight/resources/pygments/colorful.css" />

There are several themes available:

  • autumn
  • borland
  • colorful
  • default
  • emacs
  • friendly
  • fruity
  • manni
  • murphey
  • native
  • pastie
  • perldoc
  • trac

Using Highlight

After loading in the highlight engine, and putting the CSS on the page, the only remaining step it to highlight the nodes. Highlight acts on <code> blocks. It can be instantiated just as any other widget in Dojo by using the data-dojo-type:

<code data-dojo-type="dojox.highlight.Code">var foo =[1,2,3,4,5], function(n){ return n % 2 });</code>

Or by calling dojox.highlight.init(someNode).

// attempt to highlight all <code> blocks on a page:

Highlight can also be used via the dojox.highlight.processString function

@import "{{baseUrl}}dojox/highlight/resources/highlight.css"
function highlight(){

  // highlighting the code
  var code = dojox.highlight.processString("Select a from b where a = 2;").result;

  // putting the highlighted code in a html element so you can see
  dojo.attr('demoCode1', {innerHTML: code});
<div id="demoCode1">Select a from b where a = 2;</div>
<button data-dojo-type="dijit.form.Button" id="buttonOne" onClick="highlight();">Highlight Code</button>
Error in the documentation? Can’t find what you are looking for? Let us know!