648a941c5ad3708581c4d1e623f6fc83182e82e380e2b0c6e2a12e952ebe9b69d69b2b075b928b424f92433cca918a452311bbb07f8dd6311fbf25a284720e 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. # Highlight.js
  2. [![latest version](https://badgen.net/npm/v/highlight.js?label=latest)](https://www.npmjs.com/package/highlight.js)
  3. [![slack](https://badgen.net/badge/icon/slack?icon=slack&label&color=pink)](https://join.slack.com/t/highlightjs/shared_invite/zt-mj0utgqp-TNFf4VQICnDnPg4zMHChFw)
  4. [![discord](https://badgen.net/badge/icon/discord?icon=discord&label&color=pink)](https://discord.gg/M24EbU7ja9)
  5. [![license](https://badgen.net/github/license/highlightjs/highlight.js?color=cyan)](https://github.com/highlightjs/highlight.js/blob/main/LICENSE)
  6. [![install size](https://badgen.net/packagephobia/install/highlight.js?label=npm+install)](https://packagephobia.now.sh/result?p=highlight.js)
  7. ![minified](https://img.shields.io/github/size/highlightjs/cdn-release/build/highlight.min.js?label=minified)
  8. [![NPM downloads weekly](https://badgen.net/npm/dw/highlight.js?label=npm+downloads&color=purple)](https://www.npmjs.com/package/highlight.js)
  9. [![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)
  10. ![dev deps](https://badgen.net/david/dev/highlightjs/highlight.js?label=dev+deps)
  11. [![code quality](https://badgen.net/lgtm/grade/g/highlightjs/highlight.js/js)](https://lgtm.com/projects/g/highlightjs/highlight.js/?mode=list)
  12. [![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)
  13. [![open issues](https://badgen.net/github/open-issues/highlightjs/highlight.js?label=issues)](https://github.com/highlightjs/highlight.js/issues)
  14. [![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)
  15. [![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)
  16. [![vulnerabilities](https://badgen.net/snyk/highlightjs/highlight.js)](https://snyk.io/test/github/highlightjs/highlight.js?targetFile=package.json)
  17. <!-- [![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) -->
  18. <!-- [![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) -->
  19. <!-- [![](https://data.jsdelivr.com/v1/package/gh/highlightjs/cdn-release/badge?style=rounded)](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release) -->
  20. <!-- [![Total Lines](https://img.shields.io/tokei/lines/github/highlightjs/highlight.js)]() -->
  21. <!-- [![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/highlight.js.svg)](https://bundlephobia.com/result?p=highlight.js) -->
  22. Highlight.js is a syntax highlighter written in JavaScript. It works in
  23. the browser as well as on the server. It works with pretty much any
  24. markup, doesn’t depend on any framework, and has automatic language
  25. detection.
  26. #### Upgrading to Version 10
  27. Version 10 is one of the biggest releases in quite some time. If you're
  28. upgrading from version 9, there are some breaking changes and things you may
  29. want to double check first.
  30. Please read [VERSION_10_UPGRADE.md](https://github.com/highlightjs/highlight.js/blob/main/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/main/VERSION_10_BREAKING_CHANGES.md) for a more detailed list and [CHANGES.md](https://github.com/highlightjs/highlight.js/blob/main/CHANGES.md) to learn what else is new.
  31. ##### Support for older versions
  32. Please see [SECURITY.md](https://github.com/highlightjs/highlight.js/blob/main/SECURITY.md) for support information.
  33. ## Getting Started
  34. The bare minimum for using highlight.js on a web page is linking to the
  35. library along with one of the styles and calling [`highlightAll`][1]:
  36. ```html
  37. <link rel="stylesheet" href="/path/to/styles/default.css">
  38. <script src="/path/to/highlight.min.js"></script>
  39. <script>hljs.highlightAll();</script>
  40. ```
  41. This will find and highlight code inside of `<pre><code>` tags; it tries
  42. to detect the language automatically. If automatic detection doesn’t
  43. work for you, you can specify the language in the `class` attribute:
  44. ```html
  45. <pre><code class="html">...</code></pre>
  46. ```
  47. Classes may also be prefixed with either `language-` or `lang-`.
  48. ```html
  49. <pre><code class="language-html">...</code></pre>
  50. ```
  51. ### Plaintext and Disabling Highlighting
  52. To style arbitrary text like code, but without any highlighting, use the
  53. `plaintext` class:
  54. ```html
  55. <pre><code class="plaintext">...</code></pre>
  56. ```
  57. To disable highlighting of a tag completely, use the `nohighlight` class:
  58. ```html
  59. <pre><code class="nohighlight">...</code></pre>
  60. ```
  61. ### Supported Languages
  62. Highlight.js supports over 180 different languages in the core library. There are also 3rd party
  63. language plugins available for additional languages. You can find the full list of supported languages
  64. in [SUPPORTED_LANGUAGES.md][9].
  65. ## Custom Scenarios
  66. When you need a bit more control over the initialization of
  67. highlight.js, you can use the [`highlightBlock`][3] and [`configure`][4]
  68. functions. This allows you to better control *what* to highlight and *when*.
  69. Here’s the equivalent of calling [`highlightAll`][1] using
  70. only vanilla JS:
  71. ```js
  72. document.addEventListener('DOMContentLoaded', (event) => {
  73. document.querySelectorAll('pre code').forEach((block) => {
  74. hljs.highlightBlock(block);
  75. });
  76. });
  77. ```
  78. Please refer to the documentation for [`configure`][4] options.
  79. ### Using custom HTML elements for code blocks
  80. We strongly recommend `<pre><code>` wrapping for code blocks. It's quite
  81. semantic and "just works" out of the box with zero fiddling. It is possible to
  82. use other HTML elements (or combos), but you may need to pay special attention to
  83. preserving linebreaks.
  84. Let's say your markup for code blocks uses divs:
  85. ```html
  86. <div class='code'>...</div>
  87. ```
  88. To highlight such blocks manually:
  89. ```js
  90. // first, find all the div.code blocks
  91. document.querySelectorAll('div.code').forEach(block => {
  92. // then highlight each
  93. hljs.highlightBlock(block);
  94. });
  95. ```
  96. Without using a tag that preserves linebreaks (like `pre`) you'll need some
  97. additional CSS to help preserve them. You could also [pre and post-process line
  98. breaks with a plug-in][brPlugin], but *we recommend using CSS*.
  99. [brPlugin]: https://github.com/highlightjs/highlight.js/issues/2559
  100. To preserve linebreaks inside a `div` using CSS:
  101. ```css
  102. div.code {
  103. white-space: pre;
  104. }
  105. ```
  106. ## Using with Vue.js
  107. Simply register the plugin with Vue:
  108. ```js
  109. Vue.use(hljs.vuePlugin);
  110. ```
  111. And you'll be provided with a `highlightjs` component for use
  112. in your templates:
  113. ```html
  114. <div id="app">
  115. <!-- bind to a data property named `code` -->
  116. <highlightjs autodetect :code="code" />
  117. <!-- or literal code works as well -->
  118. <highlightjs language='javascript' code="var x = 5;" />
  119. </div>
  120. ```
  121. ## Web Workers
  122. You can run highlighting inside a web worker to avoid freezing the browser
  123. window while dealing with very big chunks of code.
  124. In your main script:
  125. ```js
  126. addEventListener('load', () => {
  127. const code = document.querySelector('#code');
  128. const worker = new Worker('worker.js');
  129. worker.onmessage = (event) => { code.innerHTML = event.data; }
  130. worker.postMessage(code.textContent);
  131. });
  132. ```
  133. In worker.js:
  134. ```js
  135. onmessage = (event) => {
  136. importScripts('<path>/highlight.min.js');
  137. const result = self.hljs.highlightAuto(event.data);
  138. postMessage(result.value);
  139. };
  140. ```
  141. ## Node.js
  142. You can use highlight.js with node to highlight content before sending it to the browser.
  143. Make sure to use the `.value` property to get the formatted html.
  144. For more info about the returned object refer to the [api docs](https://highlightjs.readthedocs.io/en/latest/api.html).
  145. ```js
  146. // require the highlight.js library, including all languages
  147. const hljs = require('./highlight.js');
  148. const highlightedCode = hljs.highlightAuto('<span>Hello World!</span>').value
  149. ```
  150. Or for a smaller footprint... load just the languages you need.
  151. ```js
  152. const hljs = require('highlight.js/lib/core'); // require only the core library
  153. // separately require languages
  154. hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml'));
  155. const highlightedCode = hljs.highlight('<span>Hello World!</span>', {language: 'xml'}).value
  156. ```
  157. ## ES6 Modules
  158. First, you'll likely install via `npm` or `yarn` -- see [Getting the Library](#getting-the-library) below.
  159. In your application:
  160. ```js
  161. import hljs from 'highlight.js';
  162. ```
  163. The default import imports all languages. Therefore it is likely to be more efficient to import only the library and the languages you need:
  164. ```js
  165. import hljs from 'highlight.js/lib/core';
  166. import javascript from 'highlight.js/lib/languages/javascript';
  167. hljs.registerLanguage('javascript', javascript);
  168. ```
  169. 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:
  170. ```js
  171. import hljs from 'highlight.js/lib/core';
  172. import 'highlight.js/styles/github.css';
  173. ```
  174. ## Getting the Library
  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. **Do not link to GitHub directly.** The library is not supposed to work straight
  182. from the source, it requires building. If none of the pre-packaged options
  183. work for you refer to the [building documentation][6].
  184. **On Almond.** You need to use the optimizer to give the module a name. For
  185. example:
  186. ```bash
  187. r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
  188. ```
  189. ### CDN Hosted
  190. A prebuilt version of Highlight.js bundled with many common languages is hosted by several popular CDNs.
  191. When using Highlight.js via CDN you can use Subresource Integrity for additional security. For details
  192. see [DIGESTS.md](https://github.com/highlightjs/cdn-release/blob/main/DIGESTS.md).
  193. **cdnjs** ([link](https://cdnjs.com/libraries/highlight.js))
  194. ```html
  195. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.3/styles/default.min.css">
  196. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.3/highlight.min.js"></script>
  197. <!-- and it's easy to individually load additional languages -->
  198. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.3/languages/go.min.js"></script>
  199. ```
  200. **jsdelivr** ([link](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release))
  201. ```html
  202. <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.7.3/build/styles/default.min.css">
  203. <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.7.3/build/highlight.min.js"></script>
  204. <!-- and it's easy to individually load additional languages -->
  205. <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.7.3/build/languages/go.min.js"></script>
  206. ```
  207. **unpkg** ([link](https://unpkg.com/browse/@highlightjs/cdn-assets/))
  208. ```html
  209. <link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@10.7.3/styles/default.min.css">
  210. <script src="https://unpkg.com/@highlightjs/cdn-assets@10.7.3/highlight.min.js"></script>
  211. <!-- and it's easy to individually load additional languages -->
  212. <script src="https://unpkg.com/@highlightjs/cdn-assets@10.7.3/languages/go.min.js"></script>
  213. ```
  214. **Note:** *The CDN-hosted `highlight.min.js` package doesn't bundle every language.* It would be
  215. very large. You can find our list of "common" languages that we bundle by default on our [download page][5].
  216. ### Self Hosting
  217. The [download page][5] can quickly generate a custom bundle including only the languages you need.
  218. Alternatively, you can build a browser package from [source](#source):
  219. ```
  220. node tools/build.js -t browser :common
  221. ```
  222. See our [building documentation][6] for more information.
  223. **Note:** Building from source should always result in the smallest size builds. The website download page is optimized for speed, not size.
  224. #### Prebuilt CDN assets
  225. 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.
  226. 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.
  227. ### NPM / Node.js server module
  228. Highlight.js can also be used on the server. The package with all supported languages can be installed from NPM or Yarn:
  229. ```bash
  230. npm install highlight.js
  231. # or
  232. yarn add highlight.js
  233. ```
  234. Alternatively, you can build it from [source](#source):
  235. ```bash
  236. node tools/build.js -t node
  237. ```
  238. See our [building documentation][6] for more information.
  239. ### Source
  240. [Current source][10] is always available on GitHub.
  241. ## License
  242. Highlight.js is released under the BSD License. See [LICENSE][7] file
  243. for details.
  244. ## Links
  245. The official site for the library is at <https://highlightjs.org/>.
  246. Further in-depth documentation for the API and other topics is at
  247. <http://highlightjs.readthedocs.io/>.
  248. A list of the Core Team and contributors can be found in the [CONTRIBUTORS.md][8] file.
  249. [1]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightall
  250. [2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
  251. [3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightblock-block
  252. [4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure-options
  253. [5]: https://highlightjs.org/download/
  254. [6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
  255. [7]: https://github.com/highlightjs/highlight.js/blob/main/LICENSE
  256. [8]: https://github.com/highlightjs/highlight.js/blob/main/CONTRIBUTORS.md
  257. [9]: https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
  258. [10]: https://github.com/highlightjs/