21
21
import { parseEntities } from 'parse-entities'
22
22
import { stringifyEntitiesLight } from 'stringify-entities'
23
23
import { visitParents } from 'unist-util-visit-parents'
24
- import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
25
- import { containerPhrasing } from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
26
- import { checkQuote } from 'mdast-util-to-markdown/lib/util/check-quote.js'
27
- import { track } from 'mdast-util-to-markdown/lib/util/track.js'
28
24
29
- // To do: next major: replace `containerFlow`, `containerPhrasing`, `track`
30
- // with `state` methods.
31
25
// To do: next major: expose functions.
32
26
33
27
const own = { } . hasOwnProperty
@@ -178,7 +172,7 @@ function exitContainerLabel(token) {
178
172
* @type {FromMarkdownHandle }
179
173
*/
180
174
function enterAttributes ( ) {
181
- this . setData ( ' directiveAttributes' , [ ] )
175
+ this . data . directiveAttributes = [ ]
182
176
this . buffer ( ) // Capture EOLs
183
177
}
184
178
@@ -188,7 +182,7 @@ function enterAttributes() {
188
182
*/
189
183
function exitAttributeIdValue ( token ) {
190
184
const list = /** @type {Array<[string, string]> } */ (
191
- this . getData ( ' directiveAttributes' )
185
+ this . data . directiveAttributes
192
186
)
193
187
list . push ( [
194
188
'id' ,
@@ -204,7 +198,7 @@ function exitAttributeIdValue(token) {
204
198
*/
205
199
function exitAttributeClassValue ( token ) {
206
200
const list = /** @type {Array<[string, string]> } */ (
207
- this . getData ( ' directiveAttributes' )
201
+ this . data . directiveAttributes
208
202
)
209
203
list . push ( [
210
204
'class' ,
@@ -220,7 +214,7 @@ function exitAttributeClassValue(token) {
220
214
*/
221
215
function exitAttributeValue ( token ) {
222
216
const list = /** @type {Array<[string, string]> } */ (
223
- this . getData ( ' directiveAttributes' )
217
+ this . data . directiveAttributes
224
218
)
225
219
list [ list . length - 1 ] [ 1 ] = parseEntities ( this . sliceSerialize ( token ) , {
226
220
attribute : true
@@ -233,7 +227,7 @@ function exitAttributeValue(token) {
233
227
*/
234
228
function exitAttributeName ( token ) {
235
229
const list = /** @type {Array<[string, string]> } */ (
236
- this . getData ( ' directiveAttributes' )
230
+ this . data . directiveAttributes
237
231
)
238
232
239
233
// Attribute names in CommonMark are significantly limited, so character
@@ -247,7 +241,7 @@ function exitAttributeName(token) {
247
241
*/
248
242
function exitAttributes ( ) {
249
243
const list = /** @type {Array<[string, string]> } */ (
250
- this . getData ( ' directiveAttributes' )
244
+ this . data . directiveAttributes
251
245
)
252
246
/** @type {Record<string, string> } */
253
247
const cleaned = { }
@@ -263,7 +257,7 @@ function exitAttributes() {
263
257
}
264
258
}
265
259
266
- this . setData ( ' directiveAttributes' )
260
+ this . data . directiveAttributes = undefined
267
261
this . resume ( ) // Drop EOLs
268
262
const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
269
263
node . attributes = cleaned
@@ -282,7 +276,7 @@ function exit(token) {
282
276
* @param {Directive } node
283
277
*/
284
278
function handleDirective ( node , _ , state , safeOptions ) {
285
- const tracker = track ( safeOptions )
279
+ const tracker = state . createTracker ( safeOptions )
286
280
const sequence = fence ( node )
287
281
const exit = state . enter ( node . type )
288
282
let value = tracker . move ( sequence + ( node . name || '' ) )
@@ -303,7 +297,11 @@ function handleDirective(node, _, state, safeOptions) {
303
297
const subexit = state . enter ( labelType )
304
298
value += tracker . move ( '[' )
305
299
value += tracker . move (
306
- containerPhrasing ( label , state , {
300
+ // @ts -expect-error: `containerPhrasing` is typed correctly, but TS
301
+ // generates *hardcoded* types, which means that our dynamically added
302
+ // directives are not present.
303
+ // At some point, TS should fix that, and `from-markdown` should be fine.
304
+ state . containerPhrasing ( label , {
307
305
...tracker . current ( ) ,
308
306
before : value ,
309
307
after : ']'
@@ -326,7 +324,7 @@ function handleDirective(node, _, state, safeOptions) {
326
324
327
325
if ( shallow && shallow . children && shallow . children . length > 0 ) {
328
326
value += tracker . move ( '\n' )
329
- value += tracker . move ( containerFlow ( shallow , state , tracker . current ( ) ) )
327
+ value += tracker . move ( state . containerFlow ( shallow , tracker . current ( ) ) )
330
328
}
331
329
332
330
value += tracker . move ( '\n' + sequence )
@@ -347,7 +345,7 @@ function peekDirective() {
347
345
* @returns {string }
348
346
*/
349
347
function attributes ( node , state ) {
350
- const quote = checkQuote ( state )
348
+ const quote = state . options . quote || '"'
351
349
const subset = node . type === 'textDirective' ? [ quote ] : [ quote , '\n' , '\r' ]
352
350
const attrs = node . attributes || { }
353
351
/** @type {Array<string> } */
@@ -427,7 +425,7 @@ function attributes(node, state) {
427
425
428
426
/**
429
427
* @param {BlockContent | DefinitionContent } node
430
- * @returns {node is Paragraph & {data: {directiveLabel: boolean }} }
428
+ * @returns {node is Paragraph & {data: {directiveLabel: true }} }
431
429
*/
432
430
function inlineDirectiveLabel ( node ) {
433
431
return Boolean (
0 commit comments