Skip to content

Commit 1d5a9f0

Browse files
committed
Add better interface for data
1 parent e24c09d commit 1d5a9f0

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

index.js

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,7 @@ function compiler(options) {
139139
settings.mdastExtensions || []
140140
)
141141

142-
var flowCodeInside
143-
var setextHeadingSlurpLineEnding
144-
var characterReferenceType
145-
var expectingFirstListItemValue
146-
var atHardBreak
147-
var inReference
148-
var referenceType
142+
var data = {}
149143

150144
return compile
151145

@@ -193,7 +187,9 @@ function compiler(options) {
193187
exit: exit,
194188
buffer: buffer,
195189
resume: resume,
196-
sliceSerialize: events[index][2].sliceSerialize
190+
sliceSerialize: events[index][2].sliceSerialize,
191+
setData: setData,
192+
getData: getData
197193
},
198194
events[index][1]
199195
)
@@ -340,6 +336,14 @@ function compiler(options) {
340336
return length
341337
}
342338

339+
function setData(key, value) {
340+
data[key] = value
341+
}
342+
343+
function getData(key) {
344+
return data[key]
345+
}
346+
343347
function point(d) {
344348
return {line: d.line, column: d.column, offset: d.offset}
345349
}
@@ -389,16 +393,16 @@ function compiler(options) {
389393
//
390394

391395
function onenterlistordered() {
392-
expectingFirstListItemValue = true
396+
setData('expectingFirstListItemValue', true)
393397
}
394398

395399
function onenterlistitemvalue(token) {
396-
if (expectingFirstListItemValue) {
400+
if (getData('expectingFirstListItemValue')) {
397401
this.stack[this.stack.length - 2].start = parseInt(
398402
this.sliceSerialize(token),
399403
constants.numericBaseDecimal
400404
)
401-
expectingFirstListItemValue = undefined
405+
setData('expectingFirstListItemValue')
402406
}
403407
}
404408

@@ -414,9 +418,9 @@ function compiler(options) {
414418

415419
function onexitcodefencedfence() {
416420
// Exit if this is the closing fence.
417-
if (flowCodeInside) return
421+
if (getData('flowCodeInside')) return
418422
this.buffer()
419-
flowCodeInside = true
423+
setData('flowCodeInside', true)
420424
}
421425

422426
function onexitcodefenced() {
@@ -425,7 +429,7 @@ function compiler(options) {
425429
/^(\r?\n|\r)|(\r?\n|\r)$/g,
426430
''
427431
)
428-
flowCodeInside = undefined
432+
setData('flowCodeInside')
429433
}
430434

431435
function onexitcodeindented() {
@@ -461,7 +465,7 @@ function compiler(options) {
461465
}
462466

463467
function onexitsetextheadingtext() {
464-
setextHeadingSlurpLineEnding = true
468+
setData('setextHeadingSlurpLineEnding', true)
465469
}
466470

467471
function onexitsetextheadinglinesequence(token) {
@@ -470,7 +474,7 @@ function compiler(options) {
470474
}
471475

472476
function onexitsetextheading() {
473-
setextHeadingSlurpLineEnding = undefined
477+
setData('setextHeadingSlurpLineEnding')
474478
}
475479

476480
function onenterdata(token) {
@@ -497,15 +501,15 @@ function compiler(options) {
497501
var context = this.stack[this.stack.length - 1]
498502

499503
// If we’re at a hard break, include the line ending in there.
500-
if (atHardBreak) {
504+
if (getData('atHardBreak')) {
501505
context.children[context.children.length - 1].position.end = point(
502506
token.end
503507
)
504-
atHardBreak = undefined
508+
setData('atHardBreak')
505509
return
506510
}
507511

508-
if (setextHeadingSlurpLineEnding) {
512+
if (getData('setextHeadingSlurpLineEnding')) {
509513
return
510514
}
511515

@@ -516,7 +520,7 @@ function compiler(options) {
516520
}
517521

518522
function onexithardbreak() {
519-
atHardBreak = true
523+
setData('atHardBreak', true)
520524
}
521525

522526
function onexithtmlflow() {
@@ -542,9 +546,9 @@ function compiler(options) {
542546
var context = this.stack[this.stack.length - 1]
543547

544548
// To do: clean.
545-
if (inReference) {
549+
if (getData('inReference')) {
546550
context.type += 'Reference'
547-
context.referenceType = referenceType || 'shortcut'
551+
context.referenceType = getData('referenceType') || 'shortcut'
548552
delete context.url
549553
delete context.title
550554
} else {
@@ -553,16 +557,16 @@ function compiler(options) {
553557
delete context.referenceType
554558
}
555559

556-
referenceType = undefined
560+
setData('referenceType')
557561
}
558562

559563
function onexitimage() {
560564
var context = this.stack[this.stack.length - 1]
561565

562566
// To do: clean.
563-
if (inReference) {
567+
if (getData('inReference')) {
564568
context.type += 'Reference'
565-
context.referenceType = referenceType || 'shortcut'
569+
context.referenceType = getData('referenceType') || 'shortcut'
566570
delete context.url
567571
delete context.title
568572
} else {
@@ -571,7 +575,7 @@ function compiler(options) {
571575
delete context.referenceType
572576
}
573577

574-
referenceType = undefined
578+
setData('referenceType')
575579
}
576580

577581
function onexitlabeltext(token) {
@@ -589,7 +593,7 @@ function compiler(options) {
589593
var data
590594

591595
// Assume a reference.
592-
inReference = true
596+
setData('inReference', true)
593597

594598
// If we’re in a fragment, we’re in an image and buffering.
595599
if (this.stack[this.stack.length - 1].type === 'fragment') {
@@ -609,11 +613,11 @@ function compiler(options) {
609613
}
610614

611615
function onexitresource() {
612-
inReference = undefined
616+
setData('inReference')
613617
}
614618

615619
function onenterreference() {
616-
referenceType = 'collapsed'
620+
setData('referenceType', 'collapsed')
617621
}
618622

619623
function onexitreferencestring(token) {
@@ -622,30 +626,31 @@ function compiler(options) {
622626
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
623627
this.sliceSerialize(token)
624628
).toLowerCase()
625-
referenceType = 'full'
629+
setData('referenceType', 'full')
626630
}
627631

628632
function onexitcharacterreferencemarker(token) {
629-
characterReferenceType = token.type
633+
setData('characterReferenceType', token.type)
630634
}
631635

632636
function onexitcharacterreferencevalue(token) {
633637
var data = this.sliceSerialize(token)
638+
var type = getData('characterReferenceType')
634639
var value
635640

636-
if (characterReferenceType) {
641+
if (type) {
637642
value = safeFromInt(
638643
data,
639-
characterReferenceType === types.characterReferenceMarkerNumeric
644+
type === types.characterReferenceMarkerNumeric
640645
? constants.numericBaseDecimal
641646
: constants.numericBaseHexadecimal
642647
)
648+
setData('characterReferenceType')
643649
} else {
644650
value = decode(data)
645651
}
646652

647653
this.stack[this.stack.length - 1].value += value
648-
characterReferenceType = undefined
649654
}
650655

651656
function onexitautolinkprotocol(token) {

0 commit comments

Comments
 (0)