Skip to content

Commit e89decd

Browse files
committed
Using table.
Fixed #4, #13, #16
1 parent 91f4eeb commit e89decd

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

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)