@@ -18,142 +18,106 @@ import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
18
18
footnoteReference . peek = footnoteReferencePeek
19
19
20
20
/**
21
- * Create an extension for `mdast-util-from-markdown` to enable GFM footnotes
22
- * in markdown.
23
- *
24
- * @returns {FromMarkdownExtension }
25
- * Extension for `mdast-util-from-markdown`.
21
+ * @this {CompileContext}
22
+ * @type {FromMarkdownHandle }
26
23
*/
27
- export function gfmFootnoteFromMarkdown ( ) {
28
- return {
29
- enter : {
30
- gfmFootnoteDefinition : enterFootnoteDefinition ,
31
- gfmFootnoteDefinitionLabelString : enterFootnoteDefinitionLabelString ,
32
- gfmFootnoteCall : enterFootnoteCall ,
33
- gfmFootnoteCallString : enterFootnoteCallString
34
- } ,
35
- exit : {
36
- gfmFootnoteDefinition : exitFootnoteDefinition ,
37
- gfmFootnoteDefinitionLabelString : exitFootnoteDefinitionLabelString ,
38
- gfmFootnoteCall : exitFootnoteCall ,
39
- gfmFootnoteCallString : exitFootnoteCallString
40
- }
41
- }
24
+ function enterFootnoteCallString ( ) {
25
+ this . buffer ( )
42
26
}
43
27
44
28
/**
45
- * Create an extension for `mdast-util-to-markdown` to enable GFM footnotes
46
- * in markdown.
47
- *
48
- * @returns {ToMarkdownExtension }
49
- * Extension for `mdast-util-to-markdown`.
29
+ * @this {CompileContext}
30
+ * @type {FromMarkdownHandle }
50
31
*/
51
- export function gfmFootnoteToMarkdown ( ) {
52
- return {
53
- // This is on by default already.
54
- unsafe : [ { character : '[' , inConstruct : [ 'phrasing' , 'label' , 'reference' ] } ] ,
55
- handlers : { footnoteDefinition, footnoteReference}
56
- }
32
+ function enterFootnoteCall ( token ) {
33
+ this . enter ( { type : 'footnoteReference' , identifier : '' , label : '' } , token )
57
34
}
58
35
59
36
/**
60
37
* @this {CompileContext}
61
38
* @type {FromMarkdownHandle }
62
39
*/
63
- function enterFootnoteDefinition ( token ) {
64
- this . enter (
65
- { type : 'footnoteDefinition' , identifier : '' , label : '' , children : [ ] } ,
66
- token
67
- )
40
+ function enterFootnoteDefinitionLabelString ( ) {
41
+ this . buffer ( )
68
42
}
69
43
70
44
/**
71
45
* @this {CompileContext}
72
46
* @type {FromMarkdownHandle }
73
47
*/
74
- function enterFootnoteDefinitionLabelString ( ) {
75
- this . buffer ( )
48
+ function enterFootnoteDefinition ( token ) {
49
+ this . enter (
50
+ { type : 'footnoteDefinition' , identifier : '' , label : '' , children : [ ] } ,
51
+ token
52
+ )
76
53
}
77
54
78
55
/**
79
56
* @this {CompileContext}
80
57
* @type {FromMarkdownHandle }
81
58
*/
82
- function exitFootnoteDefinitionLabelString ( token ) {
59
+ function exitFootnoteCallString ( token ) {
83
60
const label = this . resume ( )
84
61
const node = this . stack [ this . stack . length - 1 ]
85
- assert ( node . type === 'footnoteDefinition' )
86
- node . label = label
62
+ assert ( node . type === 'footnoteReference' )
87
63
node . identifier = normalizeIdentifier (
88
64
this . sliceSerialize ( token )
89
65
) . toLowerCase ( )
66
+ node . label = label
90
67
}
91
68
92
69
/**
93
70
* @this {CompileContext}
94
71
* @type {FromMarkdownHandle }
95
72
*/
96
- function exitFootnoteDefinition ( token ) {
73
+ function exitFootnoteCall ( token ) {
97
74
this . exit ( token )
98
75
}
99
76
100
77
/**
101
78
* @this {CompileContext}
102
79
* @type {FromMarkdownHandle }
103
80
*/
104
- function enterFootnoteCall ( token ) {
105
- this . enter ( { type : 'footnoteReference' , identifier : '' , label : '' } , token )
106
- }
107
-
108
- /**
109
- * @this {CompileContext}
110
- * @type {FromMarkdownHandle }
111
- */
112
- function enterFootnoteCallString ( ) {
113
- this . buffer ( )
114
- }
115
-
116
- /**
117
- * @this {CompileContext}
118
- * @type {FromMarkdownHandle }
119
- */
120
- function exitFootnoteCallString ( token ) {
81
+ function exitFootnoteDefinitionLabelString ( token ) {
121
82
const label = this . resume ( )
122
83
const node = this . stack [ this . stack . length - 1 ]
123
- assert ( node . type === 'footnoteReference' )
124
- node . label = label
84
+ assert ( node . type === 'footnoteDefinition' )
125
85
node . identifier = normalizeIdentifier (
126
86
this . sliceSerialize ( token )
127
87
) . toLowerCase ( )
88
+ node . label = label
128
89
}
129
90
130
91
/**
131
92
* @this {CompileContext}
132
93
* @type {FromMarkdownHandle }
133
94
*/
134
- function exitFootnoteCall ( token ) {
95
+ function exitFootnoteDefinition ( token ) {
135
96
this . exit ( token )
136
97
}
137
98
138
99
/**
139
100
* @type {ToMarkdownHandle }
140
- * @param {FootnoteReference } node
101
+ * @param {FootnoteDefinition } node
141
102
*/
142
- function footnoteReference ( node , _ , state , info ) {
103
+ function footnoteDefinition ( node , _ , state , info ) {
143
104
const tracker = state . createTracker ( info )
144
105
let value = tracker . move ( '[^' )
145
- const exit = state . enter ( 'footnoteReference ' )
146
- const subexit = state . enter ( 'reference ' )
106
+ const exit = state . enter ( 'footnoteDefinition ' )
107
+ const subexit = state . enter ( 'label ' )
147
108
value += tracker . move (
148
- state . safe ( state . associationId ( node ) , {
149
- ...tracker . current ( ) ,
150
- before : value ,
151
- after : ']'
152
- } )
109
+ state . safe ( state . associationId ( node ) , { before : value , after : ']' } )
153
110
)
154
111
subexit ( )
112
+ value += tracker . move (
113
+ ']:' + ( node . children && node . children . length > 0 ? ' ' : '' )
114
+ )
115
+ tracker . shift ( 4 )
116
+ value += tracker . move (
117
+ state . indentLines ( state . containerFlow ( node , tracker . current ( ) ) , map )
118
+ )
155
119
exit ( )
156
- value += tracker . move ( ']' )
120
+
157
121
return value
158
122
}
159
123
@@ -164,33 +128,61 @@ function footnoteReferencePeek() {
164
128
165
129
/**
166
130
* @type {ToMarkdownHandle }
167
- * @param {FootnoteDefinition } node
131
+ * @param {FootnoteReference } node
168
132
*/
169
- function footnoteDefinition ( node , _ , state , info ) {
133
+ function footnoteReference ( node , _ , state , info ) {
170
134
const tracker = state . createTracker ( info )
171
135
let value = tracker . move ( '[^' )
172
- const exit = state . enter ( 'footnoteDefinition ' )
173
- const subexit = state . enter ( 'label ' )
136
+ const exit = state . enter ( 'footnoteReference ' )
137
+ const subexit = state . enter ( 'reference ' )
174
138
value += tracker . move (
175
- state . safe ( state . associationId ( node ) , {
176
- ...tracker . current ( ) ,
177
- before : value ,
178
- after : ']'
179
- } )
139
+ state . safe ( state . associationId ( node ) , { after : ']' , before : value } )
180
140
)
181
141
subexit ( )
182
- value += tracker . move (
183
- ']:' + ( node . children && node . children . length > 0 ? ' ' : '' )
184
- )
185
- tracker . shift ( 4 )
186
- value += tracker . move (
187
- state . indentLines ( state . containerFlow ( node , tracker . current ( ) ) , map )
188
- )
189
142
exit ( )
190
-
143
+ value += tracker . move ( ']' )
191
144
return value
192
145
}
193
146
147
+ /**
148
+ * Create an extension for `mdast-util-from-markdown` to enable GFM footnotes
149
+ * in markdown.
150
+ *
151
+ * @returns {FromMarkdownExtension }
152
+ * Extension for `mdast-util-from-markdown`.
153
+ */
154
+ export function gfmFootnoteFromMarkdown ( ) {
155
+ return {
156
+ enter : {
157
+ gfmFootnoteCallString : enterFootnoteCallString ,
158
+ gfmFootnoteCall : enterFootnoteCall ,
159
+ gfmFootnoteDefinitionLabelString : enterFootnoteDefinitionLabelString ,
160
+ gfmFootnoteDefinition : enterFootnoteDefinition
161
+ } ,
162
+ exit : {
163
+ gfmFootnoteCallString : exitFootnoteCallString ,
164
+ gfmFootnoteCall : exitFootnoteCall ,
165
+ gfmFootnoteDefinitionLabelString : exitFootnoteDefinitionLabelString ,
166
+ gfmFootnoteDefinition : exitFootnoteDefinition
167
+ }
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Create an extension for `mdast-util-to-markdown` to enable GFM footnotes
173
+ * in markdown.
174
+ *
175
+ * @returns {ToMarkdownExtension }
176
+ * Extension for `mdast-util-to-markdown`.
177
+ */
178
+ export function gfmFootnoteToMarkdown ( ) {
179
+ return {
180
+ handlers : { footnoteDefinition, footnoteReference} ,
181
+ // This is on by default already.
182
+ unsafe : [ { character : '[' , inConstruct : [ 'label' , 'phrasing' , 'reference' ] } ]
183
+ }
184
+ }
185
+
194
186
/** @type {Map } */
195
187
function map ( line , index , blank ) {
196
188
if ( index === 0 ) {
0 commit comments