UNPKG

16.1 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<!-- [![build and CI status](https://badgen.net/github/checks/highlightjs/highlight.js?label=build)](https://github.com/highlightjs/highlight.js/actions?query=workflow%3A%22Node.js+CI%22) -->
11![build and CI status](https://badgen.net/github/checks/highlightjs/highlight.js/main?label=build)
12[![code quality](https://badgen.net/lgtm/grade/g/highlightjs/highlight.js/js?label=code+quality)](https://lgtm.com/projects/g/highlightjs/highlight.js/?mode=list)
13[![vulnerabilities](https://badgen.net/snyk/highlightjs/highlight.js)](https://snyk.io/test/github/highlightjs/highlight.js?targetFile=package.json)
14![dev deps](https://badgen.net/david/dev/highlightjs/highlight.js?label=dev+deps)
15
16
17[![discord](https://badgen.net/badge/icon/discord?icon=discord&label&color=pink)](https://discord.gg/M24EbU7ja9)
18[![open issues](https://badgen.net/github/open-issues/highlightjs/highlight.js?label=issues)](https://github.com/highlightjs/highlight.js/issues)
19[![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)
20[![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%22beginner+friendly%22)
21
22<!-- [![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) -->
23<!-- [![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) -->
24<!-- [![](https://data.jsdelivr.com/v1/package/gh/highlightjs/cdn-release/badge?style=rounded)](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release) -->
25<!-- [![Total Lines](https://img.shields.io/tokei/lines/github/highlightjs/highlight.js)]() -->
26<!-- [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/highlight.js.svg)](https://bundlephobia.com/result?p=highlight.js) -->
27
28
29Highlight.js is a syntax highlighter written in JavaScript. It works in
30the browser as well as on the server. It can work with pretty much any
31markup, doesn’t depend on any other frameworks, and has automatic language
32detection.
33
34**Contents**
35
36- [Basic Usage](#basic-usage)
37 - [In the Browser](#in-the-browser)
38 - [Plaintext Code Blocks](#plaintext-code-blocks)
39 - [Ignoring a Code Block](#ignoring-a-code-block)
40 - [Node.js on the Server](#nodejs-on-the-server)
41- [Supported Languages](#supported-languages)
42- [Custom Usage](#custom-usage)
43 - [Using custom HTML](#using-custom-html)
44 - [Using with Vue.js](#using-with-vuejs)
45 - [Using Web Workers](#using-web-workers)
46- [Importing the Library](#importing-the-library)
47 - [Node.js / `require`](#nodejs--require)
48 - [ES6 Modules / `import`](#es6-modules--import)
49- [Getting the Library](#getting-the-library)
50 - [Fetch via CDN](#fetch-via-cdn)
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.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 in the 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
271The default import will register all languages:
272
273```js
274import hljs from 'highlight.js';
275```
276
277 It is more efficient to import only the library and register the languages you need:
278
279```js
280import hljs from 'highlight.js/lib/core';
281import javascript from 'highlight.js/lib/languages/javascript';
282hljs.registerLanguage('javascript', javascript);
283```
284
285If your build tool processes CSS imports, you can also import the theme directly as a module:
286
287```js
288import hljs from 'highlight.js';
289import 'highlight.js/styles/github.css';
290```
291
292
293## Getting the Library
294
295You can get highlight.js as a hosted, or custom-build, browser script or
296as a server module. Right out of the box the browser script supports
297both AMD and CommonJS, so if you wish you can use RequireJS or
298Browserify without having to build from source. The server module also
299works perfectly fine with Browserify, but there is the option to use a
300build specific to browsers rather than something meant for a server.
301
302
303**Do not link to GitHub directly.** The library is not supposed to work straight
304from the source, it requires building. If none of the pre-packaged options
305work for you refer to the [building documentation][6].
306
307**On Almond.** You need to use the optimizer to give the module a name. For
308example:
309
310```bash
311r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
312```
313
314### Fetch via CDN
315
316A prebuilt version of Highlight.js bundled with many common languages is hosted by several popular CDNs.
317When using Highlight.js via CDN you can use Subresource Integrity for additional security. For details
318see [DIGESTS.md](https://github.com/highlightjs/cdn-release/blob/main/DIGESTS.md).
319
320**cdnjs** ([link](https://cdnjs.com/libraries/highlight.js))
321
322```html
323<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/styles/default.min.css">
324<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js"></script>
325<!-- and it's easy to individually load additional languages -->
326<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/languages/go.min.js"></script>
327```
328
329**jsdelivr** ([link](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release))
330
331```html
332<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.3.1/build/styles/default.min.css">
333<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.3.1/build/highlight.min.js"></script>
334<!-- and it's easy to individually load additional languages -->
335<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.3.1/build/languages/go.min.js"></script>
336```
337
338**unpkg** ([link](https://unpkg.com/browse/@highlightjs/cdn-assets/))
339
340```html
341<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.3.1/styles/default.min.css">
342<script src="https://unpkg.com/@highlightjs/cdn-assets@11.3.1/highlight.min.js"></script>
343<!-- and it's easy to individually load additional languages -->
344<script src="https://unpkg.com/@highlightjs/cdn-assets@11.3.1/languages/go.min.js"></script>
345```
346
347**Note:** *The CDN-hosted `highlight.min.js` package doesn't bundle every language.* It would be
348very large. You can find our list of "common" languages that we bundle by default on our [download page][5].
349
350#### Download prebuilt CDN assets
351
352You 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.
353
354There 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.
355
356### Download from our website
357
358The [download page][5] can quickly generate a custom single-file minified bundle including only the languages you desire.
359
360**Note:** [Building from source](#build-from-source) can produce slightly smaller builds than the website download.
361
362
363### Install via NPM package
364
365Our NPM package including all supported languages can be installed with NPM or Yarn:
366
367```bash
368npm install highlight.js
369# or
370yarn add highlight.js
371```
372
373Alternatively, you can build the NPM package from source.
374
375
376### Build from Source
377
378The [current source code][10] is always available on GitHub.
379
380```bash
381node tools/build.js -t node
382node tools/build.js -t browser :common
383node tools/build.js -t cdn :common
384```
385
386See our [building documentation][6] for more information.
387
388
389## Requirements
390
391Highlight.js works on all modern browsers and currently supported Node.js versions. You'll need the following software to contribute to the core library:
392
393- Node.js >= 12.x
394- npm >= 6.x
395
396## License
397
398Highlight.js is released under the BSD License. See our [LICENSE][7] file
399for details.
400
401
402## Links
403
404The official website for the library is <https://highlightjs.org/>.
405
406Further in-depth documentation for the API and other topics is at
407<http://highlightjs.readthedocs.io/>.
408
409A list of the Core Team and contributors can be found in the [CONTRIBUTORS.md][8] file.
410
411[1]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightall
412[2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
413[3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightelement
414[4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure
415[5]: https://highlightjs.org/download/
416[6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
417[7]: https://github.com/highlightjs/highlight.js/blob/main/LICENSE
418[8]: https://github.com/highlightjs/highlight.js/blob/main/CONTRIBUTORS.md
419[9]: https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
420[10]: https://github.com/highlightjs/