a8feac8e5331b733d13c9e24deb7a0cd636071a07fed86e24045e1d93b91c040b5ec87a8921c7c3b2921b0110a0610bf2ef810e48c707be212c5ff6d486ac1 19 KB

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