How to use the highlightAuto function from highlight.js

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

73
74
75
76
77
78
79
80
81

  // If you know the language
  hljs.highlight(lang, code).value;

  // Automatic language detection
  hljs.highlightAuto(code).value;
});
```

fork icon16
star icon19
watch icon31

216
217
218
219
220
221
222
223
224
225
const markdownCompiler = require('marked');
const highlightJs = require('highlight.js');

markdownCompiler.setOptions({
  renderer: new markdownCompiler.Renderer(),
  highlight: (code) => highlightJs.highlightAuto(code).value,
  pedantic: false,
  gfm: true,
  tables: true,
  breaks: false,
fork icon4
star icon49
watch icon4

117
118
119
120
121
122
123
124
125
126
127


  let hlHtml = '';
  if (lang) {
    hlHtml = hljs.highlight(lang, text).value
  } else {
    hlHtml = hljs.highlightAuto(text).value
  }
  return `<code class="hljs code-block">${hlHtml}</code>`
}

fork icon13
star icon33
watch icon7

+ 9 other calls in file

61
62
63
64
65
66
67
68
69
let ret;

if( lang ){
  ret = hljs.highlight(lang, code).value;
} else {
  ret = hljs.highlightAuto(code).value;
}

return ret;
fork icon4
star icon3
watch icon15

+ 9 other calls in file

134
135
136
137
138
139
140
141
142
143
  } catch (e) {
    console.error(e)
  }
}
if (!highlighted) {
  highlighted = highlight.highlightAuto(code).value;
}

return '<pre><code' +
  (lang ?
fork icon3
star icon2
watch icon4

18
19
20
21
22
23
24
25
26
27
    } catch (_error) {
      throw _error
    }
  }
  try {
    return hljs.highlightAuto(str).value
  } catch (_error) {
    throw _error
  }
}
fork icon1
star icon6
watch icon7

31
32
33
34
35
36
37
38
39
    out = hljs.highlight(lang, str);
  } catch (e) {
    out = hljs.highlightAuto(str);
  }
} else {
  out = hljs.highlightAuto(str);
}

if (!out || !out.value) throw new Error("failed to highlight");
fork icon0
star icon2
watch icon0

73
74
75
76
77
78
79
80
81
82
(head(conf) + $$('body')($$('p')(home + ' | ' + github) +
    marked.marked(s, {
        highlight: (code, lang) => {
            codeText += code
            if (typeof lang == 'undefined' || lang == '')
                return hljs.highlightAuto(code).value
            else if (lang == 'nohighlight')
                return code
            else return hljs.highlight(code, { language: lang }).value
        }
fork icon0
star icon2
watch icon1

+ 28 other calls in file

86
87
88
89
90
91
92
93
94
95
96
        
        if (!language) {
            return code;
        }
        
        return highlighter.highlightAuto(code).value;
    }
});


const ERRORS = {
fork icon0
star icon2
watch icon2

+ 2 other calls in file

43
44
45
46
47
48
49
50
51
52
53
54
55


default_rules.code_block = function (tokens, idx, options, env, slf) {
  var token = tokens[idx];


  return  '<pre' + slf.renderAttrs(token) + '><code>' +
          hljs.highlightAuto(tokens[idx].content).value +
          '</code></pre>\n';
};



fork icon0
star icon1
watch icon1

16
17
18
19
20
21
22
23
24
25
26
27
}


renderer.code = function(code, lang) {
    var language = lang && (' language-' + lang) || ''
    return '<pre class="prettyprint' + language + '">'
    + '<code class="hljs javascript">' + hljs.highlightAuto(code).value; + '</code>'
    + '</pre>'
}


marked.setOptions({
fork icon0
star icon1
watch icon2

439
440
441
442
443
444
445
446
447
448
449
450
		}
	});
});


router.post('/newCodeSnippet', methods.ensureAdmin, (req, res) => {
	const cCode = hljs.highlightAuto(req.body.code).value;


	const code = new Code({
		device: {
			name: req.body.device,
fork icon0
star icon0
watch icon1

219
220
221
222
223
224
225
226
227
    text = text.replace(/&#39;/g, '\'')
      .replace(/&gt;/g, '>')
      .replace(/&lt;/g, '<')
      .replace(/&quot;/g, '"')
      .replace(/&amp;/g, '&');
    var result = highlight.highlightAuto(text, language ? [language] : undefined);
    return `<pre><code class="hljs lang-${result.language}">${result.value}</code></pre>`;
  });
}
fork icon0
star icon0
watch icon1

73
74
75
76
77
78
79
80
81
82
//loaded(article) {
//article.calculatedData = doSomething();
//},
highlight(code, lang) {
	const languages = (lang != null) ? [lang] : undefined;
	return highlightjs.highlightAuto(code, languages).value;
},
// callback function for generating custom article groups.
// access the return value via the groupedArticles property, so that you can
// either return an array if you only have one group or return an object with
fork icon0
star icon0
watch icon1

+ 5 other calls in file

12
13
14
15
16
17
18
19
20
21
    return highlight.highlight(lang, str).value;
  } catch (err) {
    console.log(err)
  }
try {
  return highlight.highlightAuto(str).value;
} catch (err) {
  console.log(err)
}
return ''
fork icon0
star icon0
watch icon1

15
16
17
18
19
20
21
22
23
24
    console.log(`lang = '${lang}'`);
    if (lang !== '') {
      return hljs.highlight(code, { language: lang }).value;
    }
    else {
      return hljs.highlightAuto(code).value;
    }
  }
});

fork icon0
star icon0
watch icon1

+ 3 other calls in file

2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
    imagePreview.setAttribute('src', content);
    imagePreview.style.display = 'block';
    codePreview.style.display = 'none';
  }else{
    document.getElementById("codeview-badge").innerText = title;
    codePreview.innerHTML = hljs.highlightAuto(content).value;
    codePreview.style.display = 'block';
    imagePreview.style.display = "none";
  }
}
fork icon0
star icon0
watch icon1

+ 2 other calls in file

332
333
334
335
336
337
338
339
340
341
exercise.code = exercise.code.replace(/(^\n+)|(\s+$)/g, '') + '\n';

// Highlight.js doesn't always get it right with autodetection
var highlight = (exercise.lang in hljs.LANGUAGES) ?
                hljs.highlight(exercise.lang, exercise.code, true) :
                hljs.highlightAuto(exercise.code);

exercise.highlitCode = highlight.value;

// Remove comments because we don't want the player to type out
fork icon0
star icon0
watch icon1

+ 4 other calls in file

77
78
79
80
81
82
83
84
85
86
    clipboard: {
        matchVisual: true
    },
    toolbar: toolbarOptions,
    syntax: {
        highlight: (text) => highlight.highlightAuto(text).value,
    }
},
scrollingContainer: '#scrolling-container',
placeholder: "Digite aqui..."
fork icon0
star icon0
watch icon1

78
79
80
81
82
83
84
85
86
87

const html = marked(contents, {
    renderer,
    langPrefix: 'hljs ',
    highlight(code) {
        return highlight.highlightAuto(code).value;
    }
});

return {html, codes, catalogs};
fork icon0
star icon0
watch icon1