cmark

My personal build of CMark ✏️

Commit
e618715636a3bd60930bea34d214b3aaf8e9e766
Parent
9918827edd3dd6630f7417f1efca673bae55e6dd
Author
John MacFarlane <jgm@berkeley.edu>
Date

Require space before closing # sequence in ATX header.

Closes #169.

Diffstat

3 files changed, 23 insertions, 15 deletions

Status File Name N° Changes Insertions Deletions
Modified js/lib/blocks.js 2 1 1
Modified spec.txt 24 16 8
Modified src/blocks.c 12 6 6
diff --git a/js/lib/blocks.js b/js/lib/blocks.js
@@ -366,7 +366,7 @@ var incorporateLine = function(ln, line_number) {
             container.level = match[0].trim().length; // number of #s
             // remove trailing ###s:
             container.strings =
-                [ln.slice(offset).replace(/(?:(\\#) *#*| *#+) *$/,'$1')];
+                [ln.slice(offset).replace(/^ *#+ *$/, '').replace(/ +#+ *$/,'')];
             break;
 
         } else if ((match = ln.slice(first_nonspace).match(/^`{3,}(?!.*`)|^~{3,}(?!.*~)/))) {
diff --git a/spec.txt b/spec.txt
@@ -479,11 +479,11 @@ consists of a string of characters, parsed as inline content, between an
 opening sequence of 1--6 unescaped `#` characters and an optional
 closing sequence of any number of `#` characters.  The opening sequence
 of `#` characters cannot be followed directly by a nonspace character.
-The closing `#` characters may be followed by spaces only.  The opening
-`#` character may be indented 0-3 spaces.  The raw contents of the
-header are stripped of leading and trailing spaces before being parsed
-as inline content.  The header level is equal to the number of `#`
-characters in the opening sequence.
+The optional closing sequence of `#`s must be preceded by a space and may be
+followed by spaces only.  The opening `#` character may be indented 0-3
+spaces.  The raw contents of the header are stripped of leading and
+trailing spaces before being parsed as inline content.  The header level
+is equal to the number of `#` characters in the opening sequence.
 
 Simple headers:
 
@@ -614,16 +614,24 @@ header:
 <h3>foo ### b</h3>
 .
 
+The closing sequence must be preceded by a space:
+
+.
+# foo#
+.
+<h1>foo#</h1>
+.
+
 Backslash-escaped `#` characters do not count as part
 of the closing sequence:
 
 .
 ### foo \###
-## foo \#\##
+## foo #\##
 # foo \#
 .
-<h3>foo #</h3>
-<h2>foo ##</h2>
+<h3>foo ###</h3>
+<h2>foo ###</h2>
 <h1>foo #</h1>
 .
 
diff --git a/src/blocks.c b/src/blocks.c
@@ -432,15 +432,15 @@ static void chop_trailing_hashtags(chunk *ch)
 	chunk_rtrim(ch);
 	orig_n = n = ch->len - 1;
 
-	// if string ends in #s, remove these:
+	// if string ends in space followed by #s, remove these:
 	while (n >= 0 && peek_at(ch, n) == '#')
 		n--;
 
-	// the last # was escaped, so we include it.
-	if (n != orig_n && n >= 0 && peek_at(ch, n) == '\\')
-		n++;
-
-	ch->len = n + 1;
+	// Check for a be a space before the final #s:
+	if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
+	    ch->len = n;
+	    chunk_rtrim(ch);
+	}
 }
 
 // Process one line at a time, modifying a node_block.