Skip to content

Align line numbers with firstLineNumber attribute. #12

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

Closed
Closed
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
10 changes: 8 additions & 2 deletions src/highlightjs-line-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@
if (typeof element !== 'object') return;

var parent = element.parentNode;
// if have 'firstLineNumber' attribute and it's a positive nubmer
// start line numbering from its value.
var startFrom = element.getAttribute('firstLineNumber');
// if startFrom is 'null' regex test will return 'false'
startFrom = (/^\d+$/.test(startFrom)) ? parseInt(startFrom) : 0;
var lines = getCountLines(parent.textContent);

if (lines > 1) {
var l = '';
for (var i = 0; i < lines; i++) {
l += (i + 1) + '\n';
// shifting line number as it was numbered starting from 'startFrom'
l += (startFrom + i + 1) + '\n';
}

var linesPanel = document.createElement('code');
Expand All @@ -64,4 +70,4 @@

return lines;
}
}(window));
}(window));