Skip to content

Commit 0b5e235

Browse files
committed
add options #18
1 parent 9e818e9 commit 0b5e235

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# highlightjs-line-numbers.js [![version](http://img.shields.io/badge/release-v2.0.0-brightgreen.svg?style=flat)](https://github.com/wcoder/highlightjs-line-numbers.js/archive/master.zip)
1+
# highlightjs-line-numbers.js [![version](http://img.shields.io/badge/release-v2.1.0-brightgreen.svg?style=flat)](https://github.com/wcoder/highlightjs-line-numbers.js/archive/master.zip)
22

33
Highlight.js line numbers plugin.
44

@@ -67,5 +67,25 @@ td.hljs-ln-code {
6767
}
6868
```
6969

70+
## Options
71+
72+
After version 2.1 plugin has optional parameter `options` - for custom setup.
73+
74+
name | type | default value | description
75+
-----|------|---------------|------------
76+
singleLine | boolean | false | enable plugin for code block with one line
77+
78+
#### Examples of using
79+
80+
```js
81+
hljs.initLineNumbersOnLoad({
82+
singleLine: true
83+
});
84+
```
85+
86+
```js
87+
hljs.lineNumbersBlock(myCodeBlock, myOptions);
88+
```
89+
7090
---
7191
© 2017 Yauheni Pakala | MIT License

dist/highlightjs-line-numbers.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/highlightjs-line-numbers.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
CODE_BLOCK_NAME = 'hljs-ln-code',
77
NUMBERS_BLOCK_NAME = 'hljs-ln-numbers',
88
NUMBER_LINE_NAME = 'hljs-ln-n',
9-
DATA_ATTR_NAME = 'data-line-number';
9+
DATA_ATTR_NAME = 'data-line-number';
1010

1111
// https://wcoder.github.io/notes/string-format-for-string-formating-in-javascript
1212
String.prototype.format = String.prototype.f = function () {
@@ -34,34 +34,44 @@
3434
document.getElementsByTagName('head')[0].appendChild(css);
3535
}
3636

37-
function initLineNumbersOnLoad () {
37+
function initLineNumbersOnLoad (options) {
3838
if (document.readyState === 'complete') {
39-
documentReady();
39+
documentReady(options);
4040
} else {
41-
w.addEventListener('DOMContentLoaded', documentReady);
41+
w.addEventListener('DOMContentLoaded', function () {
42+
documentReady(options);
43+
});
4244
}
4345
}
4446

45-
function documentReady () {
47+
function documentReady (options) {
4648
try {
4749
var blocks = document.querySelectorAll('code.hljs');
4850

4951
for (var i in blocks) {
5052
if (blocks.hasOwnProperty(i)) {
51-
lineNumbersBlock(blocks[i]);
53+
lineNumbersBlock(blocks[i], options);
5254
}
5355
}
5456
} catch (e) {
5557
console.error('LineNumbers error: ', e);
5658
}
5759
}
5860

59-
function lineNumbersBlock (element) {
61+
function lineNumbersBlock (element, options) {
6062
if (typeof element !== 'object') return;
6163

64+
// define options or set default
65+
options = options || {
66+
singleLine: false
67+
};
68+
69+
// convert options
70+
var firstLineIndex = !!options.singleLine ? 0 : 1;
71+
6272
var lines = getLines(element.innerHTML);
6373

64-
if (lines.length > 1) {
74+
if (lines.length > firstLineIndex) {
6575
var html = '';
6676

6777
for (var i = 0; i < lines.length; i++) {

0 commit comments

Comments
 (0)