- Commit
- 3c68dcac2d30616904840c52d359653ba1f746b4
- Parent
- 9d005eab33db7b31edca49982398c666ccc27567
- Author
- John MacFarlane <jgm@berkeley.edu>
- Date
Simplified reMain, with AST manipulation for 2-space hardbreak.
Small performance improvement.
My personal build of CMark ✏️
Simplified reMain, with AST manipulation for 2-space hardbreak.
Small performance improvement.
1 file changed, 13 insertions, 4 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | js/lib/inlines.js | 17 | 13 | 4 |
diff --git 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;