@@ -34,7 +34,6 @@ function fromMarkdown(value, encoding, options) {
34
34
// Note this compiler only understand complete buffering, not streaming.
35
35
function compiler ( options ) {
36
36
var settings = options || { }
37
- var stack = [ { type : 'root' , children : [ ] } ]
38
37
39
38
var handlers = configure (
40
39
{
@@ -139,6 +138,7 @@ function compiler(options) {
139
138
return compile
140
139
141
140
function compile ( events ) {
141
+ var stack = [ { type : 'root' , children : [ ] } ]
142
142
var index = - 1
143
143
var listStack = [ ]
144
144
var length
@@ -175,6 +175,7 @@ function compiler(options) {
175
175
if ( own . call ( handler , events [ index ] [ 1 ] . type ) ) {
176
176
handler [ events [ index ] [ 1 ] . type ] . call (
177
177
{
178
+ stack : stack ,
178
179
handlers : handlers ,
179
180
enter : enter ,
180
181
exit : exit ,
@@ -335,18 +336,18 @@ function compiler(options) {
335
336
return open
336
337
337
338
function open ( token ) {
338
- enter ( create ( token ) , token )
339
+ enter . call ( this , create ( token ) , token )
339
340
if ( and ) and . call ( this , token )
340
341
}
341
342
}
342
343
343
344
function buffer ( ) {
344
- stack . push ( { type : 'fragment' , children : [ ] } )
345
+ this . stack . push ( { type : 'fragment' , children : [ ] } )
345
346
}
346
347
347
348
function enter ( node , token ) {
348
- stack [ stack . length - 1 ] . children . push ( node )
349
- stack . push ( node )
349
+ this . stack [ this . stack . length - 1 ] . children . push ( node )
350
+ this . stack . push ( node )
350
351
node . position = { start : point ( token . start ) }
351
352
return node
352
353
}
@@ -356,18 +357,18 @@ function compiler(options) {
356
357
357
358
function close ( token ) {
358
359
if ( and ) and . call ( this , token )
359
- exit ( token )
360
+ exit . call ( this , token )
360
361
}
361
362
}
362
363
363
364
function exit ( token ) {
364
- var node = stack . pop ( )
365
+ var node = this . stack . pop ( )
365
366
node . position . end = point ( token . end )
366
367
return node
367
368
}
368
369
369
370
function resume ( ) {
370
- var value = toString ( stack . pop ( ) )
371
+ var value = toString ( this . stack . pop ( ) )
371
372
return value
372
373
}
373
374
@@ -381,7 +382,7 @@ function compiler(options) {
381
382
382
383
function onenterlistitemvalue ( token ) {
383
384
if ( expectingFirstListItemValue ) {
384
- stack [ stack . length - 2 ] . start = parseInt (
385
+ this . stack [ this . stack . length - 2 ] . start = parseInt (
385
386
this . sliceSerialize ( token ) ,
386
387
constants . numericBaseDecimal
387
388
)
@@ -390,55 +391,60 @@ function compiler(options) {
390
391
}
391
392
392
393
function onexitcodefencedfenceinfo ( ) {
393
- var data = resume ( )
394
- stack [ stack . length - 1 ] . lang = data
394
+ var data = this . resume ( )
395
+ this . stack [ this . stack . length - 1 ] . lang = data
395
396
}
396
397
397
398
function onexitcodefencedfencemeta ( ) {
398
- var data = resume ( )
399
- stack [ stack . length - 1 ] . meta = data
399
+ var data = this . resume ( )
400
+ this . stack [ this . stack . length - 1 ] . meta = data
400
401
}
401
402
402
403
function onexitcodefencedfence ( ) {
403
404
// Exit if this is the closing fence.
404
405
if ( flowCodeInside ) return
405
- buffer ( )
406
+ this . buffer ( )
406
407
flowCodeInside = true
407
408
}
408
409
409
410
function onexitcodefenced ( ) {
410
- var data = resume ( )
411
- stack [ stack . length - 1 ] . value = data . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
411
+ var data = this . resume ( )
412
+ this . stack [ this . stack . length - 1 ] . value = data . replace (
413
+ / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g,
414
+ ''
415
+ )
412
416
flowCodeInside = undefined
413
417
}
414
418
415
419
function onexitcodeindented ( ) {
416
- var data = resume ( )
417
- stack [ stack . length - 1 ] . value = data
420
+ var data = this . resume ( )
421
+ this . stack [ this . stack . length - 1 ] . value = data
418
422
}
419
423
420
424
function onexitdefinitionlabelstring ( token ) {
421
425
// Discard label, use the source content instead.
422
- var label = resume ( )
423
- stack [ stack . length - 1 ] . label = label
424
- stack [ stack . length - 1 ] . identifier = normalizeIdentifier (
426
+ var label = this . resume ( )
427
+ this . stack [ this . stack . length - 1 ] . label = label
428
+ this . stack [ this . stack . length - 1 ] . identifier = normalizeIdentifier (
425
429
this . sliceSerialize ( token )
426
430
) . toLowerCase ( )
427
431
}
428
432
429
433
function onexitdefinitiontitlestring ( ) {
430
- var data = resume ( )
431
- stack [ stack . length - 1 ] . title = data
434
+ var data = this . resume ( )
435
+ this . stack [ this . stack . length - 1 ] . title = data
432
436
}
433
437
434
438
function onexitdefinitiondestinationstring ( ) {
435
- var data = resume ( )
436
- stack [ stack . length - 1 ] . url = data
439
+ var data = this . resume ( )
440
+ this . stack [ this . stack . length - 1 ] . url = data
437
441
}
438
442
439
443
function onexitatxheadingsequence ( token ) {
440
- if ( ! stack [ stack . length - 1 ] . depth ) {
441
- stack [ stack . length - 1 ] . depth = this . sliceSerialize ( token ) . length
444
+ if ( ! this . stack [ this . stack . length - 1 ] . depth ) {
445
+ this . stack [ this . stack . length - 1 ] . depth = this . sliceSerialize (
446
+ token
447
+ ) . length
442
448
}
443
449
}
444
450
@@ -447,7 +453,7 @@ function compiler(options) {
447
453
}
448
454
449
455
function onexitsetextheadinglinesequence ( token ) {
450
- stack [ stack . length - 1 ] . depth =
456
+ this . stack [ this . stack . length - 1 ] . depth =
451
457
this . sliceSerialize ( token ) . charCodeAt ( 0 ) === codes . equalsTo ? 1 : 2
452
458
}
453
459
@@ -456,27 +462,27 @@ function compiler(options) {
456
462
}
457
463
458
464
function onenterdata ( token ) {
459
- var siblings = stack [ stack . length - 1 ] . children
465
+ var siblings = this . stack [ this . stack . length - 1 ] . children
460
466
var tail = siblings [ siblings . length - 1 ]
461
467
462
468
if ( ! tail || tail . type !== 'text' ) {
463
469
// Add a new text node.
464
470
tail = text ( )
465
471
tail . position = { start : point ( token . start ) }
466
- stack [ stack . length - 1 ] . children . push ( tail )
472
+ this . stack [ this . stack . length - 1 ] . children . push ( tail )
467
473
}
468
474
469
- stack . push ( tail )
475
+ this . stack . push ( tail )
470
476
}
471
477
472
478
function onexitdata ( token ) {
473
- var tail = stack . pop ( )
479
+ var tail = this . stack . pop ( )
474
480
tail . value += this . sliceSerialize ( token )
475
481
tail . position . end = point ( token . end )
476
482
}
477
483
478
484
function onexitlineending ( token ) {
479
- var context = stack [ stack . length - 1 ]
485
+ var context = this . stack [ this . stack . length - 1 ]
480
486
481
487
// If we’re at a hard break, include the line ending in there.
482
488
if ( atHardBreak ) {
@@ -508,26 +514,26 @@ function compiler(options) {
508
514
}
509
515
510
516
function onexithtmlflow ( ) {
511
- var data = resume ( )
512
- stack [ stack . length - 1 ] . value = data
517
+ var data = this . resume ( )
518
+ this . stack [ this . stack . length - 1 ] . value = data
513
519
}
514
520
515
521
function onexithtmltext ( ) {
516
- var data = resume ( )
517
- stack [ stack . length - 1 ] . value = data
522
+ var data = this . resume ( )
523
+ this . stack [ this . stack . length - 1 ] . value = data
518
524
}
519
525
520
526
function onexitcodetext ( ) {
521
- var data = resume ( )
522
- stack [ stack . length - 1 ] . value = data
527
+ var data = this . resume ( )
528
+ this . stack [ this . stack . length - 1 ] . value = data
523
529
}
524
530
525
531
function onenterimage ( ) {
526
- buffer ( )
532
+ this . buffer ( )
527
533
}
528
534
529
535
function onexitlink ( ) {
530
- var context = stack [ stack . length - 1 ]
536
+ var context = this . stack [ this . stack . length - 1 ]
531
537
532
538
// To do: clean.
533
539
if ( inReference ) {
@@ -545,7 +551,7 @@ function compiler(options) {
545
551
}
546
552
547
553
function onexitimage ( ) {
548
- var context = stack [ stack . length - 1 ]
554
+ var context = this . stack [ this . stack . length - 1 ]
549
555
550
556
// To do: clean.
551
557
if ( inReference ) {
@@ -564,10 +570,10 @@ function compiler(options) {
564
570
565
571
function onexitlabeltext ( token ) {
566
572
var ctx =
567
- stack [ stack . length - 1 ] . type === 'fragment'
568
- ? stack [ stack . length - 2 ]
569
- : stack [ stack . length - 1 ]
570
- ctx . label = toString ( stack [ stack . length - 1 ] )
573
+ this . stack [ this . stack . length - 1 ] . type === 'fragment'
574
+ ? this . stack [ this . stack . length - 2 ]
575
+ : this . stack [ this . stack . length - 1 ]
576
+ ctx . label = toString ( this . stack [ this . stack . length - 1 ] )
571
577
ctx . identifier = normalizeIdentifier (
572
578
this . sliceSerialize ( token )
573
579
) . toLowerCase ( )
@@ -580,20 +586,20 @@ function compiler(options) {
580
586
inReference = true
581
587
582
588
// If we’re in a fragment, we’re in an image and buffering.
583
- if ( stack [ stack . length - 1 ] . type === 'fragment' ) {
584
- data = resume ( )
585
- stack [ stack . length - 1 ] . alt = data
589
+ if ( this . stack [ this . stack . length - 1 ] . type === 'fragment' ) {
590
+ data = this . resume ( )
591
+ this . stack [ this . stack . length - 1 ] . alt = data
586
592
}
587
593
}
588
594
589
595
function onexitresourcedestinationstring ( ) {
590
- var data = resume ( )
591
- stack [ stack . length - 1 ] . url = data
596
+ var data = this . resume ( )
597
+ this . stack [ this . stack . length - 1 ] . url = data
592
598
}
593
599
594
600
function onexitresourcetitlestring ( ) {
595
- var data = resume ( )
596
- stack [ stack . length - 1 ] . title = data
601
+ var data = this . resume ( )
602
+ this . stack [ this . stack . length - 1 ] . title = data
597
603
}
598
604
599
605
function onexitresource ( ) {
@@ -605,9 +611,9 @@ function compiler(options) {
605
611
}
606
612
607
613
function onexitreferencestring ( token ) {
608
- var label = resume ( )
609
- stack [ stack . length - 1 ] . label = label
610
- stack [ stack . length - 1 ] . identifier = normalizeIdentifier (
614
+ var label = this . resume ( )
615
+ this . stack [ this . stack . length - 1 ] . label = label
616
+ this . stack [ this . stack . length - 1 ] . identifier = normalizeIdentifier (
611
617
this . sliceSerialize ( token )
612
618
) . toLowerCase ( )
613
619
referenceType = 'full'
@@ -632,18 +638,19 @@ function compiler(options) {
632
638
value = decode ( data )
633
639
}
634
640
635
- stack [ stack . length - 1 ] . value += value
641
+ this . stack [ this . stack . length - 1 ] . value += value
636
642
characterReferenceType = undefined
637
643
}
638
644
639
645
function onexitautolinkprotocol ( token ) {
640
646
onexitdata . call ( this , token )
641
- stack [ stack . length - 1 ] . url = this . sliceSerialize ( token )
647
+ this . stack [ this . stack . length - 1 ] . url = this . sliceSerialize ( token )
642
648
}
643
649
644
650
function onexitautolinkemail ( token ) {
645
651
onexitdata . call ( this , token )
646
- stack [ stack . length - 1 ] . url = 'mailto:' + this . sliceSerialize ( token )
652
+ this . stack [ this . stack . length - 1 ] . url =
653
+ 'mailto:' + this . sliceSerialize ( token )
647
654
}
648
655
649
656
//
0 commit comments