Highlight.js

JavaScript syntax highlighter with language auto-detection and zero depende...

README

Highlight.js

latest version license install size
minified NPM downloads weekly jsDelivr CDN downloads
ci status code quality vulnerabilities
dev deps

discord open issues help welcome issues good first issue








Highlight.js is a syntax highlighter written in JavaScript. It works in
the browser as well as on the server. It can work with pretty much any
markup, doesn’t depend on any other frameworks, and has automatic language
detection.

Contents

  - [Node.js / require](#nodejs--require)
  - [ES6 Modules / import](#es6-modules--import)
    - cdnjs (link)
    - jsdelivr (link)
    - unpkg (link)


Upgrading to Version 11


As always, major releases do contain breaking changes which may require action from users.  Please read VERSION_11_UPGRADE.md for a detailed summary of breaking changes and any actions you may need to take.


Support for older versions


Please see SECURITY.md for long-term support information.


Basic Usage

In the Browser


The bare minimum for using highlight.js on a web page is linking to the
library along with one of the themes and calling [highlightAll][1]:

  1. ``` html
  2. <link rel="stylesheet" href="/path/to/styles/default.min.css">
  3. <script src="/path/to/highlight.min.js"></script>
  4. <script>hljs.highlightAll();</script>
  5. ```

This will find and highlight code inside of `
` tags; it tries
to detect the language automatically. If automatic detection doesn’t
work for you, or you simply prefer to be explicit, you can specify the language manually in the using the class attribute:


  1. ``` html
  2. <pre><code class="language-html">...</code></pre>
  3. ```

Plaintext Code Blocks


To apply the Highlight.js styling to plaintext without actually highlighting it, use the plaintext language:

  1. ``` html
  2. <pre><code class="language-plaintext">...</code></pre>
  3. ```

Ignoring a Code Block


To skip highlighting of a code block completely, use the nohighlight class:

  1. ``` html
  2. <pre><code class="nohighlight">...</code></pre>
  3. ```

Node.js on the Server


The bare minimum to auto-detect the language and highlight some code.

  1. ``` js
  2. // load the library and ALL languages
  3. hljs = require('highlight.js');
  4. html = hljs.highlightAuto('<h1>Hello World!</h1>').value
  5. ```

To load only a "common" subset of popular languages:

  1. ``` js
  2. hljs = require('highlight.js/lib/common');
  3. ```

To highlight code with a specific language, use highlight:

  1. ``` js
  2. html = hljs.highlight('<h1>Hello World!</h1>', {language: 'xml'}).value
  3. ```

See Importing the Library for more examples ofrequire vs import usage, etc.  For more information about the result object returned by highlight or highlightAuto refer to the api docs.



Supported Languages


Highlight.js supports over 180 languages in the core library.  There are also 3rd party
language definitions available to support even more languages. You can find the full list of supported languages in [SUPPORTED_LANGUAGES.md][9].

Custom Usage


If you need a bit more control over the initialization of
Highlight.js, you can use the [highlightElement][3] and [configure][4]
functions. This allows you to better control what to highlight and when.

For example, here’s the rough equivalent of calling [highlightAll][1] but doing the work manually instead:

  1. ``` js
  2. document.addEventListener('DOMContentLoaded', (event) => {
  3.   document.querySelectorAll('pre code').forEach((el) => {
  4.     hljs.highlightElement(el);
  5.   });
  6. });
  7. ```

Please refer to the documentation for [configure][4] options.


Using custom HTML


We strongly recommend `
` wrapping for code blocks. It's quite
semantic and "just works" out of the box with zero fiddling. It is possible to
use other HTML elements (or combos), but you may need to pay special attention to
preserving linebreaks.

Let's say your markup for code blocks uses divs:

  1. ``` html
  2. <div class='code'>...</div>
  3. ```

To highlight such blocks manually:

  1. ``` js
  2. // first, find all the div.code blocks
  3. document.querySelectorAll('div.code').forEach(el => {
  4.   // then highlight each
  5.   hljs.highlightElement(el);
  6. });
  7. ```

Without using a tag that preserves linebreaks (like pre) you'll need some
additional CSS to help preserve them.  You could also [pre and post-process line
breaks with a plug-in][brPlugin], but we recommend using CSS.

[brPlugin]: https://github.com/highlightjs/highlight.js/issues/2559

To preserve linebreaks inside a div using CSS:

  1. ```css
  2. div.code {
  3.   white-space: pre;
  4. }
  5. ```


Using with Vue.js


See highlightjs/vue-plugin for a simple Vue plugin that works great with Highlight.js.

An example of vue-plugin in action:

  1. ``` html
  2.   <div id="app">
  3.     
  4.     <highlightjs autodetect :code="code" />
  5.     
  6.     <highlightjs language='javascript' code="var x = 5;" />
  7.   </div>
  8. ```

Using Web Workers


You can run highlighting inside a web worker to avoid freezing the browser
window while dealing with very big chunks of code.

In your main script:

  1. ``` js
  2. addEventListener('load', () => {
  3.   const code = document.querySelector('#code');
  4.   const worker = new Worker('worker.js');
  5.   worker.onmessage = (event) => { code.innerHTML = event.data; }
  6.   worker.postMessage(code.textContent);
  7. });
  8. ```

In worker.js:

  1. ``` js
  2. onmessage = (event) => {
  3.   importScripts('<path>/highlight.min.js');
  4.   const result = self.hljs.highlightAuto(event.data);
  5.   postMessage(result.value);
  6. };
  7. ```

Importing the Library


First, you'll likely be installing the library via npm or yarn -- see Getting the Library.


Node.js / require


Requiring the top-level library will load all languages:

  1. ``` js
  2. // require the highlight.js library, including all languages
  3. const hljs = require('./highlight.js');
  4. const highlightedCode = hljs.highlightAuto('<span>Hello World!</span>').value
  5. ```

For a smaller footprint, load our common subset of languages (the same set used for our default web build).

  1. ``` js
  2. const hljs = require('highlight.js/lib/common');
  3. ```

For the smallest footprint, load only the languages you need:

  1. ``` js
  2. const hljs = require('highlight.js/lib/core');
  3. hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));

  4. const highlightedCode = hljs.highlight('<span>Hello World!</span>', {language: 'xml'}).value
  5. ```


ES6 Modules / import


*Note: You can also import directly from fully static URLs, such as our very own pre-built
ES6 Module CDN resources. See Fetch via CDN for specific examples.*

The default import will register all languages:

  1. ``` js
  2. import hljs from 'highlight.js';
  3. ```

It is more efficient to import only the library and register the languages you need:

  1. ``` js
  2. import hljs from 'highlight.js/lib/core';
  3. import javascript from 'highlight.js/lib/languages/javascript';
  4. hljs.registerLanguage('javascript', javascript);
  5. ```

If your build tool processes CSS imports, you can also import the theme directly as a module:

  1. ``` js
  2. import hljs from 'highlight.js';
  3. import 'highlight.js/styles/github.css';
  4. ```


Getting the Library


You can get highlight.js as a hosted, or custom-build, browser script or
as a server module. Right out of the box the browser script supports
both AMD and CommonJS, so if you wish you can use RequireJS or
Browserify without having to build from source. The server module also
works perfectly fine with Browserify, but there is the option to use a
build specific to browsers rather than something meant for a server.


Do not link to GitHub directly. The library is not supposed to work straight
from the source, it requires building. If none of the pre-packaged options
work for you refer to the [building documentation][6].

On Almond. You need to use the optimizer to give the module a name. For
example:

  1. ``` sh
  2. r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
  3. ```

Fetch via CDN


A prebuilt version of Highlight.js bundled with many common languages is hosted by several popular CDNs.
When using Highlight.js via CDN you can use Subresource Integrity for additional security.  For details

cdnjs (link)


Common JS

  1. ``` html
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
  4. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/languages/go.min.js"></script>
  5. ```

ES6 Modules

  1. ```` html
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/dark.min.css">
  3. <script type="module">
  4. import hljs from 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/es/highlight.min.js';
  5. //  and it's easy to individually load additional languages
  6. import go from 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/es/languages/go.min.js';
  7. hljs.registerLanguage('go', go);
  8. </script>
  9. ````


jsdelivr (link)


Common JS

  1. ``` html
  2. <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/styles/default.min.css">
  3. <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/highlight.min.js"></script>
  4. <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/languages/go.min.js"></script>
  5. ```

ES6 Modules

  1. ``` html
  2. <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/styles/default.min.css">
  3. <script type="module">
  4. import hljs from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/es/highlight.min.js';
  5. //  and it's easy to individually load additional languages
  6. import go from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/es/languages/go.min.js';
  7. hljs.registerLanguage('go', go);
  8. </script>
  9. ```

unpkg (link)


Common JS

  1. ``` html
  2. <link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.7.0/styles/default.min.css">
  3. <script src="https://unpkg.com/@highlightjs/cdn-assets@11.7.0/highlight.min.js"></script>
  4. <script src="https://unpkg.com/@highlightjs/cdn-assets@11.7.0/languages/go.min.js"></script>
  5. ```

ES6 Modules

  1. ``` html
  2. <link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.7.0/styles/default.min.css">
  3. <script type="module">
  4. import hljs from 'https://unpkg.com/@highlightjs/cdn-assets@11.7.0/es/highlight.min.js';
  5. //  and it's easy to individually load & register additional languages
  6. import go from 'https://unpkg.com/@highlightjs/cdn-assets@11.7.0/es/languages/go.min.js';
  7. hljs.registerLanguage('go', go);
  8. </script>
  9. ```


Note: The CDN-hosted highlight.min.js package doesn't bundle every language. It would be
very large. You can find our list of "common" languages that we bundle by default on our [download page][5].

Download prebuilt CDN assets


You can also download and self-host the same assets we serve up via our own CDNs.  We publish those builds to the cdn-release GitHub repository.  You can easily pull individual files off the CDN endpoints withcurl, etc; if say you only needed highlight.min.js and a single CSS file.

There is also an npm package @highlightjs/cdn-assets if pulling the assets in vianpm or yarn would be easier for your build process.

Download from our website


The [download page][5] can quickly generate a custom single-file minified bundle including only the languages you desire.

Note: Building from source can produce slightly smaller builds than the website download.


Install via NPM package


Our NPM package including all supported languages can be installed with NPM or Yarn:

  1. ``` sh
  2. npm install highlight.js
  3. # or
  4. yarn add highlight.js
  5. ```

Alternatively, you can build the NPM package from source.


Build from Source


The [current source code][10] is always available on GitHub.

  1. ``` sh
  2. node tools/build.js -t node
  3. node tools/build.js -t browser :common
  4. node tools/build.js -t cdn :common
  5. ```

See our [building documentation][6] for more information.


Requirements


Highlight.js works on all modern browsers and currently supported Node.js versions.  You'll need the following software to contribute to the core library:

- Node.js >= 12.x
- npm >= 6.x

License


Highlight.js is released under the BSD License. See our [LICENSE][7] file
for details.


Links


The official website for the library is .

Further in-depth documentation for the API and other topics is at
.

A list of the Core Team and contributors can be found in the [CONTRIBUTORS.md][8] file.

[1]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightall
[2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
[3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightelement
[4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure
[5]: https://highlightjs.org/download/
[6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
[7]: https://github.com/highlightjs/highlight.js/blob/main/LICENSE
[8]: https://github.com/highlightjs/highlight.js/blob/main/CONTRIBUTORS.md
[9]: https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
[10]: https://github.com/highlightjs/