How to use the getLanguage function from highlight.js

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

highlight.js.getLanguage is a function that returns an object containing information about a specified programming language for syntax highlighting.

86
87
88
89
90
91
92
93
94
95
let highlightRules = [];
if (highlightLinesInput) {
  highlightRules = HighlightRule.parseAllRules(highlightLinesInput, -startFromZeroBased, str);
}

if (lang && hljs.getLanguage(lang)) {
  try {
    /* With highlightjs version >= v10.7.0, usage of continuation is deprecated

    For the purposes of line-by-line highlighting, we have to first highlight the
fork icon105
star icon95
watch icon13

+ 5 other calls in file

26
27
28
29
30
31
32
33
34
35
36
const apiSrcPath = 'src/api/';
const apiDestFile = 'public/api.json';


marked.setOptions({
  highlight: (code, lang) => {
    const language = hljs.getLanguage(lang) ? lang : 'plaintext';
    return hljs.highlight(code, { language }).value;
  },
});

fork icon23
star icon64
watch icon31

How does highlight.js.getLanguage work?

highlight.js.getLanguage is a function provided by the highlight.js library that retrieves a language definition object for a given language name, which is used to highlight syntax in code snippets. When called, highlight.js.getLanguage searches the library's internal registry for the language definition object associated with the specified language name, and returns it if found. If the language definition object is not found, undefined is returned instead.

15
16
17
18
19
20
21
22
23
24
const clearModule = require('clear-module');
const readingTime = require('reading-time');
const markdown = new markdownIt({
    html: true,
    highlight: (str, lang) => {
        if(lang && highlightjs.getLanguage(lang)){
            try{
                return highlightjs.highlight(str, { language: lang }).value;
            }catch(ex){
                console.log('Failed to highlight code block', ex);
fork icon7
star icon48
watch icon3

+ 25 other calls in file

9
10
11
12
13
14
15
16
17
18
 * highlight.js config
 * @param {*} str
 * @param {*} lang
 */
const highlight = (str, lang) => {
  if (lang != null && hljs.getLanguage(lang)) {
    try {
      const code = hljs.highlight(lang, str, true).value
      return `<pre class="hljs"><code>${code}</code></pre>`
    } catch (_error) {
fork icon1
star icon6
watch icon7

Ai Example

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

const javascriptLang = hljs.getLanguage("javascript");
console.log(javascriptLang); // returns an object containing the syntax highlighting rules for JavaScript

181
182
183
184
185
186
187
188
189
190
  console.warn(
    `No language set in ${filename}, ` +
    `line ${node.position.start.line}`);
}
const language = (node.lang || '').split(' ')[0];
const highlighted = getLanguage(language) ?
  highlight(language, node.value).value :
  node.value;
node.type = 'html';
node.value = '<pre>' +
fork icon2
star icon0
watch icon2

24
25
26
27
28
29
30
31
32
33
html: true,
breaks: appconfig.features.linebreaks,
linkify: true,
typography: true,
highlight(str, lang) {
  if (appconfig.theme.code.colorize && lang && hljs.getLanguage(lang)) {
    try {
      return '<pre class="hljs"><code>' + hljs.highlight(lang, str, true).value + '</code></pre>'
    } catch (err) {
      return '<pre><code>' + _.escape(str) + '</code></pre>'
fork icon0
star icon1
watch icon1

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


import { stickerImage, stickerName } from './sticker-pack';


marked.setOptions({
  highlight: (code, lang) => {
    lang = hljs.getLanguage(lang) ? lang : 'plaintext';
    return hljs.highlight(lang, code).value;
  },
  gfm: true,
  silent: true,
fork icon0
star icon0
watch icon1

+ 2 other calls in file

3
4
5
6
7
8
9
10
11
12
const md = require('markdown-it')({
    html: true,
    linkify: false,
    typographer: false,
    highlight: function(code, lang) {
        if ( lang && hljs.getLanguage(lang) ) {
            try {
                return '<pre class="hljs"><code>' + 
                    hljs.highlight(code, { language: lang, ignoreIllegals: true }).value +
                    '</code></pre>';
fork icon0
star icon0
watch icon2

37
38
39
40
41
42
43
44
45
46
47
48
    .sort((first, second) => (first > second ? 1 : -1))
}


/** @type {(language: string) => Promise<Language>} */
async function getLanguage(language) {
  const hljsLanguage = hljs.getLanguage(language)
  if (!hljsLanguage)
    throw new Error(`Language ${JSON.stringify(language)} not found.`)


  const aliases = (hljsLanguage.aliases || [])
fork icon0
star icon0
watch icon1

+ 3 other calls in file

5
6
7
8
9
10
11
12
13
14
const highlight = require('highlight.js')
const pdf = require('html-pdf')
const commander = require('commander')
const md = new Remarkable({
  highlight: (str, lang) => {
    if (lang && highlight.getLanguage(lang))
      try {
        return highlight.highlight(lang, str).value;
      } catch (err) {
        console.log(err)
fork icon0
star icon0
watch icon1

74
75
76
77
78
79
80
81
82
for (let language of languages) {
  if (skipExtensions.has(language)) continue

  result.push(language)

  const lang = highlight.getLanguage(language)
  if (lang.aliases) {
    for (let alias of lang.aliases) {
      if (skipExtensions.has(alias)) continue
fork icon0
star icon0
watch icon0

1
2
3
4
5
6
7
8
9
10
11
const fs = require("fs");
const fsPromise = require("fs/promises");


const md = require("markdown-it")({
    highlight: function (str, lang) {
        if (lang && highlight.getLanguage(lang)) {
            try {
                return highlight.highlight(str, {language: lang}).value;
            } catch (__) {}
        }
fork icon0
star icon0
watch icon0

27
28
29
30
31
32
33
34
35
36
// }
// linesNum += "</span>";
// if (lang == null ||lang=="") {
//     lang = "css";
// }
// if (lang && hljs.getLanguage(lang)) {
//     console.log(1);
//     // highlight.js 高亮代码
//     const preCode = hljs.highlight(lang, str, true).value;
//     html = html + preCode;
fork icon0
star icon0
watch icon0

+ 3 other calls in file