1 | # Highlight.js
|
2 |
|
3 | [![Build Status](https://travis-ci.org/highlightjs/highlight.js.svg?branch=master)](https://travis-ci.org/highlightjs/highlight.js) [![Greenkeeper badge](https://badges.greenkeeper.io/highlightjs/highlight.js.svg)](https://greenkeeper.io/) [![install size](https://packagephobia.now.sh/badge?p=highlight.js)](https://packagephobia.now.sh/result?p=highlight.js)
|
4 |
|
5 | Highlight.js is a syntax highlighter written in JavaScript. It works in
|
6 | the browser as well as on the server. It works with pretty much any
|
7 | markup, doesn’t depend on any framework, and has automatic language
|
8 | detection.
|
9 |
|
10 | ## Upgrading from Version 9
|
11 |
|
12 | Version 10 is one of the biggest releases in quite some time. If you're
|
13 | upgrading from version 9, there are some breaking changes and things you may
|
14 | want to double check first.
|
15 |
|
16 | Please read [VERSION_10_UPGRADE.md](https://github.com/highlightjs/highlight.js/blob/master/VERSION_10_UPGRADE.md) for high-level summary of breaking changes and any actions you may need to take. See [VERSION_10_BREAKING_CHANGES.md](https://github.com/highlightjs/highlight.js/blob/master/VERSION_10_BREAKING_CHANGES.md) for a more detailed list and [CHANGES.md](https://github.com/highlightjs/highlight.js/blob/master/CHANGES.md) to learn what else is new.
|
17 |
|
18 | ## Getting Started
|
19 |
|
20 | The bare minimum for using highlight.js on a web page is linking to the
|
21 | library along with one of the styles and calling
|
22 | [`initHighlightingOnLoad`][1]:
|
23 |
|
24 | ```html
|
25 | <link rel="stylesheet" href="/path/to/styles/default.css">
|
26 | <script src="/path/to/highlight.min.js"></script>
|
27 | <script>hljs.initHighlightingOnLoad();</script>
|
28 | ```
|
29 |
|
30 | This will find and highlight code inside of `<pre><code>` tags; it tries
|
31 | to detect the language automatically. If automatic detection doesn’t
|
32 | work for you, you can specify the language in the `class` attribute:
|
33 |
|
34 | ```html
|
35 | <pre><code class="html">...</code></pre>
|
36 | ```
|
37 |
|
38 | Classes may also be prefixed with either `language-` or `lang-`.
|
39 |
|
40 | ```html
|
41 | <pre><code class="language-html">...</code></pre>
|
42 | ```
|
43 |
|
44 | ### Plaintext and Disabling Highlighting
|
45 |
|
46 | To style arbitrary text like code, but without any highlighting, use the
|
47 | `plaintext` class:
|
48 |
|
49 | ```html
|
50 | <pre><code class="plaintext">...</code></pre>
|
51 | ```
|
52 |
|
53 | To disable highlighting of a tag completely, use the `nohighlight` class:
|
54 |
|
55 | ```html
|
56 | <pre><code class="nohighlight">...</code></pre>
|
57 | ```
|
58 |
|
59 | ### Supported Languages
|
60 |
|
61 | Highlight.js supports over 180 different languages in the core library. There are also 3rd party
|
62 | language plugins available for additional languages. You can find the full list of supported languages
|
63 | in [SUPPORTED_LANGUAGES.md][9].
|
64 |
|
65 | ## Custom Initialization
|
66 |
|
67 | When you need a bit more control over the initialization of
|
68 | highlight.js, you can use the [`highlightBlock`][3] and [`configure`][4]
|
69 | functions. This allows you to control *what* to highlight and *when*.
|
70 |
|
71 | Here’s an equivalent way to calling [`initHighlightingOnLoad`][1] using
|
72 | vanilla JS:
|
73 |
|
74 | ```js
|
75 | document.addEventListener('DOMContentLoaded', (event) => {
|
76 | document.querySelectorAll('pre code').forEach((block) => {
|
77 | hljs.highlightBlock(block);
|
78 | });
|
79 | });
|
80 | ```
|
81 |
|
82 | You can use any tags instead of `<pre><code>` to mark up your code. If
|
83 | you don't use a container that preserves line breaks you will need to
|
84 | configure highlight.js to use the `<br>` tag:
|
85 |
|
86 | ```js
|
87 | hljs.configure({useBR: true});
|
88 |
|
89 | document.querySelectorAll('div.code').forEach((block) => {
|
90 | hljs.highlightBlock(block);
|
91 | });
|
92 | ```
|
93 |
|
94 | For other options refer to the documentation for [`configure`][4].
|
95 |
|
96 |
|
97 | ## Web Workers
|
98 |
|
99 | You can run highlighting inside a web worker to avoid freezing the browser
|
100 | window while dealing with very big chunks of code.
|
101 |
|
102 | In your main script:
|
103 |
|
104 | ```js
|
105 | addEventListener('load', () => {
|
106 | const code = document.querySelector('#code');
|
107 | const worker = new Worker('worker.js');
|
108 | worker.onmessage = (event) => { code.innerHTML = event.data; }
|
109 | worker.postMessage(code.textContent);
|
110 | });
|
111 | ```
|
112 |
|
113 | In worker.js:
|
114 |
|
115 | ```js
|
116 | onmessage = (event) => {
|
117 | importScripts('<path>/highlight.min.js');
|
118 | const result = self.hljs.highlightAuto(event.data);
|
119 | postMessage(result.value);
|
120 | };
|
121 | ```
|
122 |
|
123 | ## Node.js
|
124 |
|
125 | You can use highlight.js with node to highlight content before sending it to the browser.
|
126 | Make sure to use the `.value` property to get the formatted html.
|
127 | For more info about the returned object refer to the api docs https://highlightjs.readthedocs.io/en/latest/api.html
|
128 |
|
129 |
|
130 | ```js
|
131 | // require the highlight.js library, including all languages
|
132 | const hljs = require('./highlight.js');
|
133 | const highlightedCode = hljs.highlightAuto('<span>Hello World!</span>').value
|
134 | ```
|
135 |
|
136 | Or for a smaller footprint... load just the languages you need.
|
137 |
|
138 | ```js
|
139 | const hljs = require("highlight.js/lib/core"); // require only the core library
|
140 | // separately require languages
|
141 | hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));
|
142 |
|
143 | const highlightedCode = hljs.highlight('xml', '<span>Hello World!</span>').value
|
144 | ```
|
145 |
|
146 |
|
147 | ## ES6 Modules
|
148 |
|
149 | First, you'll likely install via `npm` or `yarn` -- see [Getting the Library](#getting-the-library) below.
|
150 |
|
151 | In your application:
|
152 |
|
153 | ```js
|
154 | import hljs from 'highlight.js';
|
155 | ```
|
156 |
|
157 | The default import imports all languages. Therefore it is likely to be more efficient to import only the library and the languages you need:
|
158 |
|
159 | ```js
|
160 | import hljs from 'highlight.js/lib/core';
|
161 | import javascript from 'highlight.js/lib/languages/javascript';
|
162 | hljs.registerLanguage('javascript', javascript);
|
163 | ```
|
164 |
|
165 | To set the syntax highlighting style, if your build tool processes CSS from your JavaScript entry point, you can also import the stylesheet directly as modules:
|
166 |
|
167 | ```js
|
168 | import hljs from 'highlight.js/lib/core';
|
169 | import 'highlight.js/styles/github.css';
|
170 | ```
|
171 |
|
172 |
|
173 | ## Getting the Library
|
174 |
|
175 | You can get highlight.js as a hosted, or custom-build, browser script or
|
176 | as a server module. Right out of the box the browser script supports
|
177 | both AMD and CommonJS, so if you wish you can use RequireJS or
|
178 | Browserify without having to build from source. The server module also
|
179 | works perfectly fine with Browserify, but there is the option to use a
|
180 | build specific to browsers rather than something meant for a server.
|
181 |
|
182 |
|
183 | **Do not link to GitHub directly.** The library is not supposed to work straight
|
184 | from the source, it requires building. If none of the pre-packaged options
|
185 | work for you refer to the [building documentation][6].
|
186 |
|
187 | **On Almond.** You need to use the optimizer to give the module a name. For
|
188 | example:
|
189 |
|
190 | ```bash
|
191 | r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
|
192 | ```
|
193 |
|
194 | ### CDN Hosted
|
195 |
|
196 | A prebuilt version of highlight.js bundled with many common languages is hosted by the following CDNs:
|
197 |
|
198 | **cdnjs** ([link](https://cdnjs.com/libraries/highlight.js))
|
199 |
|
200 | ```html
|
201 | <link rel="stylesheet"
|
202 | href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/default.min.css">
|
203 | <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js"></script>
|
204 | <!-- and it's easy to individually load additional languages -->
|
205 | <script charset="UTF-8"
|
206 | src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/languages/go.min.js"></script>
|
207 | ```
|
208 |
|
209 | **jsdelivr** ([link](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release))
|
210 |
|
211 | ```html
|
212 | <link rel="stylesheet"
|
213 | href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/styles/default.min.css">
|
214 | <script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.2/build/highlight.min.js"></script>
|
215 | ```
|
216 |
|
217 | **Note:** *The CDN-hosted `highlight.min.js` package doesn't bundle every language.* It would be
|
218 | very large. You can find our list "common" languages that we bundle by default on our [download page][5].
|
219 |
|
220 | ### Self Hosting
|
221 |
|
222 | The [download page][5] can quickly generate a custom bundle including only the languages you need.
|
223 |
|
224 | Alternatively, you can build a browser package from [source](#source):
|
225 |
|
226 | ```
|
227 | node tools/build.js -t browser :common
|
228 | ```
|
229 |
|
230 | See our [building documentation][6] for more information.
|
231 |
|
232 | **Note:** Building from source should always result in the smallest size builds. The website download page is optimized for speed, not size.
|
233 |
|
234 |
|
235 | #### Prebuilt CDN assets
|
236 |
|
237 | 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](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.
|
238 |
|
239 | There 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.
|
240 |
|
241 |
|
242 | ### NPM / Node.js server module
|
243 |
|
244 | Highlight.js can also be used on the server. The package with all supported languages can be installed from NPM or Yarn:
|
245 |
|
246 | ```bash
|
247 | npm install highlight.js
|
248 | # or
|
249 | yarn add highlight.js
|
250 | ```
|
251 |
|
252 | Alternatively, you can build it from [source](#source):
|
253 |
|
254 | ```bash
|
255 | node tools/build.js -t node
|
256 | ```
|
257 |
|
258 | See our [building documentation][6] for more information.
|
259 |
|
260 |
|
261 | ### Source
|
262 |
|
263 | [Current source][10] is always available on GitHub.
|
264 |
|
265 |
|
266 | ## License
|
267 |
|
268 | Highlight.js is released under the BSD License. See [LICENSE][7] file
|
269 | for details.
|
270 |
|
271 |
|
272 | ## Links
|
273 |
|
274 | The official site for the library is at <https://highlightjs.org/>.
|
275 |
|
276 | Further in-depth documentation for the API and other topics is at
|
277 | <http://highlightjs.readthedocs.io/>.
|
278 |
|
279 | Authors and contributors are listed in the [AUTHORS.txt][8] file.
|
280 |
|
281 | [1]: http://highlightjs.readthedocs.io/en/latest/api.html#inithighlightingonload
|
282 | [2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
|
283 | [3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightblock-block
|
284 | [4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure-options
|
285 | [5]: https://highlightjs.org/download/
|
286 | [6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
|
287 | [7]: https://github.com/highlightjs/highlight.js/blob/master/LICENSE
|
288 | [8]: https://github.com/highlightjs/highlight.js/blob/master/AUTHORS.txt
|
289 | [9]: https://github.com/highlightjs/highlight.js/blob/master/SUPPORTED_LANGUAGES.md
|
290 | [10]: https://github.com/highlightjs/
|