Skip to content

Commit 6f88ece

Browse files
wcoderWilfred
authored andcommitted
Added implementation of relative links wcoder#7
1 parent 2acd76f commit 6f88ece

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
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

+32-10
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,67 @@
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;
1217
if (document.readyState === 'complete') {
13-
documentReady();
18+
documentReady(options);
1419
} else {
15-
w.addEventListener('DOMContentLoaded', documentReady);
20+
w.addEventListener('DOMContentLoaded', function() { documentReady(options); });
1621
}
1722
}
1823

19-
function documentReady () {
24+
function documentReady (options) {
2025
try {
2126
var blocks = document.querySelectorAll('code.hljs');
2227

2328
for (var i in blocks) {
2429
if (blocks.hasOwnProperty(i)) {
25-
lineNumbersBlock(blocks[i]);
30+
lineNumbersBlock(blocks[i], {
31+
blockName: 'c' + i,
32+
withLinks: options.withLinks
33+
});
2634
}
2735
}
2836
} catch (e) {
2937
console.error('LineNumbers error: ', e);
3038
}
3139
}
3240

33-
function lineNumbersBlock (element) {
41+
function lineNumbersBlock (element, options) {
3442
if (typeof element !== 'object') return;
43+
if (!!options) {
44+
options.withLinks = options.withLinks || false;
45+
options.blockName = options.blockName || false;
46+
} else {
47+
options = defaultOptions;
48+
options.blockName = '';
49+
}
3550

3651
var parent = element.parentNode;
3752
var lines = getCountLines(parent.textContent);
3853

3954
if (lines > 1) {
4055
var l = '';
4156
for (var i = 0; i < lines; i++) {
42-
l += (i + 1) + '\n';
57+
l += options.withLinks
58+
? getLineWithLink(i + 1, options.blockName)
59+
: (i + 1) + '\n';
4360
}
4461

4562
var linesPanel = document.createElement('code');
4663
linesPanel.className = 'hljs hljs-line-numbers';
4764
linesPanel.style.float = 'left';
48-
linesPanel.textContent = l;
65+
linesPanel.innerHTML = l;
4966

5067
parent.insertBefore(linesPanel, element);
5168
}
5269
}
5370

54-
function getCountLines(text) {
71+
function getCountLines (text) {
5572
if (text.length === 0) return 0;
5673

5774
var regExp = /\r\n|\r|\n/g;
@@ -64,4 +81,9 @@
6481

6582
return lines;
6683
}
67-
}(window));
84+
85+
function getLineWithLink (i, blockName) {
86+
var id = blockName + '_l' + i;
87+
return '<a href="#' + id + '" name="' + id + '">' + i + '</a>\n'
88+
}
89+
}(window));

0 commit comments

Comments
 (0)