cmark

My personal build of CMark ✏️

Commit
ffbca7b85198c2d0efd71b95ef4a1fa693578af0
Parent
a0cbcefe82a6bff0a9b44550e22244d6d5d727c0
Author
John MacFarlane <jgm@berkeley.edu>
Date

Factored out unescapeString into new module, js/common.js.

This is used in both blocks.js and inlines.js.

Diffstat

3 files changed, 32 insertions, 26 deletions

Status File Name N° Changes Insertions Deletions
Modified js/lib/blocks.js 3 1 2
Added js/lib/common.js 30 30 0
Modified js/lib/inlines.js 25 1 24
diff --git a/js/lib/blocks.js b/js/lib/blocks.js
@@ -1,4 +1,5 @@
 var Node = require('./node');
+var unescapeString = require('./common').unescapeString;
 
 var C_GREATERTHAN = 62;
 var C_NEWLINE = 10;
@@ -7,8 +8,6 @@ var C_OPEN_BRACKET = 91;
 
 var InlineParser = require('./inlines');
 
-var unescapeString = new InlineParser().unescapeString;
-
 var BLOCKTAGNAME = '(?:article|header|aside|hgroup|iframe|blockquote|hr|body|li|map|button|object|canvas|ol|caption|output|col|p|colgroup|pre|dd|progress|div|section|dl|table|td|dt|tbody|embed|textarea|fieldset|tfoot|figcaption|th|figure|thead|footer|footer|tr|form|ul|h1|h2|h3|h4|h5|h6|video|script|style)';
 
 var HTMLBLOCKOPEN = "<(?:" + BLOCKTAGNAME + "[\\s/>]" + "|" +
diff --git a/js/lib/common.js b/js/lib/common.js
@@ -0,0 +1,30 @@
+var entityToChar = require('./html5-entities.js').entityToChar;
+
+var ENTITY = "&(?:#x[a-f0-9]{1,8}|#[0-9]{1,8}|[a-z][a-z0-9]{1,31});";
+
+var reBackslashOrAmp = /[\\&]/;
+
+var ESCAPABLE = '[!"#$%&\'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]';
+
+var reEntityOrEscapedChar = new RegExp('\\\\' + ESCAPABLE + '|' + ENTITY, 'gi');
+
+var unescapeChar = function(s) {
+    "use strict";
+    if (s[0] === '\\') {
+        return s[1];
+    } else {
+        return entityToChar(s);
+    }
+};
+
+// Replace entities and backslash escapes with literal characters.
+var unescapeString = function(s) {
+    "use strict";
+    if (reBackslashOrAmp.test(s)) {
+        return s.replace(reEntityOrEscapedChar, unescapeChar);
+    } else {
+        return s;
+    }
+};
+
+module.exports = { unescapeString: unescapeString };
diff --git a/js/lib/inlines.js b/js/lib/inlines.js
@@ -1,4 +1,5 @@
 var Node = require('./node');
+var unescapeString = require('./common').unescapeString;
 
 var fromCodePoint = require('./from-code-point.js');
 var entityToChar = require('./html5-entities.js').entityToChar;
@@ -63,10 +64,6 @@ var reEscapable = new RegExp(ESCAPABLE);
 
 var reEntityHere = new RegExp('^' + ENTITY, 'i');
 
-var reEntityOrEscapedChar = new RegExp('\\\\' + ESCAPABLE + '|' + ENTITY, 'gi');
-
-var reBackslashOrAmp = /[\\&]/;
-
 var reTicks = new RegExp('`+');
 
 var reTicksHere = new RegExp('^`+');
@@ -92,25 +89,6 @@ var reLinkLabel = /^\[(?:[^\\\[\]]|\\[\[\]]){0,1000}\]/;
 // Matches a string of non-special characters.
 var reMain = /^[^\n`\[\]\\!<&*_]+/m;
 
-var unescapeChar = function(s) {
-    "use strict";
-    if (s[0] === '\\') {
-        return s[1];
-    } else {
-        return entityToChar(s);
-    }
-};
-
-// Replace entities and backslash escapes with literal characters.
-var unescapeString = function(s) {
-    "use strict";
-    if (reBackslashOrAmp.test(s)) {
-        return s.replace(reEntityOrEscapedChar, unescapeChar);
-    } else {
-        return s;
-    }
-};
-
 // Normalize reference label: collapse internal whitespace
 // to single space, remove leading/trailing whitespace, case fold.
 var normalizeReference = function(s) {
@@ -859,7 +837,6 @@ function InlineParser(){
         match: match,
         peek: peek,
         spnl: spnl,
-        unescapeString: unescapeString,
         parseBackticks: parseBackticks,
         parseBackslash: parseBackslash,
         parseAutolink: parseAutolink,