UNPKG

17.8 kBMarkdownView Raw
1# Highlight.js
2
3[![latest version](https://badgen.net/npm/v/highlight.js?label=latest)](https://www.npmjs.com/package/highlight.js)
4[![license](https://badgen.net/github/license/highlightjs/highlight.js?color=cyan)](https://github.com/highlightjs/highlight.js/blob/main/LICENSE)
5[![install size](https://badgen.net/packagephobia/install/highlight.js?label=npm+install)](https://packagephobia.now.sh/result?p=highlight.js)
6![minified](https://img.shields.io/github/size/highlightjs/cdn-release/build/highlight.min.js?label=minified)
7[![NPM downloads weekly](https://badgen.net/npm/dw/highlight.js?label=npm+downloads&color=purple)](https://www.npmjs.com/package/highlight.js)
8[![jsDelivr CDN downloads](https://badgen.net/jsdelivr/hits/gh/highlightjs/cdn-release?label=jsDelivr+CDN&color=purple)](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release)
9
10[![ci status](https://badgen.net/github/checks/highlightjs/highlight.js/main?label=build)](https://github.com/highlightjs/highlight.js/actions/workflows/tests.js.yml)
11[![CodeQL](https://github.com/highlightjs/highlight.js/workflows/CodeQL/badge.svg)](https://github.com/highlightjs/highlight.js/actions/workflows/github-code-scanning/codeql)
12[![vulnerabilities](https://badgen.net/snyk/highlightjs/highlight.js)](https://snyk.io/test/github/highlightjs/highlight.js?targetFile=package.json)
13
14[![discord](https://badgen.net/badge/icon/discord?icon=discord&label&color=pink)](https://discord.gg/M24EbU7ja9)
15[![open issues](https://badgen.net/github/open-issues/highlightjs/highlight.js?label=issues)](https://github.com/highlightjs/highlight.js/issues)
16[![help welcome issues](https://badgen.net/github/label-issues/highlightjs/highlight.js/help%20welcome/open)](https://github.com/highlightjs/highlight.js/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+welcome%22)
17[![good first issue](https://badgen.net/github/label-issues/highlightjs/highlight.js/good%20first%20issue/open)](https://github.com/highlightjs/highlight.js/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22)
18
19<!-- [![Build CI](https://img.shields.io/github/workflow/status/highlightjs/highlight.js/Node.js%20CI)](https://github.com/highlightjs/highlight.js/actions?query=workflow%3A%22Node.js+CI%22) -->
20<!-- [![commits since release](https://img.shields.io/github/commits-since/highlightjs/highlight.js/latest?label=commits+since)](https://github.com/highlightjs/highlight.js/commits/main) -->
21<!-- [![](https://data.jsdelivr.com/v1/package/gh/highlightjs/cdn-release/badge?style=rounded)](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release) -->
22<!-- [![Total Lines](https://img.shields.io/tokei/lines/github/highlightjs/highlight.js)]() -->
23<!-- [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/highlight.js.svg)](https://bundlephobia.com/result?p=highlight.js) -->
24
25
26Highlight.js is a syntax highlighter written in JavaScript. It works in
27the browser as well as on the server. It can work with pretty much any
28markup, doesn’t depend on any other frameworks, and has automatic language
29detection.
30
31**Contents**
32
33- [Basic Usage](#basic-usage)
34 - [In the Browser](#in-the-browser)
35 - [Plaintext Code Blocks](#plaintext-code-blocks)
36 - [Ignoring a Code Block](#ignoring-a-code-block)
37 - [Node.js on the Server](#nodejs-on-the-server)
38- [Supported Languages](#supported-languages)
39- [Custom Usage](#custom-usage)
40 - [Using custom HTML](#using-custom-html)
41 - [Using with Vue.js](#using-with-vuejs)
42 - [Using Web Workers](#using-web-workers)
43- [Importing the Library](#importing-the-library)
44 - [Node.js / `require`](#nodejs--require)
45 - [ES6 Modules / `import`](#es6-modules--import)
46- [Getting the Library](#getting-the-library)
47 - [Fetch via CDN](#fetch-via-cdn)
48 - [cdnjs (link)](#cdnjs-link)
49 - [jsdelivr (link)](#jsdelivr-link)
50 - [unpkg (link)](#unpkg-link)
51 - [Download prebuilt CDN assets](#download-prebuilt-cdn-assets)
52 - [Download from our website](#download-from-our-website)
53 - [Install via NPM package](#install-via-npm-package)
54 - [Build from Source](#build-from-source)
55- [Requirements](#requirements)
56- [License](#license)
57- [Links](#links)
58
59---
60
61#### Upgrading to Version 11
62
63As always, major releases do contain breaking changes which may require action from users. Please read [VERSION_11_UPGRADE.md](https://github.com/highlightjs/highlight.js/blob/main/VERSION_11_UPGRADE.md) for a detailed summary of breaking changes and any actions you may need to take.
64
65
66#### Support for older versions <!-- omit in toc -->
67
68Please see [SECURITY.md](https://github.com/highlightjs/highlight.js/blob/main/SECURITY.md) for long-term support information.
69
70---
71
72## Basic Usage
73### In the Browser
74
75The bare minimum for using highlight.js on a web page is linking to the
76library along with one of the themes and calling [`highlightAll`][1]:
77
78```html
79<link rel="stylesheet" href="/path/to/styles/default.min.css">
80<script src="/path/to/highlight.min.js"></script>
81<script>hljs.highlightAll();</script>
82```
83
84This will find and highlight code inside of `<pre><code>` tags; it tries
85to detect the language automatically. If automatic detection doesn’t
86work for you, or you simply prefer to be explicit, you can specify the language manually by using the `class` attribute:
87
88
89```html
90<pre><code class="language-html">...</code></pre>
91```
92
93#### Plaintext Code Blocks
94
95To apply the Highlight.js styling to plaintext without actually highlighting it, use the `plaintext` language:
96
97```html
98<pre><code class="language-plaintext">...</code></pre>
99```
100
101#### Ignoring a Code Block
102
103To skip highlighting of a code block completely, use the `nohighlight` class:
104
105```html
106<pre><code class="nohighlight">...</code></pre>
107```
108
109### Node.js on the Server
110
111The bare minimum to auto-detect the language and highlight some code.
112
113```js
114// load the library and ALL languages
115hljs = require('highlight.js');
116html = hljs.highlightAuto('<h1>Hello World!</h1>').value
117```
118
119To load only a "common" subset of popular languages:
120
121```js
122hljs = require('highlight.js/lib/common');
123```
124
125To highlight code with a specific language, use `highlight`:
126
127```js
128html = hljs.highlight('<h1>Hello World!</h1>', {language: 'xml'}).value
129```
130
131See [Importing the Library](#importing-the-library) for more examples of `require` vs `import` usage, etc. For more information about the result object returned by `highlight` or `highlightAuto` refer to the [api docs](https://highlightjs.readthedocs.io/en/latest/api.html).
132
133
134
135## Supported Languages
136
137Highlight.js supports over 180 languages in the core library. There are also 3rd party
138language definitions available to support even more languages. You can find the full list of supported languages in [SUPPORTED_LANGUAGES.md][9].
139
140## Custom Usage
141
142If you need a bit more control over the initialization of
143Highlight.js, you can use the [`highlightElement`][3] and [`configure`][4]
144functions. This allows you to better control *what* to highlight and *when*.
145
146For example, here’s the rough equivalent of calling [`highlightAll`][1] but doing the work manually instead:
147
148```js
149document.addEventListener('DOMContentLoaded', (event) => {
150 document.querySelectorAll('pre code').forEach((el) => {
151 hljs.highlightElement(el);
152 });
153});
154```
155
156Please refer to the documentation for [`configure`][4] options.
157
158
159### Using custom HTML
160
161We strongly recommend `<pre><code>` wrapping for code blocks. It's quite
162semantic and "just works" out of the box with zero fiddling. It is possible to
163use other HTML elements (or combos), but you may need to pay special attention to
164preserving linebreaks.
165
166Let's say your markup for code blocks uses divs:
167
168```html
169<div class='code'>...</div>
170```
171
172To highlight such blocks manually:
173
174```js
175// first, find all the div.code blocks
176document.querySelectorAll('div.code').forEach(el => {
177 // then highlight each
178 hljs.highlightElement(el);
179});
180```
181
182Without using a tag that preserves linebreaks (like `pre`) you'll need some
183additional CSS to help preserve them. You could also [pre and post-process line
184breaks with a plug-in][brPlugin], but *we recommend using CSS*.
185
186[brPlugin]: https://github.com/highlightjs/highlight.js/issues/2559
187
188To preserve linebreaks inside a `div` using CSS:
189
190```css
191div.code {
192 white-space: pre;
193}
194```
195
196
197### Using with Vue.js
198
199See [highlightjs/vue-plugin](https://github.com/highlightjs/vue-plugin) for a simple Vue plugin that works great with Highlight.js.
200
201An example of `vue-plugin` in action:
202
203```html
204 <div id="app">
205 <!-- bind to a data property named `code` -->
206 <highlightjs autodetect :code="code" />
207 <!-- or literal code works as well -->
208 <highlightjs language='javascript' code="var x = 5;" />
209 </div>
210```
211
212### Using Web Workers
213
214You can run highlighting inside a web worker to avoid freezing the browser
215window while dealing with very big chunks of code.
216
217In your main script:
218
219```js
220addEventListener('load', () => {
221 const code = document.querySelector('#code');
222 const worker = new Worker('worker.js');
223 worker.onmessage = (event) => { code.innerHTML = event.data; }
224 worker.postMessage(code.textContent);
225});
226```
227
228In worker.js:
229
230```js
231onmessage = (event) => {
232 importScripts('<path>/highlight.min.js');
233 const result = self.hljs.highlightAuto(event.data);
234 postMessage(result.value);
235};
236```
237
238## Importing the Library
239
240First, you'll likely be installing the library via `npm` or `yarn` -- see [Getting the Library](#getting-the-library).
241
242
243### Node.js / `require`
244
245Requiring the top-level library will load all languages:
246
247```js
248// require the highlight.js library, including all languages
249const hljs = require('./highlight.js');
250const highlightedCode = hljs.highlightAuto('<span>Hello World!</span>').value
251```
252
253For a smaller footprint, load our common subset of languages (the same set used for our default web build).
254
255```js
256const hljs = require('highlight.js/lib/common');
257```
258
259For the smallest footprint, load only the languages you need:
260
261```js
262const hljs = require('highlight.js/lib/core');
263hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));
264
265const highlightedCode = hljs.highlight('<span>Hello World!</span>', {language: 'xml'}).value
266```
267
268
269### ES6 Modules / `import`
270
271*Note: You can also import directly from fully static URLs, such as our very own pre-built
272ES6 Module CDN resources. See [Fetch via CDN](#fetch-via-cdn) for specific examples.*
273
274The default import will register all languages:
275
276```js
277import hljs from 'highlight.js';
278```
279
280It is more efficient to import only the library and register the languages you need:
281
282```js
283import hljs from 'highlight.js/lib/core';
284import javascript from 'highlight.js/lib/languages/javascript';
285hljs.registerLanguage('javascript', javascript);
286```
287
288If your build tool processes CSS imports, you can also import the theme directly as a module:
289
290```js
291import hljs from 'highlight.js';
292import 'highlight.js/styles/github.css';
293```
294
295
296## Getting the Library
297
298You can get highlight.js as a hosted, or custom-build, browser script or
299as a server module. Right out of the box the browser script supports
300both AMD and CommonJS, so if you wish you can use RequireJS or
301Browserify without having to build from source. The server module also
302works perfectly fine with Browserify, but there is the option to use a
303build specific to browsers rather than something meant for a server.
304
305
306**Do not link to GitHub directly.** The library is not supposed to work straight
307from the source, it requires building. If none of the pre-packaged options
308work for you refer to the [building documentation][6].
309
310**On Almond.** You need to use the optimizer to give the module a name. For
311example:
312
313```bash
314r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
315```
316
317### Fetch via CDN
318
319A prebuilt version of Highlight.js bundled with many common languages is hosted by several popular CDNs.
320When using Highlight.js via CDN you can use Subresource Integrity for additional security. For details
321see [DIGESTS.md](https://github.com/highlightjs/cdn-release/blob/main/DIGESTS.md).
322
323#### cdnjs ([link](https://cdnjs.com/libraries/highlight.js))
324
325##### Common JS <!-- omit in toc -->
326
327```html
328<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
329<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
330<!-- and it's easy to individually load additional languages -->
331<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/go.min.js"></script>
332```
333
334##### ES6 Modules <!-- omit in toc -->
335
336````html
337<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/dark.min.css">
338<script type="module">
339import hljs from 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/es/highlight.min.js';
340// and it's easy to individually load additional languages
341import go from 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/es/languages/go.min.js';
342hljs.registerLanguage('go', go);
343</script>
344
345````
346
347
348#### jsdelivr ([link](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release))
349
350##### Common JS <!-- omit in toc -->
351
352```html
353<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/default.min.css">
354<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js"></script>
355<!-- and it's easy to individually load additional languages -->
356<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/languages/go.min.js"></script>
357```
358
359##### ES6 Modules <!-- omit in toc -->
360
361```html
362<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/styles/default.min.css">
363<script type="module">
364import hljs from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/es/highlight.min.js';
365// and it's easy to individually load additional languages
366import go from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/es/languages/go.min.js';
367hljs.registerLanguage('go', go);
368</script>
369```
370
371#### unpkg ([link](https://unpkg.com/browse/@highlightjs/cdn-assets/))
372
373##### Common JS <!-- omit in toc -->
374
375```html
376<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.9.0/styles/default.min.css">
377<script src="https://unpkg.com/@highlightjs/cdn-assets@11.9.0/highlight.min.js"></script>
378<!-- and it's easy to individually load additional languages -->
379<script src="https://unpkg.com/@highlightjs/cdn-assets@11.9.0/languages/go.min.js"></script>
380```
381
382##### ES6 Modules <!-- omit in toc -->
383
384```html
385<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.9.0/styles/default.min.css">
386<script type="module">
387import hljs from 'https://unpkg.com/@highlightjs/cdn-assets@11.9.0/es/highlight.min.js';
388// and it's easy to individually load & register additional languages
389import go from 'https://unpkg.com/@highlightjs/cdn-assets@11.9.0/es/languages/go.min.js';
390hljs.registerLanguage('go', go);
391</script>
392```
393
394
395**Note:** *The CDN-hosted `highlight.min.js` package doesn't bundle every language.* It would be
396very large. You can find our list of "common" languages that we bundle by default on our [download page][5].
397
398#### Download prebuilt CDN assets
399
400You can also download and self-host the same assets we serve up via our own CDNs. We publish those builds to the [cdn-release](https://github.com/highlightjs/cdn-release) GitHub repository. You can easily pull individual files off the CDN endpoints with `curl`, etc; if say you only needed `highlight.min.js` and a single CSS file.
401
402There is also an npm package [@highlightjs/cdn-assets](https://www.npmjs.com/package/@highlightjs/cdn-assets) if pulling the assets in via `npm` or `yarn` would be easier for your build process.
403
404### Download from our website
405
406The [download page][5] can quickly generate a custom single-file minified bundle including only the languages you desire.
407
408**Note:** [Building from source](#build-from-source) can produce slightly smaller builds than the website download.
409
410
411### Install via NPM package
412
413Our NPM package including all supported languages can be installed with NPM or Yarn:
414
415```bash
416npm install highlight.js
417# or
418yarn add highlight.js
419```
420
421Alternatively, you can build the NPM package from source.
422
423
424### Build from Source
425
426The [current source code][10] is always available on GitHub.
427
428```bash
429node tools/build.js -t node
430node tools/build.js -t browser :common
431node tools/build.js -t cdn :common
432```
433
434See our [building documentation][6] for more information.
435
436
437## Requirements
438
439Highlight.js works on all modern browsers and currently supported Node.js versions. You'll need the following software to contribute to the core library:
440
441- Node.js >= 12.x
442- npm >= 6.x
443
444## License
445
446Highlight.js is released under the BSD License. See our [LICENSE][7] file
447for details.
448
449
450## Links
451
452The official website for the library is <https://highlightjs.org/>.
453
454Further in-depth documentation for the API and other topics is at
455<http://highlightjs.readthedocs.io/>.
456
457A list of the Core Team and contributors can be found in the [CONTRIBUTORS.md][8] file.
458
459[1]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightall
460[2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
461[3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightelement
462[4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure
463[5]: https://highlightjs.org/download/
464[6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
465[7]: https://github.com/highlightjs/highlight.js/blob/main/LICENSE
466[8]: https://github.com/highlightjs/highlight.js/blob/main/CONTRIBUTORS.md
467[9]: https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
468[10]: https://github.com/highlightjs/