|
| 1 | +# Highlight.js |
| 2 | + |
| 3 | +[](https://travis-ci.org/highlightjs/highlight.js) [](https://greenkeeper.io/) |
| 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 | +## Getting Started |
| 11 | + |
| 12 | +The bare minimum for using highlight.js on a web page is linking to the |
| 13 | +library along with one of the styles and calling |
| 14 | +[`initHighlightingOnLoad`][1]: |
| 15 | + |
| 16 | +```html |
| 17 | +<link rel="stylesheet" href="/path/to/styles/default.css"> |
| 18 | +<script src="/path/to/highlight.pack.js"></script> |
| 19 | +<script>hljs.initHighlightingOnLoad();</script> |
| 20 | +``` |
| 21 | + |
| 22 | +This will find and highlight code inside of `<pre><code>` tags; it tries |
| 23 | +to detect the language automatically. If automatic detection doesn’t |
| 24 | +work for you, you can specify the language in the `class` attribute: |
| 25 | + |
| 26 | +```html |
| 27 | +<pre><code class="html">...</code></pre> |
| 28 | +``` |
| 29 | + |
| 30 | +The list of supported language classes is available in the [class |
| 31 | +reference][2]. Classes can also be prefixed with either `language-` or |
| 32 | +`lang-`. |
| 33 | + |
| 34 | +To make arbitrary text look like code, but without highlighting, use the |
| 35 | +`plaintext` class: |
| 36 | + |
| 37 | +```html |
| 38 | +<pre><code class="plaintext">...</code></pre> |
| 39 | +``` |
| 40 | + |
| 41 | +To disable highlighting altogether use the `nohighlight` class: |
| 42 | + |
| 43 | +```html |
| 44 | +<pre><code class="nohighlight">...</code></pre> |
| 45 | +``` |
| 46 | + |
| 47 | +## Custom Initialization |
| 48 | + |
| 49 | +When you need a bit more control over the initialization of |
| 50 | +highlight.js, you can use the [`highlightBlock`][3] and [`configure`][4] |
| 51 | +functions. This allows you to control *what* to highlight and *when*. |
| 52 | + |
| 53 | +Here’s an equivalent way to calling [`initHighlightingOnLoad`][1] using |
| 54 | +vanilla JS: |
| 55 | + |
| 56 | +```js |
| 57 | +document.addEventListener('DOMContentLoaded', (event) => { |
| 58 | + document.querySelectorAll('pre code').forEach((block) => { |
| 59 | + hljs.highlightBlock(block); |
| 60 | + }); |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +You can use any tags instead of `<pre><code>` to mark up your code. If |
| 65 | +you don't use a container that preserves line breaks you will need to |
| 66 | +configure highlight.js to use the `<br>` tag: |
| 67 | + |
| 68 | +```js |
| 69 | +hljs.configure({useBR: true}); |
| 70 | + |
| 71 | +document.querySelectorAll('div.code').forEach((block) => { |
| 72 | + hljs.highlightBlock(block); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +For other options refer to the documentation for [`configure`][4]. |
| 77 | + |
| 78 | + |
| 79 | +## Web Workers |
| 80 | + |
| 81 | +You can run highlighting inside a web worker to avoid freezing the browser |
| 82 | +window while dealing with very big chunks of code. |
| 83 | + |
| 84 | +In your main script: |
| 85 | + |
| 86 | +```js |
| 87 | +addEventListener('load', () => { |
| 88 | + const code = document.querySelector('#code'); |
| 89 | + const worker = new Worker('worker.js'); |
| 90 | + worker.onmessage = (event) => { code.innerHTML = event.data; } |
| 91 | + worker.postMessage(code.textContent); |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +In worker.js: |
| 96 | + |
| 97 | +```js |
| 98 | +onmessage = (event) => { |
| 99 | + importScripts('<path>/highlight.pack.js'); |
| 100 | + const result = self.hljs.highlightAuto(event.data); |
| 101 | + postMessage(result.value); |
| 102 | +}; |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +## Getting the Library |
| 107 | + |
| 108 | +You can get highlight.js as a hosted, or custom-build, browser script or |
| 109 | +as a server module. Right out of the box the browser script supports |
| 110 | +both AMD and CommonJS, so if you wish you can use RequireJS or |
| 111 | +Browserify without having to build from source. The server module also |
| 112 | +works perfectly fine with Browserify, but there is the option to use a |
| 113 | +build specific to browsers rather than something meant for a server. |
| 114 | +Head over to the [download page][5] for all the options. |
| 115 | + |
| 116 | +**Don't link to GitHub directly.** The library is not supposed to work straight |
| 117 | +from the source, it requires building. If none of the pre-packaged options |
| 118 | +work for you refer to the [building documentation][6]. |
| 119 | + |
| 120 | +**The CDN-hosted package doesn't have all the languages.** Otherwise it'd be |
| 121 | +too big. If you don't see the language you need in the ["Common" section][5], |
| 122 | +it can be added manually: |
| 123 | + |
| 124 | +```html |
| 125 | +<script |
| 126 | + charset="UTF-8" |
| 127 | + src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/go.min.js"></script> |
| 128 | +``` |
| 129 | + |
| 130 | +**On Almond.** You need to use the optimizer to give the module a name. For |
| 131 | +example: |
| 132 | + |
| 133 | +```bash |
| 134 | +r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js |
| 135 | +``` |
| 136 | + |
| 137 | + |
| 138 | +### CommonJS |
| 139 | + |
| 140 | +You can import Highlight.js as a CommonJS-module: |
| 141 | + |
| 142 | +```bash |
| 143 | +npm install highlight.js --save |
| 144 | +``` |
| 145 | + |
| 146 | +In your application: |
| 147 | + |
| 148 | +```js |
| 149 | +import hljs from 'highlight.js'; |
| 150 | +``` |
| 151 | + |
| 152 | +The default import imports all languages! Therefore it is likely to be more efficient to import only the library and the languages you need: |
| 153 | + |
| 154 | +```js |
| 155 | +import hljs from 'highlight.js/lib/highlight'; |
| 156 | +import javascript from 'highlight.js/lib/languages/javascript'; |
| 157 | +hljs.registerLanguage('javascript', javascript); |
| 158 | +``` |
| 159 | + |
| 160 | +To set the syntax highlighting style, if your build tool processes CSS from your JavaScript entry point, you can import the stylesheet directly into your CommonJS-module: |
| 161 | + |
| 162 | +```js |
| 163 | +import hljs from 'highlight.js/lib/highlight'; |
| 164 | +import 'highlight.js/styles/github.css'; |
| 165 | +``` |
| 166 | + |
| 167 | +## License |
| 168 | + |
| 169 | +Highlight.js is released under the BSD License. See [LICENSE][7] file |
| 170 | +for details. |
| 171 | + |
| 172 | +## Links |
| 173 | + |
| 174 | +The official site for the library is at <https://highlightjs.org/>. |
| 175 | + |
| 176 | +Further in-depth documentation for the API and other topics is at |
| 177 | +<http://highlightjs.readthedocs.io/>. |
| 178 | + |
| 179 | +Authors and contributors are listed in the [AUTHORS.en.txt][8] file. |
| 180 | + |
| 181 | +[1]: http://highlightjs.readthedocs.io/en/latest/api.html#inithighlightingonload |
| 182 | +[2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html |
| 183 | +[3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightblock-block |
| 184 | +[4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure-options |
| 185 | +[5]: https://highlightjs.org/download/ |
| 186 | +[6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html |
| 187 | +[7]: https://github.com/highlightjs/highlight.js/blob/master/LICENSE |
| 188 | +[8]: https://github.com/highlightjs/highlight.js/blob/master/AUTHORS.en.txt |
0 commit comments