Skip to content

New version 2.3 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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)
# 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)

Highlight.js line numbers plugin.

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

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

## Usage
Expand All @@ -40,45 +40,45 @@ hljs.initLineNumbersOnLoad();
Here’s an equivalent way to calling `initLineNumbersOnLoad` using jQuery:
```js
$(document).ready(function() {
$('code.hljs').each(function(i, block) {
hljs.lineNumbersBlock(block);
});
$('code.hljs').each(function(i, block) {
hljs.lineNumbersBlock(block);
});
});
```

If your needs cool style, add styles by taste:
```css
/* for block of numbers */
td.hljs-ln-numbers {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

text-align: center;
color: #ccc;
border-right: 1px solid #CCC;
vertical-align: top;
padding-right: 5px;

/* your custom style here */
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

text-align: center;
color: #ccc;
border-right: 1px solid #CCC;
vertical-align: top;
padding-right: 5px;

/* your custom style here */
}

/* for block of code */
td.hljs-ln-code {
padding-left: 10px;
padding-left: 10px;
}
```

## Options

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

name | type | default value | description
-----|------|---------------|------------
singleLine | boolean | false | enable plugin for code block with one line
name | type | default value | description
-----------|---------|---------------|-----------------------
singleLine | boolean | false | enable plugin for code block with one line

#### Examples of using

Expand All @@ -93,4 +93,4 @@ hljs.lineNumbersBlock(myCodeBlock, myOptions);
```

---
&copy; 2017 Yauheni Pakala | MIT License
&copy; 2018 Yauheni Pakala | MIT License
2 changes: 1 addition & 1 deletion dist/highlightjs-line-numbers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions src/highlightjs-line-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@

var lines = getLines(inputHtml);

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

if (lines.length > firstLineIndex) {
var html = '';

Expand Down Expand Up @@ -119,14 +124,14 @@
*/
function duplicateMultilineNodes (element) {
var nodes = element.childNodes;
for (var node in nodes){
for (var node in nodes) {
if (nodes.hasOwnProperty(node)) {
var child = nodes[node];
if (getLinesCount(child.textContent) > 0) {
if (child.childNodes.length > 0) {
duplicateMultilineNodes(child);
} else {
duplicateMultilineNode(child);
duplicateMultilineNode(child.parentNode);
}
}
}
Expand All @@ -138,16 +143,17 @@
* @param {HTMLElement} element
*/
function duplicateMultilineNode (element) {
var className = element.parentNode.className;
var className = element.className;

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

var lines = getLines(element.textContent);
var lines = getLines(element.innerHTML);

for (var i = 0, result = ''; i < lines.length; i++) {
result += format('<span class="{0}">{1}</span>\n', [ className, lines[i] ]);
}
element.parentNode.innerHTML = result.trim();

element.innerHTML = result.trim();
}

function getLines (text) {
Expand Down