49c9536902b03bde90067b90d76cdda444b7fbeeb5f6eb5f7e0b67f29f758122a9430ce6cc81b617fda939b424c2738b2aa02979ca318517b882d429091d24 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. # Highlight.js
  2. [![this version](https://badgen.net/badge/this%20version/v9.18.4/red)](https://github.com/highlightjs/highlight.js/blob/master/VERSION_10_UPGRADE.md)
  3. [![supported](https://badgen.net/badge/supported/no/red)](https://github.com/highlightjs/highlight.js/blob/master/VERSION_10_UPGRADE.md)
  4. [![vulnerabilities](https://badgen.net/badge/vulnerabilities/possible/red)](https://github.com/highlightjs/highlight.js/blob/master/SECURITY.md)
  5. [![latest version](https://badgen.net/npm/v/highlight.js?label=latest)](https://www.npmjs.com/package/highlight.js)
  6. [![beta](https://badgen.net/npm/v/highlight.js/beta)](https://www.npmjs.com/package/highlight.js)
  7. [![slack](https://badgen.net/badge/icon/slack?icon=slack&label&color=pink)](https://join.slack.com/t/highlightjs/shared_invite/zt-jatdlkw4-h3LdjU5rC23t7aQ6zqoxzw)
  8. ---
  9. ### :warning: :warning: Important: Version 9 is no longer supported. :warning: :warning:
  10. Version 9 has reached end-of-support and will receive no future updates.
  11. It may include unpatched security vulnerabiites. Please upgrade to version 10.
  12. *The last release was 9.18.4 on November 18, 2020.*
  13. Please see [VERSION_10_UPGRADE.md](https://github.com/highlightjs/highlight.js/blob/master/VERSION_10_UPGRADE.md) and perhaps [SECURITY.md](https://github.com/highlightjs/highlight.js/blob/master/SECURITY.md).
  14. ---
  15. Highlight.js is a syntax highlighter written in JavaScript. It works in
  16. the browser as well as on the server. It works with pretty much any
  17. markup, doesn’t depend on any framework, and has automatic language
  18. detection.
  19. ## Getting Started
  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. ```html
  24. <link rel="stylesheet" href="/path/to/styles/default.css">
  25. <script src="/path/to/highlight.pack.js"></script>
  26. <script>hljs.initHighlightingOnLoad();</script>
  27. ```
  28. This will find and highlight code inside of `<pre><code>` tags; it tries
  29. to detect the language automatically. If automatic detection doesn’t
  30. work for you, you can specify the language in the `class` attribute:
  31. ```html
  32. <pre><code class="html">...</code></pre>
  33. ```
  34. Classes may also be prefixed with either `language-` or `lang-`.
  35. ```html
  36. <pre><code class="language-html">...</code></pre>
  37. ```
  38. ### Plaintext and Disabling Highlighting
  39. To style arbitrary text like code, but without any highlighting, use the
  40. `plaintext` class:
  41. ```html
  42. <pre><code class="plaintext">...</code></pre>
  43. ```
  44. To disable highlighting of a tag completely, use the `nohighlight` class:
  45. ```html
  46. <pre><code class="nohighlight">...</code></pre>
  47. ```
  48. ### Supported Languages
  49. The table below shows the full list of supported languages (and corresponding classes) that are bundled with the library. Note: Which languages are available may depend on how you've built or included the library in your app. See [Getting the Library](#getting-the-library) below.
  50. <details>
  51. <summary>Reveal the full list of languages...</summary>
  52. | Language | Classes | Package |
  53. | :-----------------------| :--------------------- | :------ |
  54. | 1C | 1c | |
  55. | ABNF | abnf | |
  56. | Access logs | accesslog | |
  57. | Ada | ada | |
  58. | ARM assembler | armasm, arm | |
  59. | AVR assembler | avrasm | |
  60. | ActionScript | actionscript, as | |
  61. | Alan | alan, i | [highlightjs-alan](https://github.com/highlightjs/highlightjs-alan) |
  62. | AngelScript | angelscript, asc | |
  63. | Apache | apache, apacheconf | |
  64. | AppleScript | applescript, osascript | |
  65. | Arcade | arcade | |
  66. | AsciiDoc | asciidoc, adoc | |
  67. | AspectJ | aspectj | |
  68. | AutoHotkey | autohotkey | |
  69. | AutoIt | autoit | |
  70. | Awk | awk, mawk, nawk, gawk | |
  71. | Axapta | axapta | |
  72. | Bash | bash, sh, zsh | |
  73. | Basic | basic | |
  74. | BNF | bnf | |
  75. | Brainfuck | brainfuck, bf | |
  76. | C# | cs, csharp | |
  77. | C++ | cpp, c, cc, h, c++, h++, hpp | |
  78. | C/AL | cal | |
  79. | Cache Object Script | cos, cls | |
  80. | CMake | cmake, cmake.in | |
  81. | Coq | coq | |
  82. | CSP | csp | |
  83. | CSS | css | |
  84. | Cap’n Proto | capnproto, capnp | |
  85. | Clojure | clojure, clj | |
  86. | CoffeeScript | coffeescript, coffee, cson, iced | |
  87. | Crmsh | crmsh, crm, pcmk | |
  88. | Crystal | crystal, cr | |
  89. | Cypher (Neo4j) | cypher | [highlightjs-cypher](https://github.com/highlightjs/highlightjs-cypher) |
  90. | D | d | |
  91. | DNS Zone file | dns, zone, bind | |
  92. | DOS | dos, bat, cmd | |
  93. | Dart | dart | |
  94. | Delphi | delphi, dpr, dfm, pas, pascal, freepascal, lazarus, lpr, lfm | |
  95. | Diff | diff, patch | |
  96. | Django | django, jinja | |
  97. | Dockerfile | dockerfile, docker | |
  98. | dsconfig | dsconfig | |
  99. | DTS (Device Tree) | dts | |
  100. | Dust | dust, dst | |
  101. | Dylan | dylan | [highlight-dylan](https://github.com/highlightjs/highlight-dylan) |
  102. | EBNF | ebnf | |
  103. | Elixir | elixir | |
  104. | Elm | elm | |
  105. | Erlang | erlang, erl | |
  106. | Excel | excel, xls, xlsx | |
  107. | Extempore | extempore, xtlang, xtm | [highlightjs-xtlang](https://github.com/highlightjs/highlightjs-xtlang) |
  108. | F# | fsharp, fs | |
  109. | FIX | fix | |
  110. | Fortran | fortran, f90, f95 | |
  111. | G-Code | gcode, nc | |
  112. | Gams | gams, gms | |
  113. | GAUSS | gauss, gss | |
  114. | GDScript | godot, gdscript | [highlightjs-gdscript](https://github.com/highlightjs/highlightjs-gdscript) |
  115. | Gherkin | gherkin | |
  116. | GN for Ninja | gn, gni | [highlightjs-GN](https://github.com/highlightjs/highlightjs-GN/blob/master/gn.js) |
  117. | Go | go, golang | |
  118. | Grammatical Framework | gf | [highlightjs-gf](https://github.com/johnjcamilleri/highlightjs-gf) |
  119. | Golo | golo, gololang | |
  120. | Gradle | gradle | |
  121. | Groovy | groovy | |
  122. | HTML, XML | xml, html, xhtml, rss, atom, xjb, xsd, xsl, plist, svg | |
  123. | HTTP | http, https | |
  124. | Haml | haml | |
  125. | Handlebars | handlebars, hbs, html.hbs, html.handlebars | |
  126. | Haskell | haskell, hs | |
  127. | Haxe | haxe, hx | |
  128. | Hy | hy, hylang | |
  129. | Ini, TOML | ini, toml | |
  130. | Inform7 | inform7, i7 | |
  131. | IRPF90 | irpf90 | |
  132. | JSON | json | |
  133. | Java | java, jsp | |
  134. | JavaScript | javascript, js, jsx | |
  135. | Kotlin | kotlin, kt | |
  136. | Leaf | leaf | |
  137. | Lasso | lasso, ls, lassoscript | |
  138. | Less | less | |
  139. | LDIF | ldif | |
  140. | Lisp | lisp | |
  141. | LiveCode Server | livecodeserver | |
  142. | LiveScript | livescript, ls | |
  143. | Lua | lua | |
  144. | Makefile | makefile, mk, mak | |
  145. | Markdown | markdown, md, mkdown, mkd | |
  146. | Mathematica | mathematica, mma, wl | |
  147. | Matlab | matlab | |
  148. | Maxima | maxima | |
  149. | Maya Embedded Language | mel | |
  150. | Mercury | mercury | |
  151. | mIRC Scripting Language | mirc, mrc | [highlightjs-mirc](https://github.com/highlightjs/highlightjs-mirc) |
  152. | Mizar | mizar | |
  153. | Mojolicious | mojolicious | |
  154. | Monkey | monkey | |
  155. | Moonscript | moonscript, moon | |
  156. | N1QL | n1ql | |
  157. | NSIS | nsis | |
  158. | Nginx | nginx, nginxconf | |
  159. | Nimrod | nimrod, nim | |
  160. | Nix | nix | |
  161. | OCaml | ocaml, ml | |
  162. | Objective C | objectivec, mm, objc, obj-c | |
  163. | OpenGL Shading Language | glsl | |
  164. | OpenSCAD | openscad, scad | |
  165. | Oracle Rules Language | ruleslanguage | |
  166. | Oxygene | oxygene | |
  167. | PF | pf, pf.conf | |
  168. | PHP | php, php3, php4, php5, php6, php7 | |
  169. | Parser3 | parser3 | |
  170. | Perl | perl, pl, pm | |
  171. | Plaintext: no highlight | plaintext | |
  172. | Pony | pony | |
  173. | PostgreSQL & PL/pgSQL | pgsql, postgres, postgresql | |
  174. | PowerShell | powershell, ps, ps1 | |
  175. | Processing | processing | |
  176. | Prolog | prolog | |
  177. | Properties | properties | |
  178. | Protocol Buffers | protobuf | |
  179. | Puppet | puppet, pp | |
  180. | Python | python, py, gyp | |
  181. | Python profiler results | profile | |
  182. | Q | k, kdb | |
  183. | QML | qml | |
  184. | R | r | |
  185. | Razor CSHTML | cshtml, razor, razor-cshtml | [highlightjs-cshtml-razor](https://github.com/highlightjs/highlightjs-cshtml-razor) |
  186. | ReasonML | reasonml, re | |
  187. | RenderMan RIB | rib | |
  188. | RenderMan RSL | rsl | |
  189. | Roboconf | graph, instances | |
  190. | Robot Framework | robot, rf | [highlightjs-robot](https://github.com/highlightjs/highlightjs-robot) |
  191. | RPM spec files | rpm-specfile, rpm, spec, rpm-spec, specfile | [highlightjs-rpm-specfile](https://github.com/highlightjs/highlightjs-rpm-specfile) |
  192. | Ruby | ruby, rb, gemspec, podspec, thor, irb | |
  193. | Rust | rust, rs | |
  194. | SAS | SAS, sas | |
  195. | SCSS | scss | |
  196. | SQL | sql | |
  197. | STEP Part 21 | p21, step, stp | |
  198. | Scala | scala | |
  199. | Scheme | scheme | |
  200. | Scilab | scilab, sci | |
  201. | Shape Expressions | shexc | [highlightjs-shexc](https://github.com/highlightjs/highlightjs-shexc) |
  202. | Shell | shell, console | |
  203. | Smali | smali | |
  204. | Smalltalk | smalltalk, st | |
  205. | Solidity | solidity, sol | [highlightjs-solidity](https://github.com/highlightjs/highlightjs-solidity) |
  206. | Stan | stan, stanfuncs | |
  207. | Stata | stata | |
  208. | Structured Text | iecst, scl, stl, structured-text | [highlightjs-structured-text](https://github.com/highlightjs/highlightjs-structured-text) |
  209. | Stylus | stylus, styl | |
  210. | SubUnit | subunit | |
  211. | Supercollider | supercollider, sc | [highlightjs-supercollider](https://github.com/highlightjs/highlightjs-supercollider) |
  212. | Swift | swift | |
  213. | Tcl | tcl, tk | |
  214. | Terraform (HCL) | terraform, tf, hcl | [highlightjs-terraform](https://github.com/highlightjs/highlightjs-terraform) |
  215. | Test Anything Protocol | tap | |
  216. | TeX | tex | |
  217. | Thrift | thrift | |
  218. | TP | tp | |
  219. | Twig | twig, craftcms | |
  220. | TypeScript | typescript, ts | |
  221. | VB.Net | vbnet, vb | |
  222. | VBScript | vbscript, vbs | |
  223. | VHDL | vhdl | |
  224. | Vala | vala | |
  225. | Verilog | verilog, v | |
  226. | Vim Script | vim | |
  227. | x86 Assembly | x86asm | |
  228. | XL | xl, tao | |
  229. | XQuery | xquery, xpath, xq | |
  230. | YAML | yml, yaml | |
  231. | Zephir | zephir, zep | |
  232. Languages with the specified package name are defined in separate repositories
  233. and not included in `highlight.pack.js`.
  234. </details>
  235. ## Custom Initialization
  236. When you need a bit more control over the initialization of
  237. highlight.js, you can use the [`highlightBlock`][3] and [`configure`][4]
  238. functions. This allows you to control *what* to highlight and *when*.
  239. Here’s an equivalent way to calling [`initHighlightingOnLoad`][1] using
  240. vanilla JS:
  241. ```js
  242. document.addEventListener('DOMContentLoaded', (event) => {
  243. document.querySelectorAll('pre code').forEach((block) => {
  244. hljs.highlightBlock(block);
  245. });
  246. });
  247. ```
  248. You can use any tags instead of `<pre><code>` to mark up your code. If
  249. you don't use a container that preserves line breaks you will need to
  250. configure highlight.js to use the `<br>` tag:
  251. ```js
  252. hljs.configure({useBR: true});
  253. document.querySelectorAll('div.code').forEach((block) => {
  254. hljs.highlightBlock(block);
  255. });
  256. ```
  257. For other options refer to the documentation for [`configure`][4].
  258. ## Web Workers
  259. You can run highlighting inside a web worker to avoid freezing the browser
  260. window while dealing with very big chunks of code.
  261. In your main script:
  262. ```js
  263. addEventListener('load', () => {
  264. const code = document.querySelector('#code');
  265. const worker = new Worker('worker.js');
  266. worker.onmessage = (event) => { code.innerHTML = event.data; }
  267. worker.postMessage(code.textContent);
  268. });
  269. ```
  270. In worker.js:
  271. ```js
  272. onmessage = (event) => {
  273. importScripts('<path>/highlight.pack.js');
  274. const result = self.hljs.highlightAuto(event.data);
  275. postMessage(result.value);
  276. };
  277. ```
  278. ## Node.js
  279. You can use highlight.js with node to highlight content before sending it to the browser.
  280. Make sure to use the `.value` property to get the formatted html.
  281. For more info about the returned object refer to the api docs https://highlightjs.readthedocs.io/en/latest/api.html
  282. ```js
  283. // require the highlight.js library including all languages
  284. const hljs = require('./highlight.js');
  285. const highlightedCode = hljs.highlightAuto('<span>Hello World!</span>').value
  286. ```
  287. ```js
  288. // require the highlight.js library without languages
  289. const hljs = require("highlight.js/lib/highlight.js");
  290. // separately require languages
  291. hljs.registerLanguage('html', require('highlight.js/lib/languages/html'));
  292. hljs.registerLanguage('sql', require('highlight.js/lib/languages/sql'));
  293. // highlight with providing the language
  294. const highlightedCode = hljs.highlight('html', '<span>Hello World!</span>').value
  295. ```
  296. ## Getting the Library
  297. You can get highlight.js as a hosted, or custom-build, browser script or
  298. as a server module. Right out of the box the browser script supports
  299. both AMD and CommonJS, so if you wish you can use RequireJS or
  300. Browserify without having to build from source. The server module also
  301. works perfectly fine with Browserify, but there is the option to use a
  302. build specific to browsers rather than something meant for a server.
  303. Head over to the [download page][5] for all the options.
  304. **Don't link to GitHub directly.** The library is not supposed to work straight
  305. from the source, it requires building. If none of the pre-packaged options
  306. work for you refer to the [building documentation][6].
  307. **The CDN-hosted package doesn't have all the languages.** Otherwise it'd be
  308. too big. If you don't see the language you need in the ["Common" section][5],
  309. it can be added manually:
  310. ```html
  311. <script
  312. charset="UTF-8"
  313. src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.5/languages/go.min.js"></script>
  314. ```
  315. **On Almond.** You need to use the optimizer to give the module a name. For
  316. example:
  317. ```bash
  318. r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
  319. ```
  320. ### CommonJS
  321. You can import Highlight.js as a CommonJS-module:
  322. ```bash
  323. npm install highlight.js --save
  324. ```
  325. In your application:
  326. ```js
  327. import hljs from 'highlight.js';
  328. ```
  329. The default import imports all languages! Therefore it is likely to be more efficient to import only the library and the languages you need:
  330. ```js
  331. import hljs from 'highlight.js/lib/highlight';
  332. import javascript from 'highlight.js/lib/languages/javascript';
  333. hljs.registerLanguage('javascript', javascript);
  334. ```
  335. 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:
  336. ```js
  337. import hljs from 'highlight.js/lib/highlight';
  338. import 'highlight.js/styles/github.css';
  339. ```
  340. ## License
  341. Highlight.js is released under the BSD License. See [LICENSE][7] file
  342. for details.
  343. ## Links
  344. The official site for the library is at <https://highlightjs.org/>.
  345. Further in-depth documentation for the API and other topics is at
  346. <http://highlightjs.readthedocs.io/>.
  347. Authors and contributors are listed in the [AUTHORS.txt][8] file.
  348. [1]: http://highlightjs.readthedocs.io/en/latest/api.html#inithighlightingonload
  349. [2]: http://highlightjs.readthedocs.io/en/latest/css-classes-reference.html
  350. [3]: http://highlightjs.readthedocs.io/en/latest/api.html#highlightblock-block
  351. [4]: http://highlightjs.readthedocs.io/en/latest/api.html#configure-options
  352. [5]: https://highlightjs.org/download/
  353. [6]: http://highlightjs.readthedocs.io/en/latest/building-testing.html
  354. [7]: https://github.com/highlightjs/highlight.js/blob/master/LICENSE
  355. [8]: https://github.com/highlightjs/highlight.js/blob/master/AUTHORS.txt