Skip to content

Commit 9e818e9

Browse files
committed
Merge branch 'v2'
2 parents 91f4eeb + 238cc97 commit 9e818e9

File tree

6 files changed

+77
-45
lines changed

6 files changed

+77
-45
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Yauheni Pakala
3+
Copyright (c) 2017 Yauheni Pakala
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 28 additions & 17 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-v1.1.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.0.0-brightgreen.svg?style=flat)](https://github.com/wcoder/highlightjs-line-numbers.js/archive/master.zip)
22

33
Highlight.js line numbers plugin.
44

@@ -25,21 +25,6 @@ Download plugin and include file after highlight.js:
2525
<script src="path/to/highlightjs-line-numbers.min.js"></script>
2626
```
2727

28-
Adding styles:
29-
```css
30-
.hljs-line-numbers {
31-
text-align: right;
32-
border-right: 1px solid #ccc;
33-
color: #999;
34-
-webkit-touch-callout: none;
35-
-webkit-user-select: none;
36-
-khtml-user-select: none;
37-
-moz-user-select: none;
38-
-ms-user-select: none;
39-
user-select: none;
40-
}
41-
```
42-
4328
Initialize plugin after highlight.js:
4429
```js
4530
hljs.initHighlightingOnLoad();
@@ -56,5 +41,31 @@ $(document).ready(function() {
5641
});
5742
```
5843

44+
If your needs cool style, add styles by taste:
45+
```css
46+
/* for block of numbers */
47+
td.hljs-ln-numbers {
48+
-webkit-touch-callout: none;
49+
-webkit-user-select: none;
50+
-khtml-user-select: none;
51+
-moz-user-select: none;
52+
-ms-user-select: none;
53+
user-select: none;
54+
55+
text-align: center;
56+
color: #ccc;
57+
border-right: 1px solid #CCC;
58+
vertical-align: top;
59+
padding-right: 5px;
60+
61+
/* your custom style here */
62+
}
63+
64+
/* for block of code */
65+
td.hljs-ln-code {
66+
padding-left: 10px;
67+
}
68+
```
69+
5970
---
60-
&copy; 2015 Yauheni Pakala | MIT License
71+
&copy; 2017 Yauheni Pakala | MIT License

bower.json

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

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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "highlightjs-line-numbers.js",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "Highlight.js line numbers plugin.",
55
"main": "src/highlightjs-line-numbers.js",
66
"dependencies": {},

src/highlightjs-line-numbers.js

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
(function (w) {
22
'use strict';
33

4+
var TABLE_NAME = 'hljs-ln',
5+
LINE_NAME = 'hljs-ln-line',
6+
CODE_BLOCK_NAME = 'hljs-ln-code',
7+
NUMBERS_BLOCK_NAME = 'hljs-ln-numbers',
8+
NUMBER_LINE_NAME = 'hljs-ln-n',
9+
DATA_ATTR_NAME = 'data-line-number';
10+
11+
// https://wcoder.github.io/notes/string-format-for-string-formating-in-javascript
12+
String.prototype.format = String.prototype.f = function () {
13+
var args = arguments;
14+
return this.replace(/\{(\d+)\}/g, function(m, n){
15+
return args[n] ? args[n] : m;
16+
});
17+
};
18+
419
if (typeof w.hljs === 'undefined') {
520
console.error('highlight.js not detected!');
621
} else {
722
w.hljs.initLineNumbersOnLoad = initLineNumbersOnLoad;
823
w.hljs.lineNumbersBlock = lineNumbersBlock;
24+
25+
addStyles();
26+
}
27+
28+
function addStyles () {
29+
var css = document.createElement('style');
30+
css.type = 'text/css';
31+
css.innerHTML = ('.{0}{border-collapse:collapse}' +
32+
'.{0} td{padding:0}' +
33+
'.{1}:before{content:attr({2})}').format(TABLE_NAME, NUMBER_LINE_NAME, DATA_ATTR_NAME);
34+
document.getElementsByTagName('head')[0].appendChild(css);
935
}
1036

1137
function initLineNumbersOnLoad () {
@@ -33,35 +59,30 @@
3359
function lineNumbersBlock (element) {
3460
if (typeof element !== 'object') return;
3561

36-
var parent = element.parentNode;
37-
var lines = getCountLines(parent.textContent);
62+
var lines = getLines(element.innerHTML);
3863

39-
if (lines > 1) {
40-
var l = '';
41-
for (var i = 0; i < lines; i++) {
42-
l += (i + 1) + '\n';
43-
}
64+
if (lines.length > 1) {
65+
var html = '';
4466

45-
var linesPanel = document.createElement('code');
46-
linesPanel.className = 'hljs hljs-line-numbers';
47-
linesPanel.style.float = 'left';
48-
linesPanel.textContent = l;
67+
for (var i = 0; i < lines.length; i++) {
68+
html += ('<tr><td class="{0}"><div class="{1} {2}" {3}="{5}"></div></td>' +
69+
'<td class="{4}"><div class="{1}">{6}</div></td></tr>').format(
70+
NUMBERS_BLOCK_NAME,
71+
LINE_NAME,
72+
NUMBER_LINE_NAME,
73+
DATA_ATTR_NAME,
74+
CODE_BLOCK_NAME,
75+
i + 1,
76+
lines[i].length > 0 ? lines[i] : ' ');
77+
}
4978

50-
parent.insertBefore(linesPanel, element);
79+
element.innerHTML = '<table class="{0}">{1}</table>'.format(TABLE_NAME, html);
5180
}
5281
}
5382

54-
function getCountLines(text) {
55-
if (text.length === 0) return 0;
56-
57-
var regExp = /\r\n|\r|\n/g;
58-
var lines = text.match(regExp);
59-
lines = lines ? lines.length : 0;
60-
61-
if (!text[text.length - 1].match(regExp)) {
62-
lines += 1;
63-
}
64-
65-
return lines;
83+
function getLines(text) {
84+
if (text.length === 0) return [];
85+
return text.split(/\r\n|\r|\n/g);
6686
}
87+
6788
}(window));

0 commit comments

Comments
 (0)