cmark

My personal build of CMark ✏️

Commit
6f345edbcfbf2770ccdfb70c2a157eaf0e2930dd
Parent
054fea748f5b6aae9043925abf9b66991cd51887
Author
John MacFarlane <jgm@berkeley.edu>
Date

Improved newline parsing efficiency.

Don't check for `\n` when we know we have one. Gobble spaces after line break.

Diffstat

2 files changed, 12 insertions, 16 deletions

Status File Name N° Changes Insertions Deletions
Modified js/lib/blocks.js 3 1 2
Modified js/lib/inlines.js 25 11 14
diff --git a/js/lib/blocks.js b/js/lib/blocks.js
@@ -552,8 +552,7 @@ var finalize = function(block, line_number) {
 
     switch (block.t) {
     case 'Paragraph':
-        block.string_content = block.strings.join('\n').replace(/^ {2,}/m, '');
-        // delete block.strings;
+        block.string_content = block.strings.join('\n');
 
         // try parsing the beginning as link reference definitions:
         while (block.string_content.charCodeAt(0) === C_OPEN_BRACKET &&
diff --git 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.