From 3c68dcac2d30616904840c52d359653ba1f746b4 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 9 Jan 2015 14:30:49 -0800 Subject: Simplified reMain, with AST manipulation for 2-space hardbreak. Small performance improvement. --- js/lib/inlines.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'js') diff --git a/js/lib/inlines.js b/js/lib/inlines.js index 5cec93b..ff3ffde 100644 --- a/js/lib/inlines.js +++ b/js/lib/inlines.js @@ -71,7 +71,7 @@ var reEntity = new RegExp(ENTITY, 'gi'); // Matches a character with a special meaning in markdown, // or a string of non-special characters. Note: we match // clumps of _ or * or `, because they need to be handled in groups. -var reMain = /^(?:[_*`\n]+|[\[\]\\!<&*_]|(?: *[^\n `\[\]\\!<&*_]+)+|[ \n]+)/m; +var reMain = /^(?:[_*`\n]+|[\[\]\\!<&*_]|[^\n`\[\]\\!<&*_]+)/m; // Replace entities and backslash escapes with literal characters. var unescapeString = function(s) { @@ -670,10 +670,19 @@ var parseString = function(block) { // line break; otherwise a soft line break. var parseNewline = function(block) { "use strict"; - var m = this.match(/^ *\n/); + var m = this.match(/^\n/); if (m) { - var node = new Node(m.length > 2 ? 'Hardbreak' : 'Softbreak'); - block.appendChild(node); + // check previous node for trailing spaces + var lastc = block.lastChild; + if (lastc && lastc.t === 'Text') { + var sps = / *$/.exec(lastc.c)[0].length; + if (sps > 0) { + lastc.c = lastc.c.replace(/ *$/,''); + } + block.appendChild(new Node(sps >= 2 ? 'Hardbreak' : 'Softbreak')); + } else { + block.appendChild(new Node('Softbreak')); + } return true; } else { return false; -- cgit v1.2.3