How to use highlight.js

Comprehensive highlight.js code examples:

How to use highlight.js.js:

337
338
339
340
341
342
343
344
345
346
347
    })
    return current;
  },
}


// Configure highlight.js
highlight.configure({
  // "useBR": true
})

How to use highlight.js.configure:

95
96
97
98
99
100
101
102
103
104
with some fixed number of spaces or with a `<span>` to give them special
styling:

```html
<script type="text/javascript">
  hljs.configure({tabReplace: '    '}); // 4 spaces
  // ... or
  hljs.configure({tabReplace: '<span class="indent">\t</span>'});

  hljs.initHighlightingOnLoad();

How to use highlight.js.listLanguages:

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: {},

How to use highlight.js.highlightAll:

13
14
15
16
17
18
19
20
21
22

```html
<script type="text/javascript" src="/path/to/highlight.min.js"></script>
<script type="text/javascript" src="/path/to/curl.min.js"></script>
<script type="text/javascript">
  hljs.highlightAll();
</script>
```

### Using directly from the UNPKG CDN

How to use highlight.js.initHighlightingOnLoad:

90
91
92
93
94
95
96
97
98
99
<script type="text/javascript">
  hljs.tabReplace = '    '; // 4 spaces
  // ... or
  hljs.tabReplace = '<span class="indent">\t</span>';

  hljs.initHighlightingOnLoad();
</script>
```

## Custom initialization

How to use highlight.js.highlightElement:

366
367
368
369
370
371
372
373
374
375
376
      <button class="fa fa-trash-o delete-button"></button>
    </div>
    ${converter.makeHtml(content)}
  `;
  // highlight the html code elements
  newArticleElement.querySelectorAll("code").forEach(code => hljs.highlightElement(code));
  document.querySelector("#articles").appendChild(newArticleElement);
}


function createArticleElements(articles) {

How to use highlight.js.highlightBlock:

23
24
25
26
27
28
29
30
31
// Add syntax highlighting
React.useEffect(() => {
    if (demoRef.current) {
        const container = demoRef.current;
        container.querySelectorAll('pre code').forEach((block) => {
            hljs.highlightBlock(block);
        });
    }
}, [demoRef]);

How to use highlight.js.default:

91
92
93
94
95
96
97
98
99
100
var model_1 = require("../../src/model");
var paths_1 = require("../paths");
var file_1 = require("./file");
var config_1 = require("../../src/config");
marked_1.marked.setOptions({
    highlight: function (code) { return highlight_js_1.default.highlightAuto(code).value; },
});
var createRadar = function () { return __awaiter(void 0, void 0, void 0, function () {
    var fileNames, revisions, filterdRevisions, allReleases, items, flaggedItems;
    return __generator(this, function (_a) {

How to use highlight.js.COMMENT:

14
15
16
17
18
19
20
21
22
23
24


hljs.registerLanguage('51asm', function(hljs) {
    return {
        case_insensitive: true,
        contains: [
            hljs.COMMENT(';', '\n'),
            {
                scope: 'built_in',
                match: /\b(a|b|c|ab|dptr|r[0-7]|p[0-7](\.[0-7])?|acc(.[0-7])?|scon|sbuf)\b/
            },

How to use highlight.js.getLanguage:

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

How to use highlight.js.highlightAuto:

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;
});
```

How to use highlight.js.highlight:

12
13
14
15
16
17
18
19
20
21
markedConfig: {
    renderer: (function() {
        var r = new marked.Renderer();
        r.code = function(code, language){
          return '<pre><code class="hljs ' + (language || "") + '">' + 
              hljs.highlight("javascript", code).value +
            '</code></pre>';
        };
        return r;
    })()