Skip to content

Commit d66b2b0

Browse files
authoredMay 30, 2020
Merge pull request #76 from wcoder/dev
2 parents 98ca4f2 + b39dba5 commit d66b2b0

7 files changed

+677
-2028
lines changed
 

Diff for: ‎README.md

+52-9
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,48 @@ Highlight.js line numbers plugin.
66

77
## Install
88

9-
#### Bower
9+
### Bower
10+
1011
```
1112
bower install highlightjs-line-numbers.js
1213
```
1314

14-
#### Npm
15+
### Npm
16+
1517
```
1618
npm install highlightjs-line-numbers.js
1719
```
1820

1921
#### Getting the library from CDN
22+
2023
```html
21-
<script src="//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.7.0/highlightjs-line-numbers.min.js"></script>
24+
<script src="//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.8.0/highlightjs-line-numbers.min.js"></script>
2225
```
26+
2327
```html
24-
<script src="//cdn.jsdelivr.net/npm/highlightjs-line-numbers.js@2.7.0/dist/highlightjs-line-numbers.min.js"></script>
28+
<script src="//cdn.jsdelivr.net/npm/highlightjs-line-numbers.js@2.8.0/dist/highlightjs-line-numbers.min.js"></script>
2529
```
2630

2731
## Usage
2832

2933
Download plugin and include file after highlight.js:
34+
3035
```html
3136
<script src="path/to/highlight.min.js"></script>
3237

3338
<script src="path/to/highlightjs-line-numbers.min.js"></script>
3439
```
3540

3641
Initialize plugin after highlight.js:
42+
3743
```js
3844
hljs.initHighlightingOnLoad();
3945

4046
hljs.initLineNumbersOnLoad();
4147
```
4248

4349
Here’s an equivalent way to calling `initLineNumbersOnLoad` using jQuery:
50+
4451
```js
4552
$(document).ready(function() {
4653
$('code.hljs').each(function(i, block) {
@@ -50,6 +57,7 @@ $(document).ready(function() {
5057
```
5158

