@@ -17,7 +17,7 @@ var block = {
1717 fences : noop ,
1818 hr : / ^ { 0 , 3 } ( (?: - * ) { 3 , } | (?: _ * ) { 3 , } | (?: \* * ) { 3 , } ) (?: \n + | $ ) / ,
1919 // cap[2] might be ' HEADING # ' and must be trimmed appropriately.
20- heading : / ^ * ( # { 1 , 6 } ) ( .* ) (?: \n + | $ ) / ,
20+ heading : / ^ { 0 , 3 } ( # { 1 , 6 } ) (?: [ ^ \S \n ] ( .* ) ) ? (?: \n + | $ ) / ,
2121 nptable : noop ,
2222 blockquote : / ^ ( { 0 , 3 } > ? ( p a r a g r a p h | [ ^ \n ] * ) (?: \n | $ ) ) + / ,
2323 list : / ^ ( * ) ( b u l l ) [ \s \S ] + ?(?: h r | d e f | \n { 2 , } (? ! ) (? ! \1b u l l ) \n * | \s * $ ) / ,
@@ -93,9 +93,7 @@ block.normal = merge({}, block);
9393
9494block . gfm = merge ( { } , block . normal , {
9595 fences : / ^ * ( ` { 3 , } | ~ { 3 , } ) [ \. ] * ( \S + ) ? * \n ( [ \s \S ] * ?) \n ? * \1 * (?: \n + | $ ) / ,
96- paragraph : / ^ / ,
97- // cap[2] might be ' HEADING # ' and must be trimmed appropriately.
98- heading : / ^ * ( # { 1 , 6 } ) ( .+ ) (?: \n + | $ ) /
96+ paragraph : / ^ /
9997} ) ;
10098
10199block . gfm . paragraph = edit ( block . paragraph )
@@ -118,6 +116,7 @@ block.tables = merge({}, block.gfm, {
118116 */
119117
120118block . pedantic = merge ( { } , block . normal , {
119+ heading : / ^ * ( # { 1 , 6 } ) ( .* ) (?: \n + | $ ) / ,
121120 html : edit (
122121 '^ *(?:comment *(?:\\n|\\s*$)'
123122 + '|<(tag)[\\s\\S]+?</\\1> *(?:\\n{2,}|\\s*$)' // closed tag
@@ -238,13 +237,18 @@ Lexer.prototype.token = function(src, top) {
238237 if ( cap = this . rules . heading . exec ( src ) ) {
239238 src = src . substring ( cap [ 0 ] . length ) ;
240239 // cap[2] might be ' HEADING # '
241- item = cap [ 2 ] . trim ( ) ;
240+ item = ( cap [ 2 ] || '' ) . trim ( ) ;
242241 if ( item . slice ( - 1 ) === '#' ) {
243242 // NB replace(/#+$/) is quadratic on mismatch because it's unanchored,
244243 // so we protect with if-check to ensure it won't mismatch.
245- item = item . replace ( / # + $ / , '' ) ;
244+ if ( this . options . pedantic ) {
245+ item = item . replace ( / # + $ / , '' ) ;
246+ } else {
247+ // CM requires a space before additional #s
248+ item = item . replace ( / ( \s | ^ ) # + $ / , '' ) ;
249+ }
246250 }
247- item = item . trim ( )
251+ item = item . trim ( ) ;
248252 this . tokens . push ( {
249253 type : 'heading' ,
250254 depth : cap [ 1 ] . length ,
0 commit comments