From 6f345edbcfbf2770ccdfb70c2a157eaf0e2930dd Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 9 Jan 2015 21:38:41 -0800 Subject: Improved newline parsing efficiency. Don't check for `\n` when we know we have one. Gobble spaces after line break. --- js/lib/inlines.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'js/lib/inlines.js') diff --git a/js/lib/inlines.js b/js/lib/inlines.js index adeac6c..f27a7e7 100644 --- a/js/lib/inlines.js +++ b/js/lib/inlines.js @@ -672,23 +672,20 @@ var parseString = function(block) { // line break; otherwise a soft line break. var parseNewline = function(block) { "use strict"; - var m = this.match(/^\n/); - if (m) { - // check previous node for trailing spaces - var lastc = block.lastChild; - if (lastc && lastc.t === 'Text') { - var sps = / *$/.exec(lastc.literal)[0].length; - if (sps > 0) { - lastc.literal = lastc.literal.replace(/ *$/,''); - } - block.appendChild(new Node(sps >= 2 ? 'Hardbreak' : 'Softbreak')); - } else { - block.appendChild(new Node('Softbreak')); + this.pos += 1; // assume we're at a \n + // check previous node for trailing spaces + var lastc = block.lastChild; + if (lastc && lastc.t === 'Text') { + var sps = / *$/.exec(lastc.literal)[0].length; + if (sps > 0) { + lastc.literal = lastc.literal.replace(/ *$/,''); } - return true; + block.appendChild(new Node(sps >= 2 ? 'Hardbreak' : 'Softbreak')); } else { - return false; + block.appendChild(new Node('Softbreak')); } + this.match(/^ */); // gobble leading spaces in next line + return true; }; // Attempt to parse a link reference, modifying refmap. -- cgit v1.2.3