@@ -74,23 +74,10 @@ var peek = function(ln, pos) {
74
74
75
75
// These are methods of a Parser object, defined below.
76
76
77
- // Returns true if block ends with a blank line, descending if needed
78
- // into lists and sublists.
77
+ // Returns true if block ends with a blank line.
79
78
var endsWithBlankLine = function ( block ) {
80
- while ( block ) {
81
- if ( block . _lastLineBlank ) {
82
- return true ;
83
- }
84
- var t = block . type ;
85
- if ( ! block . _lastLineChecked && ( t === "list" || t === "item" ) ) {
86
- block . _lastLineChecked = true ;
87
- block = block . _lastChild ;
88
- } else {
89
- block . _lastLineChecked = true ;
90
- break ;
91
- }
92
- }
93
- return false ;
79
+ return block . next &&
80
+ block . sourcepos [ 1 ] [ 0 ] !== block . next . sourcepos [ 0 ] [ 0 ] - 1 ;
94
81
} ;
95
82
96
83
// Add a line to the block at the tip. We assume the tip
@@ -221,6 +208,43 @@ var closeUnmatchedBlocks = function() {
221
208
}
222
209
} ;
223
210
211
+ // Remove link reference definitions from given tree.
212
+ var removeLinkReferenceDefinitions = function ( parser , tree ) {
213
+ var event , node ;
214
+ var walker = tree . walker ( ) ;
215
+ var emptyNodes = [ ] ;
216
+
217
+ while ( ( event = walker . next ( ) ) ) {
218
+ node = event . node ;
219
+ if ( event . entering && node . type === "paragraph" ) {
220
+ var pos ;
221
+ var hasReferenceDefs = false ;
222
+
223
+ // Try parsing the beginning as link reference definitions;
224
+ // Note that link reference definitions must be the beginning of a
225
+ // paragraph node since link reference definitions cannot interrupt
226
+ // paragraphs.
227
+ while (
228
+ peek ( node . _string_content , 0 ) === C_OPEN_BRACKET &&
229
+ ( pos = parser . inlineParser . parseReference (
230
+ node . _string_content ,
231
+ parser . refmap
232
+ ) )
233
+ ) {
234
+ node . _string_content = node . _string_content . slice ( pos ) ;
235
+ hasReferenceDefs = true ;
236
+ }
237
+ if ( hasReferenceDefs && isBlank ( node . _string_content ) ) {
238
+ emptyNodes . push ( node ) ;
239
+ }
240
+ }
241
+ }
242
+
243
+ for ( node of emptyNodes ) {
244
+ node . unlink ( ) ;
245
+ }
246
+ } ;
247
+
224
248
// 'finalize' is run when the block is closed.
225
249
// 'continue' is run to check whether the block is continuing
226
250
// at a certain line and offset (e.g. whether a block quote
@@ -231,7 +255,8 @@ var blocks = {
231
255
continue : function ( ) {
232
256
return 0 ;
233
257
} ,
234
- finalize : function ( ) {
258
+ finalize : function ( parser , block ) {
259
+ removeLinkReferenceDefinitions ( parser , block ) ;
235
260
return ;
236
261
} ,
237
262
canContain : function ( t ) {
@@ -247,7 +272,7 @@ var blocks = {
247
272
var item = block . _firstChild ;
248
273
while ( item ) {
249
274
// check for non-final list item ending with blank line:
250
- if ( endsWithBlankLine ( item ) && item . _next ) {
275
+ if ( item . _next && endsWithBlankLine ( item ) ) {
251
276
block . _listData . tight = false ;
252
277
break ;
253
278
}
@@ -256,8 +281,8 @@ var blocks = {
256
281
var subitem = item . _firstChild ;
257
282
while ( subitem ) {
258
283
if (
259
- endsWithBlankLine ( subitem ) &&
260
- ( item . _next || subitem . _next )
284
+ subitem . _next &&
285
+ endsWithBlankLine ( subitem )
261
286
) {
262
287
block . _listData . tight = false ;
263
288
break ;
@@ -266,6 +291,7 @@ var blocks = {
266
291
}
267
292
item = item . _next ;
268
293
}
294
+ block . sourcepos [ 1 ] = block . _lastChild . sourcepos [ 1 ] ;
269
295
} ,
270
296
canContain : function ( t ) {
271
297
return t === "item" ;
@@ -320,7 +346,16 @@ var blocks = {
320
346
}
321
347
return 0 ;
322
348
} ,
323
- finalize : function ( ) {
349
+ finalize : function ( parser , block ) {
350
+ if ( block . _lastChild ) {
351
+ block . sourcepos [ 1 ] = block . _lastChild . sourcepos [ 1 ] ;
352
+ } else {
353
+ // Empty list item
354
+ block . sourcepos [ 1 ] [ 0 ] = block . sourcepos [ 0 ] [ 0 ] ;
355
+ block . sourcepos [ 1 ] [ 1 ] =
356
+ block . _listData . markerOffset + block . _listData . padding ;
357
+ }
358
+
324
359
return ;
325
360
} ,
326
361
canContain : function ( t ) {
@@ -402,10 +437,17 @@ var blocks = {
402
437
block . _literal = rest ;
403
438
} else {
404
439
// indented
405
- block . _literal = block . _string_content . replace (
406
- / ( \n * ) + $ / ,
407
- "\n"
408
- ) ;
440
+ var lines = block . _string_content . split ( "\n" ) ;
441
+ // Note that indented code block cannot be empty, so
442
+ // lines.length cannot be zero.
443
+ while ( / ^ [ \t ] * $ / . test ( lines [ lines . length - 1 ] ) ) {
444
+ lines . pop ( ) ;
445
+ }
446
+ block . _literal = lines . join ( "\n" ) + "\n" ;
447
+ block . sourcepos [ 1 ] [ 0 ] =
448
+ block . sourcepos [ 0 ] [ 0 ] + lines . length - 1 ;
449
+ block . sourcepos [ 1 ] [ 1 ] =
450
+ block . sourcepos [ 0 ] [ 1 ] + lines [ lines . length - 1 ] . length - 1 ;
409
451
}
410
452
block . _string_content = null ; // allow GC
411
453
} ,
@@ -435,24 +477,8 @@ var blocks = {
435
477
continue : function ( parser ) {
436
478
return parser . blank ? 1 : 0 ;
437
479
} ,
438
- finalize : function ( parser , block ) {
439
- var pos ;
440
- var hasReferenceDefs = false ;
441
-
442
- // try parsing the beginning as link reference definitions:
443
- while (
444
- peek ( block . _string_content , 0 ) === C_OPEN_BRACKET &&
445
- ( pos = parser . inlineParser . parseReference (
446
- block . _string_content ,
447
- parser . refmap
448
- ) )
449
- ) {
450
- block . _string_content = block . _string_content . slice ( pos ) ;
451
- hasReferenceDefs = true ;
452
- }
453
- if ( hasReferenceDefs && isBlank ( block . _string_content ) ) {
454
- block . unlink ( ) ;
455
- }
480
+ finalize : function ( ) {
481
+ return ;
456
482
} ,
457
483
canContain : function ( ) {
458
484
return false ;
@@ -835,33 +861,9 @@ var incorporateLine = function(ln) {
835
861
836
862
// finalize any blocks not matched
837
863
this . closeUnmatchedBlocks ( ) ;
838
- if ( this . blank && container . lastChild ) {
839
- container . lastChild . _lastLineBlank = true ;
840
- }
841
864
842
865
t = container . type ;
843
866
844
- // Block quote lines are never blank as they start with >
845
- // and we don't count blanks in fenced code for purposes of tight/loose
846
- // lists or breaking out of lists. We also don't set _lastLineBlank
847
- // on an empty list item, or if we just closed a fenced block.
848
- var lastLineBlank =
849
- this . blank &&
850
- ! (
851
- t === "block_quote" ||
852
- ( t === "code_block" && container . _isFenced ) ||
853
- ( t === "item" &&
854
- ! container . _firstChild &&
855
- container . sourcepos [ 0 ] [ 0 ] === this . lineNumber )
856
- ) ;
857
-
858
- // propagate lastLineBlank up through parents:
859
- var cont = container ;
860
- while ( cont ) {
861
- cont . _lastLineBlank = lastLineBlank ;
862
- cont = cont . _parent ;
863
- }
864
-
865
867
if ( this . blocks [ t ] . acceptsLines ) {
866
868
this . addLine ( ) ;
867
869
// if HtmlBlock, check for end condition
0 commit comments