Skip to content

Commit 7a2573f

Browse files
committed
Refactor to move more to context
1 parent 49ff841 commit 7a2573f

File tree

1 file changed

+66
-59
lines changed

1 file changed

+66
-59
lines changed

index.js

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ function fromMarkdown(value, encoding, options) {
3434
// Note this compiler only understand complete buffering, not streaming.
3535
function compiler(options) {
3636
var settings = options || {}
37-
var stack = [{type: 'root', children: []}]
3837

3938
var handlers = configure(
4039
{
@@ -139,6 +138,7 @@ function compiler(options) {
139138
return compile
140139

141140
function compile(events) {
141+
var stack = [{type: 'root', children: []}]
142142
var index = -1
143143
var listStack = []
144144
var length
@@ -175,6 +175,7 @@ function compiler(options) {
175175
if (own.call(handler, events[index][1].type)) {
176176
handler[events[index][1].type].call(
177177
{
178+
stack: stack,
178179
handlers: handlers,
179180
enter: enter,
180181
exit: exit,
@@ -335,18 +336,18 @@ function compiler(options) {
335336
return open
336337

337338
function open(token) {
338-
enter(create(token), token)
339+
enter.call(this, create(token), token)
339340
if (and) and.call(this, token)
340341
}
341342
}
342343

343344
function buffer() {
344-
stack.push({type: 'fragment', children: []})
345+
this.stack.push({type: 'fragment', children: []})
345346
}
346347

347348
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)
350351
node.position = {start: point(token.start)}
351352
return node
352353
}
@@ -356,18 +357,18 @@ function compiler(options) {
356357

357358
function close(token) {
358359
if (and) and.call(this, token)
359-
exit(token)
360+
exit.call(this, token)
360361
}
361362
}
362363

363364
function exit(token) {
364-
var node = stack.pop()
365+
var node = this.stack.pop()
365366
node.position.end = point(token.end)
366367
return node
367368
}
368369

369370
function resume() {
370-
var value = toString(stack.pop())
371+
var value = toString(this.stack.pop())
371372
return value
372373
}
373374

@@ -381,7 +382,7 @@ function compiler(options) {
381382

382383
function onenterlistitemvalue(token) {
383384
if (expectingFirstListItemValue) {
384-
stack[stack.length - 2].start = parseInt(
385+
this.stack[this.stack.length - 2].start = parseInt(
385386
this.sliceSerialize(token),
386387
constants.numericBaseDecimal
387388
)
@@ -390,55 +391,60 @@ function compiler(options) {
390391
}
391392

392393
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
395396
}
396397

397398
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
400401
}
401402

402403
function onexitcodefencedfence() {
403404
// Exit if this is the closing fence.
404405
if (flowCodeInside) return
405-
buffer()
406+
this.buffer()
406407
flowCodeInside = true
407408
}
408409

409410
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+
)
412416
flowCodeInside = undefined
413417
}
414418

415419
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
418422
}
419423

420424
function onexitdefinitionlabelstring(token) {
421425
// 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(
425429
this.sliceSerialize(token)
426430
).toLowerCase()
427431
}
428432

429433
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
432436
}
433437

434438
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
437441
}
438442

439443
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
442448
}
443449
}
444450

@@ -447,7 +453,7 @@ function compiler(options) {
447453
}
448454

449455
function onexitsetextheadinglinesequence(token) {
450-
stack[stack.length - 1].depth =
456+
this.stack[this.stack.length - 1].depth =
451457
this.sliceSerialize(token).charCodeAt(0) === codes.equalsTo ? 1 : 2
452458
}
453459

@@ -456,27 +462,27 @@ function compiler(options) {
456462
}
457463

458464
function onenterdata(token) {
459-
var siblings = stack[stack.length - 1].children
465+
var siblings = this.stack[this.stack.length - 1].children
460466
var tail = siblings[siblings.length - 1]
461467

462468
if (!tail || tail.type !== 'text') {
463469
// Add a new text node.
464470
tail = text()
465471
tail.position = {start: point(token.start)}
466-
stack[stack.length - 1].children.push(tail)
472+
this.stack[this.stack.length - 1].children.push(tail)
467473
}
468474

469-
stack.push(tail)
475+
this.stack.push(tail)
470476
}
471477

472478
function onexitdata(token) {
473-
var tail = stack.pop()
479+
var tail = this.stack.pop()
474480
tail.value += this.sliceSerialize(token)
475481
tail.position.end = point(token.end)
476482
}
477483

478484
function onexitlineending(token) {
479-
var context = stack[stack.length - 1]
485+
var context = this.stack[this.stack.length - 1]
480486

481487
// If we’re at a hard break, include the line ending in there.
482488
if (atHardBreak) {
@@ -508,26 +514,26 @@ function compiler(options) {
508514
}
509515

510516
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
513519
}
514520

515521
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
518524
}
519525

520526
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
523529
}
524530

525531
function onenterimage() {
526-
buffer()
532+
this.buffer()
527533
}
528534

529535
function onexitlink() {
530-
var context = stack[stack.length - 1]
536+
var context = this.stack[this.stack.length - 1]
531537

532538
// To do: clean.
533539
if (inReference) {
@@ -545,7 +551,7 @@ function compiler(options) {
545551
}
546552

547553
function onexitimage() {
548-
var context = stack[stack.length - 1]
554+
var context = this.stack[this.stack.length - 1]
549555

550556
// To do: clean.
551557
if (inReference) {
@@ -564,10 +570,10 @@ function compiler(options) {
564570

565571
function onexitlabeltext(token) {
566572
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])
571577
ctx.identifier = normalizeIdentifier(
572578
this.sliceSerialize(token)
573579
).toLowerCase()
@@ -580,20 +586,20 @@ function compiler(options) {
580586
inReference = true
581587

582588
// 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
586592
}
587593
}
588594

589595
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
592598
}
593599

594600
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
597603
}
598604

599605
function onexitresource() {
@@ -605,9 +611,9 @@ function compiler(options) {
605611
}
606612

607613
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(
611617
this.sliceSerialize(token)
612618
).toLowerCase()
613619
referenceType = 'full'
@@ -632,18 +638,19 @@ function compiler(options) {
632638
value = decode(data)
633639
}
634640

635-
stack[stack.length - 1].value += value
641+
this.stack[this.stack.length - 1].value += value
636642
characterReferenceType = undefined
637643
}
638644

639645
function onexitautolinkprotocol(token) {
640646
onexitdata.call(this, token)
641-
stack[stack.length - 1].url = this.sliceSerialize(token)
647+
this.stack[this.stack.length - 1].url = this.sliceSerialize(token)
642648
}
643649

644650
function onexitautolinkemail(token) {
645651
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)
647654
}
648655

649656
//

0 commit comments

Comments
 (0)