1 |
efrain |
1 |
# Highlight.js
|
|
|
2 |
|
|
|
3 |
Highlight.js highlights syntax in code examples on blogs, forums and,
|
|
|
4 |
in fact, on any web page. It's very easy to use because it works
|
|
|
5 |
automatically: finds blocks of code, detects a language, highlights it.
|
|
|
6 |
|
|
|
7 |
Autodetection can be fine tuned when it fails by itself (see "Heuristics").
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
## Basic usage
|
|
|
11 |
|
|
|
12 |
Link the library and a stylesheet from your page and hook highlighting to
|
|
|
13 |
the page load event:
|
|
|
14 |
|
|
|
15 |
```html
|
|
|
16 |
<link rel="stylesheet" href="styles/default.css">
|
|
|
17 |
<script src="highlight.pack.js"></script>
|
|
|
18 |
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
19 |
```
|
|
|
20 |
|
|
|
21 |
This will highlight all code on the page marked up as `<pre><code> .. </code></pre>`.
|
|
|
22 |
If you use different markup or need to apply highlighting dynamically, read
|
|
|
23 |
"Custom initialization" below.
|
|
|
24 |
|
|
|
25 |
- You can download your own customized version of "highlight.pack.js" or
|
|
|
26 |
use the hosted one as described on the download page:
|
|
|
27 |
<http://highlightjs.org/download/>
|
|
|
28 |
|
|
|
29 |
- Style themes are available in the download package or as hosted files.
|
|
|
30 |
To create a custom style for your site see the class reference in the file
|
|
|
31 |
[CSS classes reference][cr] from the downloaded package.
|
|
|
32 |
|
|
|
33 |
[cr]: http://highlightjs.readthedocs.org/en/latest/css-classes-reference.html
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
## node.js
|
|
|
37 |
|
|
|
38 |
Highlight.js can be used under node.js. The package with all supported languages is
|
|
|
39 |
installable from NPM:
|
|
|
40 |
|
|
|
41 |
npm install highlight.js
|
|
|
42 |
|
|
|
43 |
Alternatively, you can build it from the source with only languages you need:
|
|
|
44 |
|
|
|
45 |
python3 tools/build.py -tnode lang1 lang2 ..
|
|
|
46 |
|
|
|
47 |
Using the library:
|
|
|
48 |
|
|
|
49 |
```javascript
|
|
|
50 |
var hljs = require('highlight.js');
|
|
|
51 |
|
|
|
52 |
// If you know the language
|
|
|
53 |
hljs.highlight(lang, code).value;
|
|
|
54 |
|
|
|
55 |
// Automatic language detection
|
|
|
56 |
hljs.highlightAuto(code).value;
|
|
|
57 |
```
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
## AMD
|
|
|
61 |
|
|
|
62 |
Highlight.js can be used with an AMD loader. You will need to build it from
|
|
|
63 |
source in order to do so:
|
|
|
64 |
|
|
|
65 |
```bash
|
|
|
66 |
$ python3 tools/build.py -tamd lang1 lang2 ..
|
|
|
67 |
```
|
|
|
68 |
|
|
|
69 |
Which will generate a `build/highlight.pack.js` which will load as an AMD
|
|
|
70 |
module with support for the built languages and can be used like so:
|
|
|
71 |
|
|
|
72 |
```javascript
|
|
|
73 |
require(["highlight.js/build/highlight.pack"], function(hljs){
|
|
|
74 |
|
|
|
75 |
// If you know the language
|
|
|
76 |
hljs.highlight(lang, code).value;
|
|
|
77 |
|
|
|
78 |
// Automatic language detection
|
|
|
79 |
hljs.highlightAuto(code).value;
|
|
|
80 |
});
|
|
|
81 |
```
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
## Tab replacement
|
|
|
85 |
|
|
|
86 |
You can replace TAB ('\x09') characters used for indentation in your code
|
|
|
87 |
with some fixed number of spaces or with a `<span>` to give them special
|
|
|
88 |
styling:
|
|
|
89 |
|
|
|
90 |
```html
|
|
|
91 |
<script type="text/javascript">
|
|
|
92 |
hljs.configure({tabReplace: ' '}); // 4 spaces
|
|
|
93 |
// ... or
|
|
|
94 |
hljs.configure({tabReplace: '<span class="indent">\t</span>'});
|
|
|
95 |
|
|
|
96 |
hljs.initHighlightingOnLoad();
|
|
|
97 |
</script>
|
|
|
98 |
```
|
|
|
99 |
|
|
|
100 |
## Custom initialization
|
|
|
101 |
|
|
|
102 |
If you use different markup for code blocks you can initialize them manually
|
|
|
103 |
with `highlightBlock(code)` function. It takes a DOM element containing the
|
|
|
104 |
code to highlight and optionally a string with which to replace TAB
|
|
|
105 |
characters.
|
|
|
106 |
|
|
|
107 |
Initialization using, for example, jQuery might look like this:
|
|
|
108 |
|
|
|
109 |
```javascript
|
|
|
110 |
$(document).ready(function() {
|
|
|
111 |
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
|
|
|
112 |
});
|
|
|
113 |
```
|
|
|
114 |
|
|
|
115 |
You can use `highlightBlock` to highlight blocks dynamically inserted into
|
|
|
116 |
the page. Just make sure you don't do it twice for already highlighted
|
|
|
117 |
blocks.
|
|
|
118 |
|
|
|
119 |
If your code container relies on `<br>` tags instead of line breaks (i.e. if
|
|
|
120 |
it's not `<pre>`) set the `useBR` option to `true`:
|
|
|
121 |
|
|
|
122 |
```javascript
|
|
|
123 |
hljs.configure({useBR: true});
|
|
|
124 |
$('div.code').each(function(i, e) {hljs.highlightBlock(e)});
|
|
|
125 |
```
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
## Heuristics
|
|
|
129 |
|
|
|
130 |
Autodetection of a code's language is done using a simple heuristic:
|
|
|
131 |
the program tries to highlight a fragment with all available languages and
|
|
|
132 |
counts all syntactic structures that it finds along the way. The language
|
|
|
133 |
with greatest count wins.
|
|
|
134 |
|
|
|
135 |
This means that in short fragments the probability of an error is high
|
|
|
136 |
(and it really happens sometimes). In this cases you can set the fragment's
|
|
|
137 |
language explicitly by assigning a class to the `<code>` element:
|
|
|
138 |
|
|
|
139 |
```html
|
|
|
140 |
<pre><code class="html">...</code></pre>
|
|
|
141 |
```
|
|
|
142 |
|
|
|
143 |
You can use class names recommended in HTML5: "language-html",
|
|
|
144 |
"language-php". Classes also can be assigned to the `<pre>` element.
|
|
|
145 |
|
|
|
146 |
To disable highlighting of a fragment altogether use "no-highlight" class:
|
|
|
147 |
|
|
|
148 |
```html
|
|
|
149 |
<pre><code class="no-highlight">...</code></pre>
|
|
|
150 |
```
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
## Export
|
|
|
154 |
|
|
|
155 |
File export.html contains a little program that allows you to paste in a code
|
|
|
156 |
snippet and then copy and paste the resulting HTML code generated by the
|
|
|
157 |
highlighter. This is useful in situations when you can't use the script itself
|
|
|
158 |
on a site.
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
## Meta
|
|
|
162 |
|
|
|
163 |
- Version: 8.0
|
|
|
164 |
- URL: http://highlightjs.org/
|
|
|
165 |
|
|
|
166 |
For the license terms see LICENSE files.
|
|
|
167 |
For authors and contributors see AUTHORS.en.txt file.
|