Skip to content

Commit 883ba5d

Browse files
committed
Added implementation of relative links #7
1 parent 9a3553a commit 883ba5d

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,26 @@ $(document).ready(function() {
5656
});
5757
```
5858

59+
## Additional features
60+
61+
#### Links
62+
```js
63+
hljs.initLineNumbersOnLoad({
64+
withLinks: true
65+
});
66+
```
67+
68+
```css
69+
.hljs-line-numbers a {
70+
text-decoration: none;
71+
color: #999;
72+
}
73+
.hljs-line-numbers a:target {
74+
color: #ff0000;
75+
text-decoration: underline;
76+
outline: none;
77+
}
78+
```
79+
5980
---
6081
© 2015 Yauheni Pakala | MIT License

dist/highlightjs-line-numbers.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/highlightjs-line-numbers.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@
88
w.hljs.lineNumbersBlock = lineNumbersBlock;
99
}
1010

11-
function initLineNumbersOnLoad () {
11+
var defaultOptions = {
12+
withLinks: false
13+
};
14+
15+
function initLineNumbersOnLoad (options) {
16+
options = options || defaultOptions;
17+
1218
w.addEventListener('load', function () {
1319
try {
1420
var blocks = document.querySelectorAll('code.hljs');
1521

1622
for (var i in blocks) {
1723
if (blocks.hasOwnProperty(i)) {
18-
lineNumbersBlock(blocks[i]);
24+
lineNumbersBlock(blocks[i], {
25+
blockName: 'c' + i,
26+
withLinks: options.withLinks
27+
});
1928
}
2029
}
2130
} catch (e) {
@@ -24,28 +33,37 @@
2433
});
2534
}
2635

27-
function lineNumbersBlock (element) {
36+
function lineNumbersBlock (element, options) {
2837
if (typeof element !== 'object') return;
38+
if (!!options) {
39+
options.withLinks = options.withLinks || false;
40+
options.blockName = options.blockName || false;
41+
} else {
42+
options = defaultOptions;
43+
options.blockName = '';
44+
}
2945

3046
var parent = element.parentNode;
3147
var lines = getCountLines(parent.textContent);
3248

3349
if (lines > 1) {
3450
var l = '';
3551
for (var i = 0; i < lines; i++) {
36-
l += (i + 1) + '\n';
52+
l += options.withLinks
53+
? getLineWithLink(i + 1, options.blockName)
54+
: (i + 1) + '\n';
3755
}
3856

3957
var linesPanel = document.createElement('code');
4058
linesPanel.className = 'hljs hljs-line-numbers';
4159
linesPanel.style.float = 'left';
42-
linesPanel.textContent = l;
60+
linesPanel.innerHTML = l;
4361

4462
parent.insertBefore(linesPanel, element);
4563
}
4664
}
4765

48-
function getCountLines(text) {
66+
function getCountLines (text) {
4967
if (text.length === 0) return 0;
5068

5169
var regExp = /\r\n|\r|\n/g;
@@ -58,4 +76,9 @@
5876

5977
return lines;
6078
}
79+
80+
function getLineWithLink (i, blockName) {
81+
var id = blockName + '_l' + i;
82+
return '<a href="#' + id + '" id="' + id + '">' + i + '</span>\n'
83+
}
6184
}(window));

0 commit comments

Comments
 (0)