<h1 id="id-highlightjs">Highlight.js</h1> <p><a href="https://www.npmjs.com/package/highlight.js"><img src="https://badgen.net/npm/v/highlight.js?label=latest" alt="latest version"></a> <a href="https://github.com/highlightjs/highlight.js/blob/main/LICENSE"><img src="https://badgen.net/github/license/highlightjs/highlight.js?color=cyan" alt="license"></a> <a href="https://packagephobia.now.sh/result?p=highlight.js"><img src="https://badgen.net/packagephobia/install/highlight.js?label=npm+install" alt="install size"></a> <img src="https://img.shields.io/github/size/highlightjs/cdn-release/build/highlight.min.js?label=minified" alt="minified"> <a href="https://www.npmjs.com/package/highlight.js"><img src="https://badgen.net/npm/dw/highlight.js?label=npm+downloads&color=purple" alt="NPM downloads weekly"></a> <a href="https://www.jsdelivr.com/package/gh/highlightjs/cdn-release"><img src="https://badgen.net/jsdelivr/hits/gh/highlightjs/cdn-release?label=jsDelivr+CDN&color=purple" alt="jsDelivr CDN downloads"></a></p> <p><a href="https://github.com/highlightjs/highlight.js/actions/workflows/tests.js.yml"><img src="https://badgen.net/github/checks/highlightjs/highlight.js/main?label=build" alt="ci status"></a> <a href="https://github.com/highlightjs/highlight.js/actions/workflows/github-code-scanning/codeql"><img src="https://github.com/highlightjs/highlight.js/workflows/CodeQL/badge.svg" alt="CodeQL"></a> <a href="https://snyk.io/test/github/highlightjs/highlight.js?targetFile=package.json"><img src="https://badgen.net/snyk/highlightjs/highlight.js" alt="vulnerabilities"></a></p> <p><a href="https://discord.gg/M24EbU7ja9"><img src="https://badgen.net/badge/icon/discord?icon=discord&label&color=pink" alt="discord"></a> <a href="https://github.com/highlightjs/highlight.js/issues"><img src="https://badgen.net/github/open-issues/highlightjs/highlight.js?label=issues" alt="open issues"></a> <a href="https://github.com/highlightjs/highlight.js/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+welcome%22"><img src="https://badgen.net/github/label-issues/highlightjs/highlight.js/help%20welcome/open" alt="help welcome issues"></a> <a href="https://github.com/highlightjs/highlight.js/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22"><img src="https://badgen.net/github/label-issues/highlightjs/highlight.js/good%20first%20issue/open" alt="good first issue"></a></p> <!-- [](https://github.com/highlightjs/highlight.js/actions?query=workflow%3A%22Node.js+CI%22) --> <!-- [](https://github.com/highlightjs/highlight.js/commits/main) --> <!-- [](https://www.jsdelivr.com/package/gh/highlightjs/cdn-release) --> <!-- []() --> <!-- [](https://bundlephobia.com/result?p=highlight.js) --> <p>Highlight.js is a syntax highlighter written in JavaScript. It works in the browser as well as on the server. It can work with pretty much any markup, doesn’t depend on any other frameworks, and has automatic language detection.</p> <p><strong>Contents</strong></p> <ul> <li><a href="#basic-usage">Basic Usage</a><ul> <li><a href="#in-the-browser">In the Browser</a><ul> <li><a href="#plaintext-code-blocks">Plaintext Code Blocks</a></li> <li><a href="#ignoring-a-code-block">Ignoring a Code Block</a></li> </ul> </li> <li><a href="#nodejs-on-the-server">Node.js on the Server</a></li> </ul> </li> <li><a href="#supported-languages">Supported Languages</a></li> <li><a href="#custom-usage">Custom Usage</a><ul> <li><a href="#using-custom-html">Using custom HTML</a></li> <li><a href="#using-with-vuejs">Using with Vue.js</a></li> <li><a href="#using-web-workers">Using Web Workers</a></li> </ul> </li> <li><a href="#importing-the-library">Importing the Library</a><ul> <li><a href="#nodejs-commonjs-modules--require">Node.js CommonJS Modules / <code>require</code></a></li> <li><a href="#nodejs-es6-modules--import">Node.js ES6 Modules / <code>import</code></a></li> <li><a href="#browser-es6-modules">Browser ES6 Modules</a></li> </ul> </li> <li><a href="#getting-the-library">Getting the Library</a><ul> <li><a href="#fetch-via-cdn">Fetch via CDN</a><ul> <li><a href="#cdnjs-link">cdnjs (link)</a></li> <li><a href="#jsdelivr-link">jsdelivr (link)</a></li> <li><a href="#unpkg-link">unpkg (link)</a></li> </ul> </li> <li><a href="#download-prebuilt-cdn-assets">Download prebuilt CDN assets</a></li> <li><a href="#download-from-our-website">Download from our website</a></li> <li><a href="#install-via-npm-package">Install via NPM package</a></li> <li><a href="#build-from-source">Build from Source</a></li> </ul> </li> <li><a href="#requirements">Requirements</a></li> <li><a href="#license">License</a></li> <li><a href="#links">Links</a></li> </ul> <hr> <h4 id="id-upgrading-to-version-11">Upgrading to Version 11</h4> <p>As always, major releases do contain breaking changes which may require action from users. Please read <a href="https://github.com/highlightjs/highlight.js/blob/main/VERSION_11_UPGRADE.md">VERSION_11_UPGRADE.md</a> for a detailed summary of breaking changes and any actions you may need to take.</p> <h4 id="id-support-for-older-versions-">Support for older versions <!-- omit in toc --></h4> <p>Please see <a href="https://github.com/highlightjs/highlight.js/blob/main/SECURITY.md">SECURITY.md</a> for long-term support information.</p> <hr> <h2 id="id-basic-usage">Basic Usage</h2> <h3 id="id-in-the-browser">In the Browser</h3> <p>The bare minimum for using highlight.js on a web page is linking to the library along with one of the themes and calling <a href="http://highlightjs.readthedocs.io/en/latest/api.html#highlightall"><code>highlightAll</code></a>:</p> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/path/to/styles/default.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"/path/to/highlight.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span>></span>hljs.highlightAll();<span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <p>This will find and highlight code inside of <code><pre><code></code> tags; it tries to detect the language automatically. If automatic detection doesn’t work for you, or you simply prefer to be explicit, you can specify the language manually by using the <code>class</code> attribute:</p> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">pre</span>></span><span class="hljs-tag"><<span class="hljs-name">code</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"language-html"</span>></span>...<span class="hljs-tag"></<span class="hljs-name">code</span>></span><span class="hljs-tag"></<span class="hljs-name">pre</span>></span> </code></pre> <h4 id="id-plaintext-code-blocks">Plaintext Code Blocks</h4> <p>To apply the Highlight.js styling to plaintext without actually highlighting it, use the <code>plaintext</code> language:</p> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">pre</span>></span><span class="hljs-tag"><<span class="hljs-name">code</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"language-plaintext"</span>></span>...<span class="hljs-tag"></<span class="hljs-name">code</span>></span><span class="hljs-tag"></<span class="hljs-name">pre</span>></span> </code></pre> <h4 id="id-ignoring-a-code-block">Ignoring a Code Block</h4> <p>To skip highlighting of a code block completely, use the <code>nohighlight</code> class:</p> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">pre</span>></span><span class="hljs-tag"><<span class="hljs-name">code</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nohighlight"</span>></span>...<span class="hljs-tag"></<span class="hljs-name">code</span>></span><span class="hljs-tag"></<span class="hljs-name">pre</span>></span> </code></pre> <h3 id="id-nodejs-on-the-server">Node.js on the Server</h3> <p>The bare minimum to auto-detect the language and highlight some code.</p> <pre><code class="hljs language-js"><span class="hljs-comment">// load the library and ALL languages</span> hljs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'highlight.js'</span>); html = hljs.<span class="hljs-title function_">highlightAuto</span>(<span class="hljs-string">'<h1>Hello World!</h1>'</span>).<span class="hljs-property">value</span> </code></pre> <p>To load only a "common" subset of popular languages:</p> <pre><code class="hljs language-js">hljs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'highlight.js/lib/common'</span>); </code></pre> <p>To highlight code with a specific language, use <code>highlight</code>:</p> <pre><code class="hljs language-js">html = hljs.highlight('<span class="hljs-tag"><<span class="hljs-name">h1</span>></span>Hello World!<span class="hljs-tag"></<span class="hljs-name">h1</span>></span>', {language: 'xml'}).value </code></pre> <p>See <a href="#importing-the-library">Importing the Library</a> for more examples of <code>require</code> vs <code>import</code> usage, etc. For more information about the result object returned by <code>highlight</code> or <code>highlightAuto</code> refer to the <a href="https://highlightjs.readthedocs.io/en/latest/api.html">api docs</a>.</p> <h2 id="id-supported-languages">Supported Languages</h2> <p>Highlight.js supports over 180 languages in the core library. There are also 3rd party language definitions available to support even more languages. You can find the full list of supported languages in <a href="https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md">SUPPORTED_LANGUAGES.md</a>.</p> <h2 id="id-custom-usage">Custom Usage</h2> <p>If you need a bit more control over the initialization of Highlight.js, you can use the <a href="http://highlightjs.readthedocs.io/en/latest/api.html#highlightelement"><code>highlightElement</code></a> and <a href="http://highlightjs.readthedocs.io/en/latest/api.html#configure"><code>configure</code></a> functions. This allows you to better control <em>what</em> to highlight and <em>when</em>.</p> <p>For example, here’s the rough equivalent of calling <a href="http://highlightjs.readthedocs.io/en/latest/api.html#highlightall"><code>highlightAll</code></a> but doing the work manually instead:</p> <pre><code class="hljs language-js"><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'DOMContentLoaded'</span>, <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> { <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelectorAll</span>(<span class="hljs-string">'pre code'</span>).<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params">el</span>) =></span> { hljs.<span class="hljs-title function_">highlightElement</span>(el); }); }); </code></pre> <p>Please refer to the documentation for <a href="http://highlightjs.readthedocs.io/en/latest/api.html#configure"><code>configure</code></a> options.</p> <h3 id="id-using-custom-html">Using custom HTML</h3> <p>We strongly recommend <code><pre><code></code> wrapping for code blocks. It's quite semantic and "just works" out of the box with zero fiddling. It is possible to use other HTML elements (or combos), but you may need to pay special attention to preserving linebreaks.</p> <p>Let's say your markup for code blocks uses divs:</p> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">'code'</span>></span>...<span class="hljs-tag"></<span class="hljs-name">div</span>></span> </code></pre> <p>To highlight such blocks manually:</p> <pre><code class="hljs language-js"><span class="hljs-comment">// first, find all the div.code blocks</span> <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelectorAll</span>(<span class="hljs-string">'div.code'</span>).<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">el</span> =></span> { <span class="hljs-comment">// then highlight each</span> hljs.<span class="hljs-title function_">highlightElement</span>(el); }); </code></pre> <p>Without using a tag that preserves linebreaks (like <code>pre</code>) you'll need some additional CSS to help preserve them. You could also <a href="https://github.com/highlightjs/highlight.js/issues/2559">pre and post-process line breaks with a plug-in</a>, but <em>we recommend using CSS</em>.</p> <p>To preserve linebreaks inside a <code>div</code> using CSS:</p> <pre><code class="hljs language-css">div.code { white-space: pre; } </code></pre> <h3 id="id-using-with-vuejs">Using with Vue.js</h3> <p>See <a href="https://github.com/highlightjs/vue-plugin">highlightjs/vue-plugin</a> for a simple Vue plugin that works great with Highlight.js.</p> <p>An example of <code>vue-plugin</code> in action:</p> <pre><code class="hljs language-html"> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"app"</span>></span> <span class="hljs-comment"><!-- bind to a data property named `code` --></span> <span class="hljs-tag"><<span class="hljs-name">highlightjs</span> <span class="hljs-attr">autodetect</span> <span class="hljs-attr">:code</span>=<span class="hljs-string">"code"</span> /></span> <span class="hljs-comment"><!-- or literal code works as well --></span> <span class="hljs-tag"><<span class="hljs-name">highlightjs</span> <span class="hljs-attr">language</span>=<span class="hljs-string">'javascript'</span> <span class="hljs-attr">code</span>=<span class="hljs-string">"var x = 5;"</span> /></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> </code></pre> <h3 id="id-using-web-workers">Using Web Workers</h3> <p>You can run highlighting inside a web worker to avoid freezing the browser window while dealing with very big chunks of code.</p> <p>In your main script:</p> <pre><code class="hljs language-js"><span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'load'</span>, <span class="hljs-function">() =></span> { <span class="hljs-keyword">const</span> code = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">'#code'</span>); <span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">'worker.js'</span>); worker.<span class="hljs-property">onmessage</span> = <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> { code.<span class="hljs-property">innerHTML</span> = event.<span class="hljs-property">data</span>; } worker.<span class="hljs-title function_">postMessage</span>(code.<span class="hljs-property">textContent</span>); }); </code></pre> <p>In worker.js:</p> <pre><code class="hljs language-js">onmessage = <span class="hljs-function">(<span class="hljs-params">event</span>) =></span> { <span class="hljs-title function_">importScripts</span>(<span class="hljs-string">'<path>/highlight.min.js'</span>); <span class="hljs-keyword">const</span> result = self.<span class="hljs-property">hljs</span>.<span class="hljs-title function_">highlightAuto</span>(event.<span class="hljs-property">data</span>); <span class="hljs-title function_">postMessage</span>(result.<span class="hljs-property">value</span>); }; </code></pre> <h2 id="id-importing-the-library">Importing the Library</h2> <p>First, you'll likely be installing the library via <code>npm</code> or <code>yarn</code> -- see <a href="#getting-the-library">Getting the Library</a>.</p> <h3 id="id-nodejs-commonjs-modules--require">Node.js CommonJS Modules / <code>require</code></h3> <p>Requiring the top-level library will load all languages:</p> <pre><code class="hljs language-js"><span class="hljs-comment">// require the highlight.js library, including all languages</span> <span class="hljs-keyword">const</span> hljs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./highlight.js'</span>); <span class="hljs-keyword">const</span> highlightedCode = hljs.<span class="hljs-title function_">highlightAuto</span>(<span class="hljs-string">'<span>Hello World!</span>'</span>).<span class="hljs-property">value</span> </code></pre> <p>For a smaller footprint, load our common subset of languages (the same set used for our default web build).</p> <pre><code class="hljs language-js"><span class="hljs-keyword">const</span> hljs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'highlight.js/lib/common'</span>); </code></pre> <p>For the smallest footprint, load only the languages you need:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">const</span> hljs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'highlight.js/lib/core'</span>); hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'xml'</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">'highlight.js/lib/languages/xml'</span>)); <span class="hljs-keyword">const</span> highlightedCode = hljs.<span class="hljs-title function_">highlight</span>(<span class="hljs-string">'<span>Hello World!</span>'</span>, {<span class="hljs-attr">language</span>: <span class="hljs-string">'xml'</span>}).<span class="hljs-property">value</span> </code></pre> <h3 id="id-nodejs-es6-modules--import">Node.js ES6 Modules / <code>import</code></h3> <p>The default import will register all languages:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'highlight.js'</span>; </code></pre> <p>It is more efficient to import only the library and register the languages you need:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'highlight.js/lib/core'</span>; <span class="hljs-keyword">import</span> javascript <span class="hljs-keyword">from</span> <span class="hljs-string">'highlight.js/lib/languages/javascript'</span>; hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'javascript'</span>, javascript); </code></pre> <p>If your build tool processes CSS imports, you can also import the theme directly as a module:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'highlight.js'</span>; <span class="hljs-keyword">import</span> <span class="hljs-string">'highlight.js/styles/github.css'</span>; </code></pre> <h3 id="id-browser-es6-modules">Browser ES6 Modules</h3> <p><em>Note: For now you'll want to install <code>@highlightjs/cdn-assets</code> package instead of <code>highlight.js</code>. See <a href="#download-prebuilt-cdn-assets">Download prebuilt CDN assets</a></em></p> <p>To import the library and register only those languages that you need:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/js/@highlightjs/cdn-assets/es/core.js'</span>; <span class="hljs-keyword">import</span> javascript <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/js/@highlightjs/cdn-assets/es/languages/javascript.min.js'</span>; hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'javascript'</span>, javascript); </code></pre> <p>To import the library and register all languages:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'./assets/js/@highlightjs/cdn-assets/es/highlight.js'</span>; </code></pre> <p><em>Note: The path to these files will vary depending on where you have installed/copied them within your project or site. The above path is only an example.</em></p> <p>You can also use <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap"><code>importmap</code></a> to import in similar way as Node:</p> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"importmap"</span>></span><span class="language-javascript"> { <span class="hljs-string">"imports"</span>: { <span class="hljs-string">"@highlightjs"</span>: <span class="hljs-string">"./assets/js/@highlightjs/cdn-assets/es/"</span> } } </span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <p>Use the above code in your HTML. After that, your JavaScript can import using the named key from your <code>importmap</code>, for example <code>@highlightjs</code> in this case:</p> <pre><code class="hljs language-js"><span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'@highlightjs/core.js'</span>; <span class="hljs-keyword">import</span> javascript <span class="hljs-keyword">from</span> <span class="hljs-string">'@highlightjs/languages/javascript.min.js'</span>; hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'javascript'</span>, javascript); </code></pre> <p><em>Note: You can also import directly from fully static URLs, such as our very own pre-built ES6 Module CDN resources. See <a href="#fetch-via-cdn">Fetch via CDN</a> for specific examples.</em></p> <h2 id="id-getting-the-library">Getting the Library</h2> <p>You can get highlight.js as a hosted, or custom-build, browser script or as a server module. Right out of the box the browser script supports both AMD and CommonJS, so if you wish you can use RequireJS or Browserify without having to build from source. The server module also works perfectly fine with Browserify, but there is the option to use a build specific to browsers rather than something meant for a server.</p> <p><strong>Do not link to GitHub directly.</strong> The library is not supposed to work straight from the source, it requires building. If none of the pre-packaged options work for you refer to the <a href="http://highlightjs.readthedocs.io/en/latest/building-testing.html">building documentation</a>.</p> <p><strong>On Almond.</strong> You need to use the optimizer to give the module a name. For example:</p> <pre><code class="hljs language-bash">r.<span class="hljs-property">js</span> -o name=hljs paths.<span class="hljs-property">hljs</span>=<span class="hljs-regexp">/path/</span>to/highlight out=highlight.<span class="hljs-property">js</span> </code></pre> <h3 id="id-fetch-via-cdn">Fetch via CDN</h3> <p>A prebuilt version of Highlight.js bundled with many common languages is hosted by several popular CDNs. When using Highlight.js via CDN you can use Subresource Integrity for additional security. For details see <a href="https://github.com/highlightjs/cdn-release/blob/main/DIGESTS.md">DIGESTS.md</a>.</p> <h4 id="id-cdnjs-link">cdnjs (<a href="https://cdnjs.com/libraries/highlight.js">link</a>)</h4> <h5 id="id-common-js-">Common JS <!-- omit in toc --></h5> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/default.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> <span class="hljs-comment"><!-- and it's easy to individually load additional languages --></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/languages/go.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <h5 id="id-es6-modules-">ES6 Modules <!-- omit in toc --></h5> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/dark.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span>></span><span class="language-javascript"> <span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/es/highlight.min.js'</span>; <span class="hljs-comment">// and it's easy to individually load additional languages</span> <span class="hljs-keyword">import</span> go <span class="hljs-keyword">from</span> <span class="hljs-string">'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/es/languages/go.min.js'</span>; hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'go'</span>, go); </span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <h4 id="id-jsdelivr-link">jsdelivr (<a href="https://www.jsdelivr.com/package/gh/highlightjs/cdn-release">link</a>)</h4> <h5 id="id-common-js--1">Common JS <!-- omit in toc --></h5> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/styles/default.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/highlight.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> <span class="hljs-comment"><!-- and it's easy to individually load additional languages --></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/languages/go.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <h5 id="id-es6-modules--1">ES6 Modules <!-- omit in toc --></h5> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/styles/default.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span>></span><span class="language-javascript"> <span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/es/highlight.min.js'</span>; <span class="hljs-comment">// and it's easy to individually load additional languages</span> <span class="hljs-keyword">import</span> go <span class="hljs-keyword">from</span> <span class="hljs-string">'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.11.1/build/es/languages/go.min.js'</span>; hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'go'</span>, go); </span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <h4 id="id-unpkg-link">unpkg (<a href="https://unpkg.com/browse/@highlightjs/cdn-assets/">link</a>)</h4> <h5 id="id-common-js--2">Common JS <!-- omit in toc --></h5> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://unpkg.com/@highlightjs/cdn-assets@11.11.1/styles/default.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/@highlightjs/cdn-assets@11.11.1/highlight.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> <span class="hljs-comment"><!-- and it's easy to individually load additional languages --></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://unpkg.com/@highlightjs/cdn-assets@11.11.1/languages/go.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <h5 id="id-es6-modules--2">ES6 Modules <!-- omit in toc --></h5> <pre><code class="hljs language-html"><span class="hljs-tag"><<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://unpkg.com/@highlightjs/cdn-assets@11.11.1/styles/default.min.css"</span>></span> <span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"module"</span>></span><span class="language-javascript"> <span class="hljs-keyword">import</span> hljs <span class="hljs-keyword">from</span> <span class="hljs-string">'https://unpkg.com/@highlightjs/cdn-assets@11.11.1/es/highlight.min.js'</span>; <span class="hljs-comment">// and it's easy to individually load & register additional languages</span> <span class="hljs-keyword">import</span> go <span class="hljs-keyword">from</span> <span class="hljs-string">'https://unpkg.com/@highlightjs/cdn-assets@11.11.1/es/languages/go.min.js'</span>; hljs.<span class="hljs-title function_">registerLanguage</span>(<span class="hljs-string">'go'</span>, go); </span><span class="hljs-tag"></<span class="hljs-name">script</span>></span> </code></pre> <p><strong>Note:</strong> <em>The CDN-hosted <code>highlight.min.js</code> package doesn't bundle every language.</em> It would be very large. You can find our list of "common" languages that we bundle by default on our <a href="https://highlightjs.org/download/">download page</a>.</p> <h3 id="id-download-prebuilt-cdn-assets">Download prebuilt CDN assets</h3> <p>You can also download and self-host the same assets we serve up via our own CDNs. We publish those builds to the <a href="https://github.com/highlightjs/cdn-release">cdn-release</a> GitHub repository. You can easily pull individual files off the CDN endpoints with <code>curl</code>, etc; if say you only needed <code>highlight.min.js</code> and a single CSS file.</p> <p>There is also an npm package <a href="https://www.npmjs.com/package/@highlightjs/cdn-assets">@highlightjs/cdn-assets</a> if pulling the assets in via <code>npm</code> or <code>yarn</code> would be easier for your build process.</p> <h3 id="id-download-from-our-website">Download from our website</h3> <p>The <a href="https://highlightjs.org/download/">download page</a> can quickly generate a custom single-file minified bundle including only the languages you desire.</p> <p><strong>Note:</strong> <a href="#build-from-source">Building from source</a> can produce slightly smaller builds than the website download.</p> <h3 id="id-install-via-npm-package">Install via NPM package</h3> <p>Our NPM package including all supported languages can be installed with NPM or Yarn:</p> <pre><code class="hljs language-bash">npm install highlight.js <span class="hljs-comment"># or</span> yarn add highlight.js </code></pre> <p>There is also another npm package <a href="https://www.npmjs.com/package/@highlightjs/cdn-assets">@highlightjs/cdn-assets</a> that contains prebuilt CDN assets including <a href="#browser-es6-modules">ES6 Modules that can be imported in browser</a>:</p> <pre><code class="hljs language-bash">npm install @highlightjs/cdn-assets <span class="hljs-comment"># or</span> yarn add @highlightjs/cdn-assets </code></pre> <p>Alternatively, you can build the NPM package from source.</p> <h3 id="id-build-from-source">Build from Source</h3> <p>The <a href="https://github.com/highlightjs/">current source code</a> is always available on GitHub.</p> <pre><code class="hljs language-bash">node tools/build.js -t node node tools/build.js -t browser :common node tools/build.js -t cdn :common </code></pre> <p>See our <a href="http://highlightjs.readthedocs.io/en/latest/building-testing.html">building documentation</a> for more information.</p> <h2 id="id-requirements">Requirements</h2> <p>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:</p> <ul> <li>Node.js >= 12.x</li> <li>npm >= 6.x</li> </ul> <h2 id="id-license">License</h2> <p>Highlight.js is released under the BSD License. See our <a href="https://github.com/highlightjs/highlight.js/blob/main/LICENSE">LICENSE</a> file for details.</p> <h2 id="id-links">Links</h2> <p>The official website for the library is <a href="https://highlightjs.org/">https://highlightjs.org/</a>.</p> <p>Further in-depth documentation for the API and other topics is at <a href="http://highlightjs.readthedocs.io/">http://highlightjs.readthedocs.io/</a>.</p> <p>A list of the Core Team and contributors can be found in the <a href="https://github.com/highlightjs/highlight.js/blob/main/CONTRIBUTORS.md">CONTRIBUTORS.md</a> file.</p>