diff --git a/README.md b/README.md index 375fce4..3c3d431 100644 --- a/README.md +++ b/README.md @@ -6,27 +6,32 @@ Highlight.js line numbers plugin. ## Install -#### Bower +### Bower + ``` bower install highlightjs-line-numbers.js ``` -#### Npm +### Npm + ``` npm install highlightjs-line-numbers.js ``` #### Getting the library from CDN + ```html - + ``` + ```html - + ``` ## Usage Download plugin and include file after highlight.js: + ```html @@ -34,6 +39,7 @@ Download plugin and include file after highlight.js: ``` Initialize plugin after highlight.js: + ```js hljs.initHighlightingOnLoad(); @@ -41,6 +47,7 @@ hljs.initLineNumbersOnLoad(); ``` Here’s an equivalent way to calling `initLineNumbersOnLoad` using jQuery: + ```js $(document).ready(function() { $('code.hljs').each(function(i, block) { @@ -50,6 +57,7 @@ $(document).ready(function() { ``` If your needs cool style, add styles by taste: + ```css /* for block of numbers */ .hljs-ln-numbers { @@ -79,11 +87,12 @@ If your needs cool style, add styles by taste: After version 2.1 plugin has optional parameter `options` - for custom setup. -name | type | default value | description ------------|---------|---------------|----------------------- -singleLine | boolean | false | enable plugin for code block with one line +version | name | type | default value | description +--------|------------|---------|---------------|----------------------- +v2.1 | singleLine | boolean | false | enable plugin for code block with one line +v2.8 | startFrom | int | 1 | [Start numbering from a custom value](startFrom) -#### Examples of using +### Examples of using ```js hljs.initLineNumbersOnLoad({ @@ -95,6 +104,40 @@ hljs.initLineNumbersOnLoad({ hljs.lineNumbersBlock(myCodeBlock, myOptions); ``` +```js +hljs.lineNumbersValue(myCodeBlock, myOptions); +``` + +### startFrom + +If you want numbering to start from some other value than `1`, you can specify a _numbering offset_, in one of the following ways: + +- Specifying desired offset in `hljs.lineNumbersBlock()` call, as in: + +```js +hljs.lineNumbersBlock(myCodeBlock, { + startFrom: 10 +}); +``` + +- Specifying the desired offset in `data-ln-start-from` attribute of `code` element, as in: + +```html +
+    
+    ...
+    
+
+``` + +In both cases numbering offset will be `10`, meaning that the numbering will start from `10`. + +## Skipping some blocks + +(Applies to `hljs.initLineNumbersOnLoad()` initialization only.) + +If you want to skip some of your `code` blocks (to leave them unnumbered), you can mark them with `.nohljsln` class. + ## CSS selectors You may need to select some lines of code after rendering. For instance, you may want @@ -110,4 +153,4 @@ CSS selector | description `.hljs-ln-numbers[data-line-number="i"]` | Select the ith line number, excluding the line of code `.hljs-ln-code[data-line-number="i"]` | Select the ith line of code, excluding the line number --- -© 2018 Yauheni Pakala | MIT License +© 2020 Yauheni Pakala and [Community](https://github.com/wcoder/highlightjs-line-numbers.js/graphs/contributors) | MIT License diff --git a/bower.json b/bower.json index 37be8fe..440708d 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "highlightjs-line-numbers.js", - "version": "2.7.0", + "version": "2.8.0", "homepage": "https://github.com/wcoder/highlightjs-line-numbers.js", "authors": [ "Yauheni Pakala " diff --git a/dist/highlightjs-line-numbers.min.js b/dist/highlightjs-line-numbers.min.js index fb886a0..a5f9f20 100644 --- a/dist/highlightjs-line-numbers.min.js +++ b/dist/highlightjs-line-numbers.min.js @@ -1 +1 @@ -!function(e,n){"use strict";function t(e){for(var n=e;n;){if(n.className&&n.className.indexOf("hljs-ln-code")!==-1)return!0;n=n.parentNode}return!1}function r(e){for(var n=e;"TABLE"!==n.nodeName;)n=n.parentNode;return n}function o(e){for(var n=e.toString(),t=e.anchorNode;"TD"!==t.nodeName;)t=t.parentNode;for(var o=e.focusNode;"TD"!==o.nodeName;)o=o.parentNode;var a=parseInt(t.dataset.lineNumber),i=parseInt(o.dataset.lineNumber);if(a!=i){var l=t.textContent,s=o.textContent;if(a>i){var c=a;a=i,i=c,c=l,l=s,s=c}for(;0!==n.indexOf(l);)l=l.slice(1);for(;n.lastIndexOf(s)===-1;)s=s.slice(0,-1);for(var d=l,u=r(t),f=a+1;fn){for(var r="",o=0,a=t.length;o
{6}',[L,j,y,T,b,o+1,t[o].length>0?t[o]:" "]);return m('{1}
',[N,r])}return e}function f(e){var n=e.childNodes;for(var t in n)if(n.hasOwnProperty(t)){var r=n[t];p(r.textContent)>0&&(r.childNodes.length>0?f(r):h(r.parentNode))}}function h(e){var n=e.className;if(/hljs-/.test(n)){for(var t=v(e.innerHTML),r=0,o="";r0?t[r]:" ";o+=m('{1}\n',[n,a])}e.innerHTML=o.trim()}}function v(e){return 0===e.length?[]:e.split(x)}function p(e){return(e.trim().match(x)||[]).length}function g(n){e.setTimeout(n,0)}function m(e,n){return e.replace(/\{(\d+)\}/g,function(e,t){return n[t]?n[t]:e})}var N="hljs-ln",L="hljs-ln-line",b="hljs-ln-code",j="hljs-ln-numbers",y="hljs-ln-n",T="data-line-number",x=/\r\n|\r|\n/g;e.hljs?(e.hljs.initLineNumbersOnLoad=i,e.hljs.lineNumbersBlock=s,e.hljs.lineNumbersValue=c,a()):e.console.error("highlight.js not detected!"),document.addEventListener("copy",function(e){var n=window.getSelection();if(t(n.anchorNode)){var r;r=window.navigator.userAgent.indexOf("Edge")!==-1?o(n):n.toString(),e.clipboardData.setData("text/plain",r),e.preventDefault()}})}(window,document); \ No newline at end of file +!function(r,o){"use strict";var e,i="hljs-ln",l="hljs-ln-line",h="hljs-ln-code",s="hljs-ln-numbers",c="hljs-ln-n",m="data-line-number",a=/\r\n|\r|\n/g;function u(e){for(var n=e.toString(),t=e.anchorNode;"TD"!==t.nodeName;)t=t.parentNode;for(var r=e.focusNode;"TD"!==r.nodeName;)r=r.parentNode;var o=parseInt(t.dataset.lineNumber),a=parseInt(r.dataset.lineNumber);if(o==a)return n;var i,l=t.textContent,s=r.textContent;for(a
{6}',[l,s,c,m,h,o+n.startFrom,0{1}',[i,r])}return e}(e.innerHTML,o)}function v(e){var n=e.className;if(/hljs-/.test(n)){for(var t=g(e.innerHTML),r=0,o="";r{1}\n',[n,0 --> - - + +