Skip to content

Commit 675285e

Browse files
authored
Merge pull request #33 from wcoder/dev
New version 2.3
2 parents 4ab966b + a007797 commit 675285e

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

README.md

Lines changed: 24 additions & 24 deletions
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.2.0-brightgreen.svg?style=flat)](https://github.com/wcoder/highlightjs-line-numbers.js/archive/master.zip)
1+
# highlightjs-line-numbers.js [![npm](https://img.shields.io/npm/v/highlightjs-line-numbers.js.svg)](https://www.npmjs.com/package/highlightjs-line-numbers.js) ![npm](https://img.shields.io/npm/dw/highlightjs-line-numbers.js.svg)
22

33
Highlight.js line numbers plugin.
44

@@ -18,7 +18,7 @@ npm install highlightjs-line-numbers.js
1818

1919
#### Getting the library from CDN
2020
```html
21-
<script src="//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.2.0/highlightjs-line-numbers.min.js"></script>
21+
<script src="//cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.3.0/highlightjs-line-numbers.min.js"></script>
2222
```
2323

2424
## Usage
@@ -40,45 +40,45 @@ hljs.initLineNumbersOnLoad();
4040
Here’s an equivalent way to calling `initLineNumbersOnLoad` using jQuery:
4141
```js
4242
$(document).ready(function() {
43-
$('code.hljs').each(function(i, block) {
44-
hljs.lineNumbersBlock(block);
45-
});
43+
$('code.hljs').each(function(i, block) {
44+
hljs.lineNumbersBlock(block);
45+
});
4646
});
4747
```
4848

4949
If your needs cool style, add styles by taste:
5050
```css
5151
/* for block of numbers */
5252
td.hljs-ln-numbers {
53-
-webkit-touch-callout: none;
54-
-webkit-user-select: none;
55-
-khtml-user-select: none;
56-
-moz-user-select: none;
57-
-ms-user-select: none;
58-
user-select: none;
59-
60-
text-align: center;
61-
color: #ccc;
62-
border-right: 1px solid #CCC;
63-
vertical-align: top;
64-
padding-right: 5px;
65-
66-
/* your custom style here */
53+
-webkit-touch-callout: none;
54+
-webkit-user-select: none;
55+
-khtml-user-select: none;
56+
-moz-user-select: none;
57+
-ms-user-select: none;
58+
user-select: none;
59+
60+
text-align: center;
61+
color: #ccc;
62+
border-right: 1px solid #CCC;
63+
vertical-align: top;
64+
padding-right: 5px;
65+
66+
/* your custom style here */
6767
}
6868

6969
/* for block of code */
7070
td.hljs-ln-code {
71-
padding-left: 10px;
71+
padding-left: 10px;
7272
}
7373
```
7474

7575
## Options
7676

7777
After version 2.1 plugin has optional parameter `options` - for custom setup.
7878

79-
name | type | default value | description
80-
-----|------|---------------|------------
81-
singleLine | boolean | false | enable plugin for code block with one line
79+
name | type | default value | description
80+
-----------|---------|---------------|-----------------------
81+
singleLine | boolean | false | enable plugin for code block with one line
8282

8383
#### Examples of using
8484

@@ -93,4 +93,4 @@ hljs.lineNumbersBlock(myCodeBlock, myOptions);
9393
```
9494

9595
---
96-
&copy; 2017 Yauheni Pakala | MIT License
96+
&copy; 2018 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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282

8383
var lines = getLines(inputHtml);
8484

85+
// if last line contains only carriage return remove it
86+
if (lines[lines.length-1].trim() === '') {
87+
lines.pop();
88+
}
89+
8590
if (lines.length > firstLineIndex) {
8691
var html = '';
8792

@@ -119,14 +124,14 @@
119124
*/
120125
function duplicateMultilineNodes (element) {
121126
var nodes = element.childNodes;
122-
for (var node in nodes){
127+
for (var node in nodes) {
123128
if (nodes.hasOwnProperty(node)) {
124129
var child = nodes[node];
125130
if (getLinesCount(child.textContent) > 0) {
126131
if (child.childNodes.length > 0) {
127132
duplicateMultilineNodes(child);
128133
} else {
129-
duplicateMultilineNode(child);
134+
duplicateMultilineNode(child.parentNode);
130135
}
131136
}
132137
}
@@ -138,16 +143,17 @@
138143
* @param {HTMLElement} element
139144
*/
140145
function duplicateMultilineNode (element) {
141-
var className = element.parentNode.className;
146+
var className = element.className;
142147

143148
if ( ! /hljs-/.test(className)) return;
144149

145-
var lines = getLines(element.textContent);
150+
var lines = getLines(element.innerHTML);
146151

147152
for (var i = 0, result = ''; i < lines.length; i++) {
148153
result += format('<span class="{0}">{1}</span>\n', [ className, lines[i] ]);
149154
}
150-
element.parentNode.innerHTML = result.trim();
155+
156+
element.innerHTML = result.trim();
151157
}
152158

153159
function getLines (text) {

0 commit comments

Comments
 (0)