highlight.js is a popular open-source JavaScript library created to add syntax highlighting to code snippets on websites and web applications. Its main purpose is to make code easy to read and understand by coloring different parts of the syntax, such as keywords, comments, and strings. It works directly in the browser or on a server with Node.js. The library is not a company product but a community-driven project maintained by many contributors. It is known for its simplicity, extensive language support, and having no external dependencies, which makes it easy to integrate into any project.
highlight.js is a completely free and open-source software. It is distributed under the BSD 3-Clause License, which permits free use in both personal and commercial projects. There are no pricing plans, premium versions, or paid features. The entire library, including all languages and themes, is available for everyone to use at no cost.
As an open-source project, highlight.js does not have a 'free plan' because the entire tool is free. There are no limitations on its use. You get full access to all features, including:
There is no trial period or need to sign up. You can download or link to the library and start using it immediately.
There are two common ways to start using highlight.js.
1. Using a CDN for a simple website:
This is the easiest method for adding highlighting to a static website or blog.
<head>
of your HTML file. You can choose any theme you like. For example, the default theme:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
</body>
tag:
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<pre><code>
tags. You can let the library auto-detect the language or specify it in the class attribute:
<pre><code class="language-javascript">console.log('Hello, World!');</code></pre>
2. Using npm for a Node.js project:
This method is for developers building applications with Node.js.
npm install highlight.js
import hljs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
hljs.registerLanguage('javascript', javascript);
highlight
function to process your code string:
const code = "console.log('Hello!');";
const highlightedCode = hljs.highlight(code, { language: 'javascript' }).value;
Pros:
Cons:
highlight.js is a library, so it does not have direct integrations with SaaS applications like Salesforce or Outlook. Instead, it is designed to be integrated into other software projects. It works well with:
highlight.js provides a JavaScript API for developers to use its features programmatically. It is not a web API, so you do not need an API key. You can use the API directly in your JavaScript code after importing the library.
The main API functions are:
hljs.highlight(code, {language})
: Highlights a string of code for a specific language.hljs.highlightAuto(code)
: Highlights a string of code, automatically detecting the language.hljs.registerLanguage(name, language)
: Registers a language so it can be used for highlighting.hljs.highlightAll()
: Applies highlighting to all <pre><code>
elements on a webpage.Here is a basic developer example in a Node.js environment:
// Import the core library and the XML language
import hljs from 'highlight.js/lib/core';
import xml from 'highlight.js/lib/languages/xml';
// Register the language
hljs.registerLanguage('xml', xml);
const codeSnippet = '<span>Hello World!</span>';
// Use the API to highlight the code
const result = hljs.highlight(codeSnippet, { language: 'xml' });
console.log(result.value);
// Outputs: <span class="hljs-tag"><<span class="hljs-name">span</span>></span>Hello World!<span class="hljs-tag"></<span class="hljs-name">span</span>></span>
For more details, developers can consult the official API documentation on the highlight.js website.
highlight.js does not have an affiliate program. As a free, open-source project, there are no products to sell, so there is no commission-based system for referrals. The project is supported by its community of users and contributors. If you wish to support the project, you can contribute by reporting bugs, improving documentation, or submitting code changes on its GitHub repository. Some open-source projects also accept donations, which is the most direct way to provide financial support.