How to use the listLanguages function from highlight.js

Find comprehensive JavaScript highlight.js.listLanguages code examples handpicked from public code repositorys.

highlight.js.listLanguages is a function that returns an array of all the language names supported by the highlight.js library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14


const fs = require('fs')


const hljs = require('highlight.js')


const languages = hljs.listLanguages()


const result = {
  languages: languages,
  aliases: {},
fork icon6
star icon13
watch icon0

89
90
91
92
93
94
95
96
97
98
99
100
 *
 *   listLanguages()
 *   => ['javascript', 'python', 'c', ...]
 */


hicat.listLanguages = hljs.listLanguages;


/**
 * extname() : extname(filename)
 * (private) Extracts the extension from a given `filename`.
fork icon0
star icon2
watch icon0

How does highlight.js.listLanguages work?

The highlight.js.listLanguages() function is a method provided by the highlight.js library that returns an array of all the languages supported by the library. Internally, the listLanguages() function retrieves a list of all the registered languages from a global registry maintained by the library. The registry contains information about the language syntax, keywords, and rules that are used to highlight code snippets. When listLanguages() is called, it simply returns an array of language names that can be used to select a specific language for highlighting.

10
11
12
13
14
15
16
17
18
19
20
}


$(function(){
    if($('.editor').length > 0){
        const jar = CodeJar(document.querySelector('.editor'), hljs.highlightElement);
        hljs.listLanguages().forEach((language)=>{
            if(language === 'plaintext'){
                $('#language-input').append(`<option selected value="${language}">${language.capitalize()}</option>`);
            }else{
                $('#language-input').append(`<option value="${language}">${language.capitalize()}</option>`);
fork icon0
star icon0
watch icon1

+ 9 other calls in file

66
67
68
69
70
71
72
73
74
75
76
77
*/


// -----------------------------------------------------------------------------
function initExtensions () {
  const result = []
  const languages = highlight.listLanguages()
  const skipExtensions = new Set('md markdown'.split(' '))


  for (let language of languages) {
    if (skipExtensions.has(language)) continue
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
const hljs = require("highlight.js");

const languages = hljs.listLanguages();
console.log(languages);

This code will print an array of all the registered language names to the console.

23
24
25
26
27
28
29
30
31
32
33
34
});


// array of CM language names
const cmLanguages = Object.keys(langKeys);
// array of HL language names
const hlLangs = highlightJS.listLanguages();


/**
 * @param {String} filename
 * @returns {String|undefined} The highlight.js language extension, or undefined if unknown
fork icon0
star icon0
watch icon0