109
109
* Stack of nodes.
110
110
* @property {Array<TokenTuple> } tokenStack
111
111
* Stack of tokens.
112
- * @property {<Key extends keyof CompileData>(key: Key) => CompileData[Key] } getData
113
- * Get data from the key/value store.
114
- * @property {<Key extends keyof CompileData>(key: Key, value?: CompileData[Key]) => undefined } setData
115
- * Set data into the key/value store.
116
112
* @property {(this: CompileContext) => undefined } buffer
117
113
* Capture some of the output data.
118
114
* @property {(this: CompileContext) => string } resume
119
115
* Stop capturing and access the output data.
120
116
* @property {(this: CompileContext, node: Nodes, token: Token, onError?: OnEnterError) => undefined } enter
121
- * Enter a token .
117
+ * Enter a node .
122
118
* @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined } exit
123
- * Exit a token .
119
+ * Exit a node .
124
120
* @property {TokenizeContext['sliceSerialize'] } sliceSerialize
125
121
* Get the string value of a token.
126
122
* @property {Config } config
127
123
* Configuration.
124
+ * @property {CompileData } data
125
+ * Info passed around; key/value store.
128
126
*
129
127
* @typedef FromMarkdownOptions
130
128
* Configuration for how to build mdast.
135
133
* Configuration.
136
134
*/
137
135
138
- // To do: next major: remove setter/getter.
139
-
140
136
import { ok as assert } from 'devlop'
141
137
import { toString } from 'mdast-util-to-string'
142
138
import { parse , postprocess , preprocess } from 'micromark'
@@ -317,8 +313,7 @@ function compiler(options) {
317
313
exit,
318
314
buffer,
319
315
resume,
320
- setData,
321
- getData
316
+ data
322
317
}
323
318
/** @type {Array<number> } */
324
319
const listStack = [ ]
@@ -538,36 +533,6 @@ function compiler(options) {
538
533
return length
539
534
}
540
535
541
- /**
542
- * Set data.
543
- *
544
- * @template {keyof CompileData} Key
545
- * Field type.
546
- * @param {Key } key
547
- * Key of field.
548
- * @param {CompileData[Key] | undefined } [value]
549
- * New value.
550
- * @returns {undefined }
551
- * Nothing.
552
- */
553
- function setData ( key , value ) {
554
- data [ key ] = value
555
- }
556
-
557
- /**
558
- * Get data.
559
- *
560
- * @template {keyof CompileData} Key
561
- * Field type.
562
- * @param {Key } key
563
- * Key of field.
564
- * @returns {CompileData[Key] }
565
- * Value.
566
- */
567
- function getData ( key ) {
568
- return data [ key ]
569
- }
570
-
571
536
/**
572
537
* Create an opener handle.
573
538
*
@@ -704,23 +669,23 @@ function compiler(options) {
704
669
* @type {Handle }
705
670
*/
706
671
function onenterlistordered ( ) {
707
- setData ( ' expectingFirstListItemValue' , true )
672
+ this . data . expectingFirstListItemValue = true
708
673
}
709
674
710
675
/**
711
676
* @this {CompileContext}
712
677
* @type {Handle }
713
678
*/
714
679
function onenterlistitemvalue ( token ) {
715
- if ( getData ( ' expectingFirstListItemValue' ) ) {
680
+ if ( this . data . expectingFirstListItemValue ) {
716
681
const ancestor = this . stack [ this . stack . length - 2 ]
717
682
assert ( ancestor , 'expected nodes on stack' )
718
683
assert ( ancestor . type === 'list' , 'expected list on stack' )
719
684
ancestor . start = Number . parseInt (
720
685
this . sliceSerialize ( token ) ,
721
686
constants . numericBaseDecimal
722
687
)
723
- setData ( ' expectingFirstListItemValue' )
688
+ this . data . expectingFirstListItemValue = undefined
724
689
}
725
690
}
726
691
@@ -754,9 +719,9 @@ function compiler(options) {
754
719
*/
755
720
function onexitcodefencedfence ( ) {
756
721
// Exit if this is the closing fence.
757
- if ( getData ( ' flowCodeInside' ) ) return
722
+ if ( this . data . flowCodeInside ) return
758
723
this . buffer ( )
759
- setData ( ' flowCodeInside' , true )
724
+ this . data . flowCodeInside = true
760
725
}
761
726
762
727
/**
@@ -770,7 +735,7 @@ function compiler(options) {
770
735
assert ( node . type === 'code' , 'expected code on stack' )
771
736
772
737
node . value = data . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
773
- setData ( ' flowCodeInside' )
738
+ this . data . flowCodeInside = undefined
774
739
}
775
740
776
741
/**
@@ -859,7 +824,7 @@ function compiler(options) {
859
824
* @type {Handle }
860
825
*/
861
826
function onexitsetextheadingtext ( ) {
862
- setData ( ' setextHeadingSlurpLineEnding' , true )
827
+ this . data . setextHeadingSlurpLineEnding = true
863
828
}
864
829
865
830
/**
@@ -880,7 +845,7 @@ function compiler(options) {
880
845
* @type {Handle }
881
846
*/
882
847
function onexitsetextheading ( ) {
883
- setData ( ' setextHeadingSlurpLineEnding' )
848
+ this . data . setextHeadingSlurpLineEnding = undefined
884
849
}
885
850
886
851
/**
@@ -935,17 +900,17 @@ function compiler(options) {
935
900
assert ( context , 'expected `node`' )
936
901
937
902
// If we’re at a hard break, include the line ending in there.
938
- if ( getData ( ' atHardBreak' ) ) {
903
+ if ( this . data . atHardBreak ) {
939
904
assert ( 'children' in context , 'expected `parent`' )
940
905
const tail = context . children [ context . children . length - 1 ]
941
906
assert ( tail . position , 'expected tail to have a starting position' )
942
907
tail . position . end = point ( token . end )
943
- setData ( ' atHardBreak' )
908
+ this . data . atHardBreak = undefined
944
909
return
945
910
}
946
911
947
912
if (
948
- ! getData ( ' setextHeadingSlurpLineEnding' ) &&
913
+ ! this . data . setextHeadingSlurpLineEnding &&
949
914
config . canContainEols . includes ( context . type )
950
915
) {
951
916
onenterdata . call ( this , token )
@@ -959,7 +924,7 @@ function compiler(options) {
959
924
*/
960
925
961
926
function onexithardbreak ( ) {
962
- setData ( ' atHardBreak' , true )
927
+ this . data . atHardBreak = true
963
928
}
964
929
965
930
/**
@@ -1018,9 +983,9 @@ function compiler(options) {
1018
983
// These are used / cleaned here.
1019
984
1020
985
// To do: clean.
1021
- if ( getData ( ' inReference' ) ) {
986
+ if ( this . data . inReference ) {
1022
987
/** @type {ReferenceType } */
1023
- const referenceType = getData ( ' referenceType' ) || 'shortcut'
988
+ const referenceType = this . data . referenceType || 'shortcut'
1024
989
1025
990
node . type += 'Reference'
1026
991
// @ts -expect-error: mutate.
@@ -1035,7 +1000,7 @@ function compiler(options) {
1035
1000
delete node . label
1036
1001
}
1037
1002
1038
- setData ( ' referenceType' )
1003
+ this . data . referenceType = undefined
1039
1004
}
1040
1005
1041
1006
/**
@@ -1052,9 +1017,9 @@ function compiler(options) {
1052
1017
// These are used / cleaned here.
1053
1018
1054
1019
// To do: clean.
1055
- if ( getData ( ' inReference' ) ) {
1020
+ if ( this . data . inReference ) {
1056
1021
/** @type {ReferenceType } */
1057
- const referenceType = getData ( ' referenceType' ) || 'shortcut'
1022
+ const referenceType = this . data . referenceType || 'shortcut'
1058
1023
1059
1024
node . type += 'Reference'
1060
1025
// @ts -expect-error: mutate.
@@ -1069,7 +1034,7 @@ function compiler(options) {
1069
1034
delete node . label
1070
1035
}
1071
1036
1072
- setData ( ' referenceType' )
1037
+ this . data . referenceType = undefined
1073
1038
}
1074
1039
1075
1040
/**
@@ -1111,7 +1076,7 @@ function compiler(options) {
1111
1076
)
1112
1077
1113
1078
// Assume a reference.
1114
- setData ( ' inReference' , true )
1079
+ this . data . inReference = true
1115
1080
1116
1081
if ( node . type === 'link' ) {
1117
1082
/** @type {Array<PhrasingContent> } */
@@ -1161,7 +1126,7 @@ function compiler(options) {
1161
1126
*/
1162
1127
1163
1128
function onexitresource ( ) {
1164
- setData ( ' inReference' )
1129
+ this . data . inReference = undefined
1165
1130
}
1166
1131
1167
1132
/**
@@ -1170,7 +1135,7 @@ function compiler(options) {
1170
1135
*/
1171
1136
1172
1137
function onenterreference ( ) {
1173
- setData ( ' referenceType' , 'collapsed' )
1138
+ this . data . referenceType = 'collapsed'
1174
1139
}
1175
1140
1176
1141
/**
@@ -1194,7 +1159,7 @@ function compiler(options) {
1194
1159
node . identifier = normalizeIdentifier (
1195
1160
this . sliceSerialize ( token )
1196
1161
) . toLowerCase ( )
1197
- setData ( ' referenceType' , 'full' )
1162
+ this . data . referenceType = 'full'
1198
1163
}
1199
1164
1200
1165
/**
@@ -1207,7 +1172,7 @@ function compiler(options) {
1207
1172
token . type === 'characterReferenceMarkerNumeric' ||
1208
1173
token . type === 'characterReferenceMarkerHexadecimal'
1209
1174
)
1210
- setData ( ' characterReferenceType' , token . type )
1175
+ this . data . characterReferenceType = token . type
1211
1176
}
1212
1177
1213
1178
/**
@@ -1216,7 +1181,7 @@ function compiler(options) {
1216
1181
*/
1217
1182
function onexitcharacterreferencevalue ( token ) {
1218
1183
const data = this . sliceSerialize ( token )
1219
- const type = getData ( ' characterReferenceType' )
1184
+ const type = this . data . characterReferenceType
1220
1185
/** @type {string } */
1221
1186
let value
1222
1187
@@ -1227,7 +1192,7 @@ function compiler(options) {
1227
1192
? constants . numericBaseDecimal
1228
1193
: constants . numericBaseHexadecimal
1229
1194
)
1230
- setData ( ' characterReferenceType' )
1195
+ this . data . characterReferenceType = undefined
1231
1196
} else {
1232
1197
const result = decodeNamedCharacterReference ( data )
1233
1198
assert ( result !== false , 'expected reference to decode' )
0 commit comments