diff --git a/README.md b/README.md index c9406af..a6e875c 100644 --- a/README.md +++ b/README.md @@ -56,5 +56,26 @@ $(document).ready(function() { }); ``` +## Additional features + +#### Links +```js +hljs.initLineNumbersOnLoad({ + withLinks: true +}); +``` + +```css +.hljs-line-numbers a { + text-decoration: none; + color: #999; +} +.hljs-line-numbers a:target { + color: #ff0000; + text-decoration: underline; + outline: none; +} +``` + --- © 2015 Yauheni Pakala | MIT License diff --git a/dist/highlightjs-line-numbers.min.js b/dist/highlightjs-line-numbers.min.js index 6d0a3cb..e438d41 100644 --- a/dist/highlightjs-line-numbers.min.js +++ b/dist/highlightjs-line-numbers.min.js @@ -1 +1 @@ -!function(e){"use strict";function t(){"complete"===document.readyState?n():e.addEventListener("DOMContentLoaded",n)}function n(){try{var e=document.querySelectorAll("code.hljs");for(var t in e)e.hasOwnProperty(t)&&r(e[t])}catch(n){console.error("LineNumbers error: ",n)}}function r(e){if("object"==typeof e){var t=e.parentNode,n=o(t.textContent);if(n>1){for(var r="",c=0;n>c;c++)r+=c+1+"\n";var l=document.createElement("code");l.className="hljs hljs-line-numbers",l.style["float"]="left",l.textContent=r,t.insertBefore(l,e)}}}function o(e){if(0===e.length)return 0;var t=/\r\n|\r|\n/g,n=e.match(t);return n=n?n.length:0,e[e.length-1].match(t)||(n+=1),n}"undefined"==typeof e.hljs?console.error("highlight.js not detected!"):(e.hljs.initLineNumbersOnLoad=t,e.hljs.lineNumbersBlock=r)}(window); \ No newline at end of file +!function(e){"use strict";function n(n){n=n||c,"complete"===document.readyState?t(n):e.addEventListener("DOMContentLoaded",function(){t(n)})}function t(e){try{var n=document.querySelectorAll("code.hljs");for(var t in n)n.hasOwnProperty(t)&&r(n[t],{blockName:"c"+t,withLinks:e.withLinks})}catch(o){console.error("LineNumbers error: ",o)}}function r(e,n){if("object"==typeof e){n?(n.withLinks=n.withLinks||!1,n.blockName=n.blockName||!1):(n=c,n.blockName="");var t=e.parentNode,r=o(t.textContent);if(r>1){for(var a="",l=0;l'+e+"\n"}"undefined"==typeof e.hljs?console.error("highlight.js not detected!"):(e.hljs.initLineNumbersOnLoad=n,e.hljs.lineNumbersBlock=r);var c={withLinks:!1}}(window); \ No newline at end of file diff --git a/src/highlightjs-line-numbers.js b/src/highlightjs-line-numbers.js index f68f8f2..63131d2 100644 --- a/src/highlightjs-line-numbers.js +++ b/src/highlightjs-line-numbers.js @@ -8,21 +8,29 @@ w.hljs.lineNumbersBlock = lineNumbersBlock; } - function initLineNumbersOnLoad () { + var defaultOptions = { + withLinks: false + }; + + function initLineNumbersOnLoad (options) { + options = options || defaultOptions; if (document.readyState === 'complete') { - documentReady(); + documentReady(options); } else { - w.addEventListener('DOMContentLoaded', documentReady); + w.addEventListener('DOMContentLoaded', function() { documentReady(options); }); } } - function documentReady () { + function documentReady (options) { try { var blocks = document.querySelectorAll('code.hljs'); for (var i in blocks) { if (blocks.hasOwnProperty(i)) { - lineNumbersBlock(blocks[i]); + lineNumbersBlock(blocks[i], { + blockName: 'c' + i, + withLinks: options.withLinks + }); } } } catch (e) { @@ -30,8 +38,15 @@ } } - function lineNumbersBlock (element) { + function lineNumbersBlock (element, options) { if (typeof element !== 'object') return; + if (!!options) { + options.withLinks = options.withLinks || false; + options.blockName = options.blockName || false; + } else { + options = defaultOptions; + options.blockName = ''; + } var parent = element.parentNode; var lines = getCountLines(parent.textContent); @@ -39,19 +54,21 @@ if (lines > 1) { var l = ''; for (var i = 0; i < lines; i++) { - l += (i + 1) + '\n'; + l += options.withLinks + ? getLineWithLink(i + 1, options.blockName) + : (i + 1) + '\n'; } var linesPanel = document.createElement('code'); linesPanel.className = 'hljs hljs-line-numbers'; linesPanel.style.float = 'left'; - linesPanel.textContent = l; + linesPanel.innerHTML = l; parent.insertBefore(linesPanel, element); } } - function getCountLines(text) { + function getCountLines (text) { if (text.length === 0) return 0; var regExp = /\r\n|\r|\n/g; @@ -64,4 +81,9 @@ return lines; } -}(window)); \ No newline at end of file + + function getLineWithLink (i, blockName) { + var id = blockName + '_l' + i; + return '' + i + '\n' + } +}(window));