Gathering detailed insights and metrics for highlight.js
Gathering detailed insights and metrics for highlight.js
Gathering detailed insights and metrics for highlight.js
Gathering detailed insights and metrics for highlight.js
JavaScript syntax highlighter with language auto-detection and zero dependencies.
npm install highlight.js
99.5
Supply Chain
100
Quality
81.9
Maintenance
100
Vulnerability
100
License
11.10.0 - Welcome to the 2024 Mega-smorgisbord update.
Published on 15 Jul 2024
11.9.0 - Fall is in the air!
Published on 09 Oct 2023
11.8.0 "Spring has arrived" Edition
Published on 29 Apr 2023
11.7.0 - "Gobble Gobble" Thanksgiving Edition
Published on 23 Nov 2022
11.6.0 - "Pink Lemonade on a hot day" Edition
Published on 13 Jul 2022
Version 11.5.1
Published on 11 Apr 2022
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
23,773 Stars
6,661 Commits
3,609 Forks
306 Watching
20 Branches
718 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
JavaScript (63.77%)
CSS (34.85%)
HTML (0.76%)
R (0.34%)
Groovy (0.12%)
Hy (0.08%)
Shell (0.04%)
Scheme (0.03%)
Dockerfile (0.02%)
Cumulative downloads
Total Downloads
Last day
-4%
1,667,229
Compared to previous day
Last week
3.6%
9,628,921
Compared to previous week
Last month
14.8%
39,334,520
Compared to previous month
Last year
5.5%
379,800,920
Compared to previous year
34
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.
Contents
As always, major releases do contain breaking changes which may require action from users. Please read VERSION_11_UPGRADE.md for a detailed summary of breaking changes and any actions you may need to take.
Please see SECURITY.md for long-term support information.
The bare minimum for using highlight.js on a web page is linking to the
library along with one of the themes and calling highlightAll
:
1<link rel="stylesheet" href="/path/to/styles/default.min.css"> 2<script src="/path/to/highlight.min.js"></script> 3<script>hljs.highlightAll();</script>
This will find and highlight code inside of <pre><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 class
attribute:
1<pre><code class="language-html">...</code></pre>
To apply the Highlight.js styling to plaintext without actually highlighting it, use the plaintext
language:
1<pre><code class="language-plaintext">...</code></pre>
To skip highlighting of a code block completely, use the nohighlight
class:
1<pre><code class="nohighlight">...</code></pre>
The bare minimum to auto-detect the language and highlight some code.
1// load the library and ALL languages 2hljs = require('highlight.js'); 3html = hljs.highlightAuto('<h1>Hello World!</h1>').value
To load only a "common" subset of popular languages:
1hljs = require('highlight.js/lib/common');
To highlight code with a specific language, use highlight
:
1html = hljs.highlight('<h1>Hello World!</h1>', {language: 'xml'}).value
See 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.
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 SUPPORTED_LANGUAGES.md.
If you need a bit more control over the initialization of
Highlight.js, you can use the highlightElement
and configure
functions. This allows you to better control what to highlight and when.
For example, here’s the rough equivalent of calling highlightAll
but doing the work manually instead:
1document.addEventListener('DOMContentLoaded', (event) => { 2 document.querySelectorAll('pre code').forEach((el) => { 3 hljs.highlightElement(el); 4 }); 5});
Please refer to the documentation for configure
options.
We strongly recommend <pre><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.
Let's say your markup for code blocks uses divs:
1<div class='code'>...</div>
To highlight such blocks manually:
1// first, find all the div.code blocks
2document.querySelectorAll('div.code').forEach(el => {
3 // then highlight each
4 hljs.highlightElement(el);
5});
Without using a tag that preserves linebreaks (like pre
) you'll need some
additional CSS to help preserve them. You could also pre and post-process line
breaks with a plug-in, but we recommend using CSS.
To preserve linebreaks inside a div
using CSS:
1div.code { 2 white-space: pre; 3}
See highlightjs/vue-plugin for a simple Vue plugin that works great with Highlight.js.
An example of vue-plugin
in action:
1 <div id="app"> 2 <!-- bind to a data property named `code` --> 3 <highlightjs autodetect :code="code" /> 4 <!-- or literal code works as well --> 5 <highlightjs language='javascript' code="var x = 5;" /> 6 </div>
You can run highlighting inside a web worker to avoid freezing the browser window while dealing with very big chunks of code.
In your main script:
1addEventListener('load', () => { 2 const code = document.querySelector('#code'); 3 const worker = new Worker('worker.js'); 4 worker.onmessage = (event) => { code.innerHTML = event.data; } 5 worker.postMessage(code.textContent); 6});
In worker.js:
1onmessage = (event) => {
2 importScripts('<path>/highlight.min.js');
3 const result = self.hljs.highlightAuto(event.data);
4 postMessage(result.value);
5};
First, you'll likely be installing the library via npm
or yarn
-- see Getting the Library.
require
Requiring the top-level library will load all languages:
1// require the highlight.js library, including all languages 2const hljs = require('./highlight.js'); 3const highlightedCode = hljs.highlightAuto('<span>Hello World!</span>').value
For a smaller footprint, load our common subset of languages (the same set used for our default web build).
1const hljs = require('highlight.js/lib/common');
For the smallest footprint, load only the languages you need:
1const hljs = require('highlight.js/lib/core'); 2hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml')); 3 4const highlightedCode = hljs.highlight('<span>Hello World!</span>', {language: 'xml'}).value
import
The default import will register all languages:
1import hljs from 'highlight.js';
It is more efficient to import only the library and register the languages you need:
1import hljs from 'highlight.js/lib/core'; 2import javascript from 'highlight.js/lib/languages/javascript'; 3hljs.registerLanguage('javascript', javascript);
If your build tool processes CSS imports, you can also import the theme directly as a module:
1import hljs from 'highlight.js'; 2import 'highlight.js/styles/github.css';
Note: For now you'll want to install @highlightjs/cdn-assets
package instead of highlight.js
.
See Download prebuilt CDN assets
To import the library and register only those languages that you need:
1import hljs from './assets/js/@highlightjs/cdn-assets/es/core.js'; 2import javascript from './assets/js/@highlightjs/cdn-assets/es/languages/javascript.min.js'; 3 4hljs.registerLanguage('javascript', javascript);
To import the library and register all languages:
1import hljs from './assets/js/@highlightjs/cdn-assets/es/highlight.js';
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.
You can also use importmap
to import in similar way as Node:
1<script type="importmap"> 2{ 3 "imports": { 4 "@highlightjs": "./assets/js/@highlightjs/cdn-assets/es/" 5 } 6} 7</script>
Use the above code in your HTML. After that, your JavaScript can import using the named key from
your importmap
, for example @highlightjs
in this case:
1import hljs from '@highlightjs/core.js'; 2import javascript from '@highlightjs/languages/javascript.min.js'; 3 4hljs.registerLanguage('javascript', javascript);
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 for specific examples.
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.
Do not link to GitHub directly. 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 building documentation.
On Almond. You need to use the optimizer to give the module a name. For example:
1r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
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 DIGESTS.md.
1<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/styles/default.min.css"> 2<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/highlight.min.js"></script> 3<!-- and it's easy to individually load additional languages --> 4<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/languages/go.min.js"></script>
1<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/styles/dark.min.css"> 2<script type="module"> 3import hljs from 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/es/highlight.min.js'; 4// and it's easy to individually load additional languages 5import go from 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.10.0/es/languages/go.min.js'; 6hljs.registerLanguage('go', go); 7</script> 8
1<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.10.0/build/styles/default.min.css"> 2<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.10.0/build/highlight.min.js"></script> 3<!-- and it's easy to individually load additional languages --> 4<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.10.0/build/languages/go.min.js"></script>
1<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.10.0/build/styles/default.min.css"> 2<script type="module"> 3import hljs from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.10.0/build/es/highlight.min.js'; 4// and it's easy to individually load additional languages 5import go from 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.10.0/build/es/languages/go.min.js'; 6hljs.registerLanguage('go', go); 7</script>
1<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.10.0/styles/default.min.css"> 2<script src="https://unpkg.com/@highlightjs/cdn-assets@11.10.0/highlight.min.js"></script> 3<!-- and it's easy to individually load additional languages --> 4<script src="https://unpkg.com/@highlightjs/cdn-assets@11.10.0/languages/go.min.js"></script>
1<link rel="stylesheet" href="https://unpkg.com/@highlightjs/cdn-assets@11.10.0/styles/default.min.css"> 2<script type="module"> 3import hljs from 'https://unpkg.com/@highlightjs/cdn-assets@11.10.0/es/highlight.min.js'; 4// and it's easy to individually load & register additional languages 5import go from 'https://unpkg.com/@highlightjs/cdn-assets@11.10.0/es/languages/go.min.js'; 6hljs.registerLanguage('go', go); 7</script>
Note: The CDN-hosted highlight.min.js
package doesn't bundle every language. It would be
very large. You can find our list of "common" languages that we bundle by default on our download page.
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 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.
There is also an npm package @highlightjs/cdn-assets if pulling the assets in via npm
or yarn
would be easier for your build process.
The download page can quickly generate a custom single-file minified bundle including only the languages you desire.
Note: Building from source can produce slightly smaller builds than the website download.
Our NPM package including all supported languages can be installed with NPM or Yarn:
1npm install highlight.js 2# or 3yarn add highlight.js
There is also another npm package @highlightjs/cdn-assets that contains prebuilt CDN assets including ES6 Modules that can be imported in browser:
1npm install @highlightjs/cdn-assets 2# or 3yarn add @highlightjs/cdn-assets
Alternatively, you can build the NPM package from source.
The current source code is always available on GitHub.
1node tools/build.js -t node 2node tools/build.js -t browser :common 3node tools/build.js -t cdn :common
See our building documentation for more information.
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:
Highlight.js is released under the BSD License. See our LICENSE file for details.
The official website for the library is https://highlightjs.org/.
Further in-depth documentation for the API and other topics is at http://highlightjs.readthedocs.io/.
A list of the Core Team and contributors can be found in the CONTRIBUTORS.md file.
The latest stable version of the package.
Stable Version
3
0/10
Summary
ReDOS vulnerabities: multiple grammars
Affected Versions
>= 9.0.0, < 10.4.1
Patched Versions
10.4.1
5.8/10
Summary
Prototype Pollution in highlight.js
Affected Versions
>= 10.0.0, < 10.1.2
Patched Versions
10.1.2
5.8/10
Summary
Prototype Pollution in highlight.js
Affected Versions
< 9.18.2
Patched Versions
9.18.2
Reason
no dangerous workflow patterns detected
Reason
23 commit(s) and 21 issue activity found in the last 90 days -- score normalized to 10
Reason
security policy file detected
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 28/30 approved changesets -- score normalized to 9
Reason
7 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More