diff --git a/README.md b/README.md index 3637514..5602203 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 afaaf7b..9626a5d 100644 --- a/dist/highlightjs-line-numbers.min.js +++ b/dist/highlightjs-line-numbers.min.js @@ -1 +1 @@ -!function(e){"use strict";function t(){e.addEventListener("load",function(){try{var e=document.querySelectorAll("code.hljs");for(var t in e)e.hasOwnProperty(t)&&n(e[t])}catch(r){console.error("LineNumbers error: ",r)}})}function n(e){if("object"==typeof e){var t=e.parentNode,n=r(t.textContent);if(n>1){for(var o="",l=0;n>l;l++)o+=l+1+"\n";var c=document.createElement("code");c.className="hljs hljs-line-numbers",c.style["float"]="left",c.textContent=o,t.insertBefore(c,e)}}}function r(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=n)}(window); \ No newline at end of file +!function(e){"use strict";function n(n){n=n||i,e.addEventListener("load",function(){try{var e=document.querySelectorAll("code.hljs");for(var r in e)e.hasOwnProperty(r)&&t(e[r],{blockName:"c"+r,withLinks:n.withLinks})}catch(o){console.error("LineNumbers error: ",o)}})}function t(e,n){if("object"==typeof e){n?(n.withLinks=n.withLinks||!1,n.blockName=n.blockName||!1):(n=i,n.blockName="");var t=e.parentNode,l=r(t.textContent);if(l>1){for(var c="",s=0;l>s;s++)c+=n.withLinks?o(s+1,n.blockName):s+1+"\n";var a=document.createElement("code");a.className="hljs hljs-line-numbers",a.style["float"]="left",a.innerHTML=c,t.insertBefore(a,e)}}}function r(e){if(0===e.length)return 0;var n=/\r\n|\r|\n/g,t=e.match(n);return t=t?t.length:0,e[e.length-1].match(n)||(t+=1),t}function o(e,n){var t=n+"_l"+e;return''+e+"\n"}"undefined"==typeof e.hljs?console.error("highlight.js not detected!"):(e.hljs.initLineNumbersOnLoad=n,e.hljs.lineNumbersBlock=t);var i={withLinks:!1}}(window); \ No newline at end of file diff --git a/src/highlightjs-line-numbers.js b/src/highlightjs-line-numbers.js index 2c7fc2f..ba264bb 100644 --- a/src/highlightjs-line-numbers.js +++ b/src/highlightjs-line-numbers.js @@ -8,14 +8,23 @@ w.hljs.lineNumbersBlock = lineNumbersBlock; } - function initLineNumbersOnLoad () { + var defaultOptions = { + withLinks: false + }; + + function initLineNumbersOnLoad (options) { + options = options || defaultOptions; + w.addEventListener('load', function () { 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) { @@ -24,8 +33,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); @@ -33,19 +49,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; @@ -58,4 +76,9 @@ return lines; } + + function getLineWithLink (i, blockName) { + var id = blockName + '_l' + i; + return '' + i + '\n' + } }(window)); \ No newline at end of file