Skip to content

Commit 86e84df

Browse files
Allow setting an initial line-number.
The initial value at which the line-numbers will start can be set by adding an inline style which sets the CSS variable `--line-numbers-init` to that desired initial value. Note: This behavior now requires a modern browser, which supports CSS variables.
1 parent a84325a commit 86e84df

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ print my_example_code(TRUE);
5151
</code></pre>
5252
```
5353

54+
### Starting line-numbers from other number than 1
55+
To make the line numbers start at a different number than 1, you can add an inline style to the &lt;code> element
56+
which sets the CSS variable `--line-numbers-init` to its initial value.
57+
58+
```html
59+
<pre><code class="line-numbers" style="--line-numbers-init: 42">
60+
/**
61+
* Example Code.
62+
*/
63+
function my_example_code($line_numbers = TRUE) {
64+
$message = 'This example code does';
65+
$message .= ($line_numbers ? '' : ' not');
66+
$message .= ' have line numbers starting at 42.';
67+
68+
return $message;
69+
}
70+
71+
print my_example_code(TRUE);
72+
</code></pre>
73+
```
74+
5475
### Highlighting line numbers.
5576

5677
To highlight particular line numbers in a code block, add the attribute 'data-highlight-lines' to the &lt;code> element.

line-numbers.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
code.line-numbers {
2-
counter-reset: line_numbers;
2+
counter-reset: line_numbers calc(var(--line-numbers-init, 1) - 1);
33
}
44

55
code span.line-number {

line-numbers.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ function addLineNumbers() {
7272
highlights = getLineNumberHighlights(line_numbers[l].getAttribute("data-highlight-lines"));
7373
}
7474

75+
// Determine the intial value of the line-numbers-counter.
76+
var init_offset = getInitialLineNumberCounterValue(line_numbers[l]);
77+
7578
// Get the content of the code block.
7679
var content = line_numbers[l].innerHTML;
7780
var classes = '';
@@ -85,7 +88,7 @@ function addLineNumbers() {
8588
// Add class 'line-number'.
8689
classes = 'line-number';
8790
// Should this line be highlighted?
88-
if (highlights.indexOf(n+1) > -1) {
91+
if (highlights.indexOf(n+1 + init_offset) > -1) {
8992
classes += ' highlight-line';
9093
}
9194
// Append line with line number span.
@@ -141,3 +144,20 @@ function getLineNumberHighlights(line_numbers) {
141144
// Return count of hightlight lines.
142145
return highlights;
143146
}
147+
148+
// Determines the initial value of the line-numbers CSS-counter.
149+
function getInitialLineNumberCounterValue(block) {
150+
var init_line_number = 0;
151+
152+
// Retrieve initial value from style.
153+
var string_from_css = getComputedStyle(block, null).getPropertyValue('--line-numbers-init');
154+
// Verify initial value.
155+
if (typeof string_from_css == 'string') {
156+
var initVal = parseInt(string_from_css);
157+
if (! isNaN(initVal)) {
158+
init_line_number = initVal - 1;
159+
}
160+
}
161+
162+
return init_line_number;
163+
}

0 commit comments

Comments
 (0)