5259
If your needs cool style, add styles by taste:
60+
5361
```css
5462
/* for block of numbers */
5563
.hljs-ln-numbers {
@@ -79,11 +87,12 @@ If your needs cool style, add styles by taste:
7987

8088
After version 2.1 plugin has optional parameter `options` - for custom setup.
8189

82-
name | type | default value | description
83-
-----------|---------|---------------|-----------------------
84-
singleLine | boolean | false | enable plugin for code block with one line
90+
version | name | type | default value | description
91+
--------|------------|---------|---------------|-----------------------
92+
v2.1 | singleLine | boolean | false | enable plugin for code block with one line
93+
v2.8 | startFrom | int | 1 | [Start numbering from a custom value](startFrom)
8594

86-
#### Examples of using
95+
### Examples of using
8796

8897
```js
8998
hljs.initLineNumbersOnLoad({
@@ -95,6 +104,40 @@ hljs.initLineNumbersOnLoad({
95104
hljs.lineNumbersBlock(myCodeBlock, myOptions);
96105
```
97106

107+
```js
108+
hljs.lineNumbersValue(myCodeBlock, myOptions);
109+
```
110+
111+
### startFrom
112+
113+
If you want numbering to start from some other value than `1`, you can specify a _numbering offset_, in one of the following ways:
114+
115+
- Specifying desired offset in `hljs.lineNumbersBlock()` call, as in:
116+
117+
```js
118+
hljs.lineNumbersBlock(myCodeBlock, {
119+
startFrom: 10
120+
});
121+
```
122+
123+
- Specifying the desired offset in `data-ln-start-from` attribute of `code` element, as in:
124+
125+
```html
126+
<pre>
127+
<code data-ln-start-from="10">
128+
...
129+
</code>
130+
</pre>
131+
```
132+
133+
In both cases numbering offset will be `10`, meaning that the numbering will start from `10`.
134+
135+
## Skipping some blocks
136+
137+
(Applies to `hljs.initLineNumbersOnLoad()` initialization only.)
138+
139+
If you want to skip some of your `code` blocks (to leave them unnumbered), you can mark them with `.nohljsln` class.
140+
98141
## CSS selectors
99142

100143
You may need to select some lines of code after rendering. For instance, you may want
@@ -110,4 +153,4 @@ CSS selector | description
110153
`.hljs-ln-numbers[data-line-number="i"]` | Select the ith line number, excluding the line of code
111154
`.hljs-ln-code[data-line-number="i"]` | Select the ith line of code, excluding the line number
112155
---
113-
&copy; 2018 Yauheni Pakala | MIT License
156+
&copy; 2020 Yauheni Pakala and [Community](https://github.com/wcoder/highlightjs-line-numbers.js/graphs/contributors) | MIT License

Diff for: ‎bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "highlightjs-line-numbers.js",
3-
"version": "2.7.0",
3+
"version": "2.8.0",
44
"homepage": "https://github.com/wcoder/highlightjs-line-numbers.js",
55
"authors": [
66
"Yauheni Pakala <evgeniy.pakalo@gmail.com>"

Diff for: ‎dist/highlightjs-line-numbers.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎package-lock.json

+519-1,817
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "highlightjs-line-numbers.js",
3-
"version": "2.7.0",
3+
"version": "2.8.0",
44
"description": "Highlight.js line numbers plugin.",
55
"main": "src/highlightjs-line-numbers.js",
66
"dependencies": {},
77
"devDependencies": {
8-
"gulp": "^4.0.0",
9-
"gulp-rename": "^1.4.0",
10-
"gulp-replace": "^0.6.1",
11-
"gulp-uglify": "^1.2.0"
8+
"gulp": "^4.0.2",
9+
"gulp-rename": "^2.0.0",
10+
"gulp-replace": "^1.0.0",
11+
"gulp-uglify": "^3.0.2"
1212
},
1313
"scripts": {
1414
"test": "echo \"Error: no test specified\" && exit 1"

Diff for: ‎samples/index.html

+23-181
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
<!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
88
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> -->
99

10-
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
11-
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
10+
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css">
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
1212
<script src="../src/highlightjs-line-numbers.js"></script>
1313
<!-- <script src="../dist/highlightjs-line-numbers.min.js"></script> -->
1414
<script>
1515
hljs.initHighlightingOnLoad();
16-
hljs.initLineNumbersOnLoad();
16+
hljs.initLineNumbersOnLoad({
17+
// singleLine:true,
18+
// startFrom: 5,
19+
});
1720
</script>
1821
<style>
1922

@@ -53,198 +56,37 @@
5356
<h1>Examples</h1>
5457

5558

56-
<pre><code class="nohighlight">var a = 1;
59+
<pre><code class="nohighlight">// nohighlight
5760

5861
func as(int a)
5962
{
6063
return a * 1000;
6164
}
6265
</code></pre>
6366

67+
<pre><code class="nohljsln">// nohljsln
6468

65-
<pre><code>{
66-
"averageBuyFee" : 1.3999999999999999,
67-
"timestamp" : 1527721200000,
69+
func as(int a)
70+
{
71+
return a * 1000;
72+
}
73+
</code></pre>
74+
75+
76+
<pre><code data-ln-start-from="10">{
77+
"averageBuyFee" : 1.1234,
78+
"timestamp" : 1234,
6879
"rates" : {
69-
"HUF" : 274.05874999999997,
70-
"HRK" : 6.3338999999999999,
71-
"CDF" : 1614.661462,
72-
"ILS" : 3.56968,
73-
"NGN" : 360.24833999999998,
74-
"GYD" : 208.950526,
75-
"BHD" : 0.37757800000000002,
76-
"SZL" : 12.539944,
77-
"INR" : 67.439999999999998,
78-
"SDG" : 18.044215000000001,
79-
"PEN" : 3.2682959999999999,
80-
"QAR" : 3.6410819999999999,
81-
"EUR" : 0.85727299999999995,
82-
"PGK" : 3.2629299999999999,
83-
"LRD" : 138.1113,
84-
"ISK" : 105.34,
85-
"SYP" : 514.96999000000005,
86-
"TRY" : 4.4670449999999997,
87-
"UAH" : 26.113116000000002,
88-
"SGD" : 1.339046,
89-
"MMK" : 1354.149087,
90-
"NIO" : 31.548477999999999,
91-
"BIF" : 1768.5704430000001,
92-
"AFN" : 71.386266000000006,
93-
"LKR" : 158.013589,
94-
"GTQ" : 7.4797250000000002,
95-
"CHF" : 0.98897100000000004,
96-
"XPT" : 0.00110042,
97-
"THB" : 32.031666999999999,
98-
"AMD" : 484.44491900000003,
99-
"AOA" : 237.339,
100-
"SEK" : 8.8073490000000003,
101-
"SAR" : 3.7513000000000001,
102-
"KWD" : 0.30244599999999999,
103-
"WST" : 2.5682330000000002,
104-
"IRR" : 42157.265155000001,
105-
"BMD" : 1,
106-
"BGN" : 1.67479,
107-
"PHP" : 52.542000000000002,
108-
"XAF" : 562.334157,
109-
"ZMW" : 10.268117999999999,
110-
"BDT" : 84.448802999999998,
111-
"NOK" : 8.1833340000000003,
112-
"BOB" : 6.9053979999999999,
113-
"TZS" : 2281,
114-
"VEF" : 79902.5,
115-
"VUV" : 108.462901,
116-
"BND" : 1.365947,
117-
"SCR" : 13.454981,
118-
"XAG" : 0.060565220000000003,
119-
"XCD" : 2.70255,
120-
"ANG" : 1.7938430000000001,
121-
"KYD" : 0.83285299999999995,
122-
"DJF" : 178,
123-
"CLF" : 0.022929999999999999,
124-
"LSL" : 12.539552,
125-
"MOP" : 8.0757530000000006,
126-
"ALL" : 108.396237,
127-
"UZS" : 8000.9282050000002,
128-
"PLN" : 3.6911580000000002,
129-
"UYU" : 31.165526,
130-
"LYD" : 1.3612089999999999,
131-
"JPY" : 108.82599999999999,
132-
"MNT" : 2404.4274110000001,
133-
"FJD" : 2.0859999999999999,
134-
"ZWL" : 322.35501099999999,
135-
"KPW" : 900,
136-
"PKR" : 115.668333,
137-
"MRO" : 355.5,
138-
"OMR" : 0.38504300000000002,
139-
"GBP" : 0.752745,
140-
"SHP" : 0.752745,
141-
"GEL" : 2.4720840000000002,
142-
"TND" : 2.556092,
143-
"DKK" : 6.3805290000000001,
144-
"KRW" : 1076.1783330000001,
145-
"NPR" : 107.89304199999999,
146-
"BSD" : 1,
147-
"CRC" : 565.55771700000003,
148-
"EGP" : 17.921500000000002,
149-
"AUD" : 1.320354,
150-
"MAD" : 9.4990600000000001,
151-
"SLL" : 7823.3006290000003,
152-
"MWK" : 719.82837300000006,
153-
"RSD" : 101.295891,
154-
"NZD" : 1.4312769999999999,
155-
"SRD" : 7.468,
156-
"CLP" : 627.70000000000005,
157-
"RUB" : 62.107999999999997,
158-
"HKD" : 7.8472340000000003,
159-
"NAD" : 12.539552,
160-
"GMD" : 47.159999999999997,
161-
"VND" : 22845.908908000001,
162-
"LAK" : 8379.8500000000004,
163-
"CUC" : 1,
164-
"RON" : 3.986386,
165-
"MUR" : 34.847499999999997,
166-
"XAU" : 0.00076787999999999997,
167-
"BRL" : 3.7237,
168-
"STD" : 21076.892414000002,
169-
"MXN" : 19.778500000000001,
170-
"AWG" : 1.7849980000000001,
171-
"MVR" : 15.509850999999999,
172-
"PAB" : 1,
173-
"TJS" : 8.9870079999999994,
174-
"GNF" : 9033.9833330000001,
175-
"MGA" : 3327.9333329999999,
176-
"XDR" : 0.70712399999999997,
177-
"ETB" : 27.550000000000001,
178-
"COP" : 2899.1596639999998,
179-
"ZAR" : 12.528663,
180-
"IDR" : 13999.130101000001,
181-
"SVC" : 8.7445249999999994,
182-
"CVE" : 95.25,
183-
"TTD" : 6.7357120000000004,
184-
"GIP" : 0.752745,
185-
"PYG" : 5731.4847490000002,
186-
"MZN" : 60.387998000000003,
187-
"FKP" : 0.752745,
188-
"KZT" : 329.554575,
189-
"UGX" : 3767.4893870000001,
190-
"USD" : 1,
191-
"RWF" : 866.26532099999997,
192-
"ARS" : 24.927,
193-
"GHS" : 4.6670319999999998,
194-
"DOP" : 49.524921999999997,
195-
"LBP" : 1510.147766,
196-
"BTN" : 67.441953999999996,
197-
"BZD" : 2.0086949999999999,
198-
"YER" : 250.319142,
199-
"MYR" : 3.9874999999999998,
200-
"JMD" : 127.013092,
201-
"TOP" : 2.2954629999999998,
202-
"SOS" : 575.82003899999995,
203-
"TMT" : 3.504988,
204-
"XPD" : 0.0010126899999999999,
205-
"MDL" : 16.926297000000002,
206-
"XOF" : 562.334157,
207-
"TWD" : 29.909185999999998,
208-
"BBD" : 2,
209-
"CAD" : 1.288872,
210-
"CNY" : 6.4198170000000001,
211-
"JOD" : 0.70950299999999999,
212-
"XPF" : 102.299868,
213-
"IQD" : 1190.1505790000001,
214-
"HNL" : 23.889554,
215-
"AED" : 3.673181,
216-
"KES" : 101.55004599999999,
217-
"ERN" : 14.996267,
218-
"SSP" : 130.26339999999999,
219-
"KMF" : 422.54243500000001,
220-
"DZD" : 116.678772,
221-
"MKD" : 52.774999999999999,
222-
"CUP" : 25.5,
223-
"BWP" : 9.9635840000000009,
224-
"AZN" : 1.7024999999999999,
225-
"SBD" : 7.9034870000000002,
226-
"KGS" : 68.359093000000001,
227-
"KHR" : 4077.6333330000002,
228-
"HTG" : 65.057357999999994,
80+
"HUF": 274.1234,
22981
"CZK" : 22.1663,
230-
"BAM" : 1.6809190000000001
82+
"BAM":1.1234,
23183
},
23284
"base" : "USD",
233-
"excludedCurrencies" : [
234-
"XDR",
235-
"BYN",
236-
"XPT",
237-
"XAU",
238-
"ZWL",
239-
"XPD",
240-
"JEP",
241-
"GGP",
242-
"CLF",
243-
"IMP",
244-
"XAG",
85+
"excludedCurrencies" : [ "XDR",
24586
"ZMK",
24687
"BTC"
247-
]
88+
],
89+
_invalid
24890
}</code></pre>
24991

25092
<pre><code>var test = "single line";

Diff for: ‎src/highlightjs-line-numbers.js

+76-14
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
var selectionText;
118118
// workaround an issue with Microsoft Edge as copied line breaks
119119
// are removed otherwise from the selection string
120-
if (window.navigator.userAgent.indexOf("Edge") !== -1) {
120+
if (window.navigator.userAgent.indexOf('Edge') !== -1) {
121121
selectionText = edgeGetSelectedCodeLines(selection);
122122
} else {
123123
// other browsers can directly use the selection string
@@ -159,14 +159,20 @@
159159

160160
for (var i in blocks) {
161161
if (blocks.hasOwnProperty(i)) {
162-
lineNumbersBlock(blocks[i], options);
162+
if (!isPluginDisabledForBlock(blocks[i])) {
163+
lineNumbersBlock(blocks[i], options);
164+
}
163165
}
164166
}
165167
} catch (e) {
166168
w.console.error('LineNumbers error: ', e);
167169
}
168170
}
169171

172+
function isPluginDisabledForBlock(element) {
173+
return element.classList.contains('nohljsln');
174+
}
175+
170176
function lineNumbersBlock (element, options) {
171177
if (typeof element !== 'object') return;
172178

@@ -185,29 +191,23 @@
185191
}
186192

187193
function lineNumbersInternal (element, options) {
188-
// define options or set default
189-
options = options || {
190-
singleLine: false
191-
};
192194

193-
// convert options
194-
var firstLineIndex = !!options.singleLine ? 0 : 1;
195+
var internalOptions = mapOptions(element, options);
195196

196197
duplicateMultilineNodes(element);
197198

198-
return addLineNumbersBlockFor(element.innerHTML, firstLineIndex);
199+
return addLineNumbersBlockFor(element.innerHTML, internalOptions);
199200
}
200201

201-
function addLineNumbersBlockFor (inputHtml, firstLineIndex) {
202-
202+
function addLineNumbersBlockFor (inputHtml, options) {
203203
var lines = getLines(inputHtml);
204204

205205
// if last line contains only carriage return remove it
206206
if (lines[lines.length-1].trim() === '') {
207207
lines.pop();
208208
}
209209

210-
if (lines.length > firstLineIndex) {
210+
if (lines.length > 1 || options.singleLine) {
211211
var html = '';
212212

213213
for (var i = 0, l = lines.length; i < l; i++) {
@@ -226,7 +226,7 @@
226226
NUMBER_LINE_NAME,
227227
DATA_ATTR_NAME,
228228
CODE_BLOCK_NAME,
229-
i + 1,
229+
i + options.startFrom,
230230
lines[i].length > 0 ? lines[i] : ' '
231231
]);
232232
}
@@ -237,6 +237,44 @@
237237
return inputHtml;
238238
}
239239

240+
/**
241+
* @param {HTMLElement} element Code block.
242+
* @param {Object} options External API options.
243+
* @returns {Object} Internal API options.
244+
*/
245+
function mapOptions (element, options) {
246+
options = options || {};
247+
return {
248+
singleLine: getSingleLineOption(options),
249+
startFrom: getStartFromOption(element, options)
250+
};
251+
}
252+
253+
function getSingleLineOption (options) {
254+
var defaultValue = false;
255+
if (!!options.singleLine) {
256+
return options.singleLine;
257+
}
258+
return defaultValue;
259+
}
260+
261+
function getStartFromOption (element, options) {
262+
var defaultValue = 1;
263+
var startFrom = defaultValue;
264+
265+
if (isFinite(options.startFrom)) {
266+
startFrom = options.startFrom;
267+
}
268+
269+
// can be overridden because local option is priority
270+
var value = getAttribute(element, 'data-ln-start-from');
271+
if (value !== null) {
272+
startFrom = toNumber(value, defaultValue);
273+
}
274+
275+
return startFrom;
276+
}
277+
240278
/**
241279
* Recursive method for fix multi-line elements implementation in highlight.js
242280
* Doing deep passage on child nodes.
@@ -286,6 +324,10 @@
286324
return (text.trim().match(BREAK_LINE_REGEXP) || []).length;
287325
}
288326

327+
///
328+
/// HELPERS
329+
///
330+
289331
function async (func) {
290332
w.setTimeout(func, 0);
291333
}
@@ -297,8 +339,28 @@
297339
*/
298340
function format (format, args) {
299341
return format.replace(/\{(\d+)\}/g, function(m, n){
300-
return args[n] ? args[n] : m;
342+
return args[n] !== undefined ? args[n] : m;
301343
});
302344
}
303345

346+
/**
347+
* @param {HTMLElement} element Code block.
348+
* @param {String} attrName Attribute name.
349+
* @returns {String} Attribute value or empty.
350+
*/
351+
function getAttribute (element, attrName) {
352+
return element.hasAttribute(attrName) ? element.getAttribute(attrName) : null;
353+
}
354+
355+
/**
356+
* @param {String} str Source string.
357+
* @param {Number} fallback Fallback value.
358+
* @returns Parsed number or fallback value.
359+
*/
360+
function toNumber (str, fallback) {
361+
if (!str) return fallback;
362+
var number = Number(str);
363+
return isFinite(number) ? number : fallback;
364+
}
365+
304366
}(window, document));

0 commit comments

Comments
 (0)
Please sign in to comment.