From 02b220e000eb37363b27871d1bd32c8b4e4e012c Mon Sep 17 00:00:00 2001 From: Golmote Date: Wed, 26 Aug 2015 19:46:48 +0200 Subject: [PATCH] Fix computation of line numbers for single-line code blocks. Fix #721 --- plugins/line-numbers/prism-line-numbers.js | 3 ++- plugins/line-numbers/prism-line-numbers.min.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/line-numbers/prism-line-numbers.js b/plugins/line-numbers/prism-line-numbers.js index 9cf273fce4..d8a25bf71a 100644 --- a/plugins/line-numbers/prism-line-numbers.js +++ b/plugins/line-numbers/prism-line-numbers.js @@ -28,7 +28,8 @@ Prism.hooks.add('complete', function (env) { pre.className += ' line-numbers'; } - var linesNum = env.code.match(/\n(?!$)/g).length + 1; + var match = env.code.match(/\n(?!$)/g); + var linesNum = match ? match.length + 1 : 1; var lineNumbersWrapper; var lines = new Array(linesNum + 1); diff --git a/plugins/line-numbers/prism-line-numbers.min.js b/plugins/line-numbers/prism-line-numbers.min.js index 65f6e55b9d..6cd713b3f9 100644 --- a/plugins/line-numbers/prism-line-numbers.min.js +++ b/plugins/line-numbers/prism-line-numbers.min.js @@ -1 +1 @@ -Prism.hooks.add("complete",function(e){if(e.code){var t=e.element.parentNode,s=/\s*\bline-numbers\b\s*/;if(t&&/pre/i.test(t.nodeName)&&(s.test(t.className)||s.test(e.element.className))&&!e.element.querySelector(".line-numbers-rows")){s.test(e.element.className)&&(e.element.className=e.element.className.replace(s,"")),s.test(t.className)||(t.className+=" line-numbers");var a,n=e.code.match(/\n(?!$)/g).length+1,l=new Array(n+1);l=l.join(""),a=document.createElement("span"),a.className="line-numbers-rows",a.innerHTML=l,t.hasAttribute("data-start")&&(t.style.counterReset="linenumber "+(parseInt(t.getAttribute("data-start"),10)-1)),e.element.appendChild(a)}}}); \ No newline at end of file +Prism.hooks.add("complete",function(e){if(e.code){var t=e.element.parentNode,s=/\s*\bline-numbers\b\s*/;if(t&&/pre/i.test(t.nodeName)&&(s.test(t.className)||s.test(e.element.className))&&!e.element.querySelector(".line-numbers-rows")){s.test(e.element.className)&&(e.element.className=e.element.className.replace(s,"")),s.test(t.className)||(t.className+=" line-numbers");var a,n=e.code.match(/\n(?!$)/g),l=n?n.length+1:1,m=new Array(l+1);m=m.join(""),a=document.createElement("span"),a.className="line-numbers-rows",a.innerHTML=m,t.hasAttribute("data-start")&&(t.style.counterReset="linenumber "+(parseInt(t.getAttribute("data-start"),10)-1)),e.element.appendChild(a)}}}); \ No newline at end of file