@@ -21,6 +21,7 @@ import {visitParents} from 'unist-util-visit-parents'
21
21
import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
22
22
import { containerPhrasing } from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
23
23
import { checkQuote } from 'mdast-util-to-markdown/lib/util/check-quote.js'
24
+ import { track } from 'mdast-util-to-markdown/lib/util/track.js'
24
25
25
26
const own = { } . hasOwnProperty
26
27
@@ -217,51 +218,60 @@ function exit(token) {
217
218
* @type {ToMarkdownHandle }
218
219
* @param {Directive } node
219
220
*/
220
- function handleDirective ( node , _ , context ) {
221
- const prefix = fence ( node )
221
+ function handleDirective ( node , _ , context , safeOptions ) {
222
+ const tracker = track ( safeOptions )
223
+ const sequence = fence ( node )
222
224
const exit = context . enter ( node . type )
223
- let value =
224
- prefix +
225
- ( node . name || '' ) +
226
- label ( node , context ) +
227
- attributes ( node , context )
225
+ let value = tracker . move ( sequence + ( node . name || '' ) )
226
+ /** @type {Directive|Paragraph|undefined } */
227
+ let label = node
228
228
229
229
if ( node . type === 'containerDirective' ) {
230
- const subvalue = content ( node , context )
231
- if ( subvalue ) value += '\n' + subvalue
232
- value += '\n' + prefix
230
+ const head = ( node . children || [ ] ) [ 0 ]
231
+ label = inlineDirectiveLabel ( head ) ? head : undefined
233
232
}
234
233
235
- exit ( )
236
- return value
237
- }
238
-
239
- /** @type {ToMarkdownHandle } */
240
- function peekDirective ( ) {
241
- return ':'
242
- }
234
+ if ( label && label . children && label . children . length > 0 ) {
235
+ const exit = context . enter ( 'label' )
236
+ const subexit = context . enter ( node . type + 'Label' )
237
+ value += tracker . move ( '[' )
238
+ value += tracker . move (
239
+ containerPhrasing ( label , context , {
240
+ ...tracker . current ( ) ,
241
+ before : value ,
242
+ after : ']'
243
+ } )
244
+ )
245
+ value += tracker . move ( ']' )
246
+ subexit ( )
247
+ exit ( )
248
+ }
243
249
244
- /**
245
- * @param {Directive } node
246
- * @param {Context } context
247
- * @returns {string }
248
- */
249
- function label ( node , context ) {
250
- /** @type {Directive|Paragraph } */
251
- let label = node
250
+ value += tracker . move ( attributes ( node , context ) )
252
251
253
252
if ( node . type === 'containerDirective' ) {
254
253
const head = ( node . children || [ ] ) [ 0 ]
255
- if ( ! inlineDirectiveLabel ( head ) ) return ''
256
- label = head
254
+ let shallow = node
255
+
256
+ if ( inlineDirectiveLabel ( head ) ) {
257
+ shallow = Object . assign ( { } , node , { children : node . children . slice ( 1 ) } )
258
+ }
259
+
260
+ if ( shallow && shallow . children && shallow . children . length > 0 ) {
261
+ value += tracker . move ( '\n' )
262
+ value += tracker . move ( containerFlow ( shallow , context , tracker . current ( ) ) )
263
+ }
264
+
265
+ value += tracker . move ( '\n' + sequence )
257
266
}
258
267
259
- const exit = context . enter ( 'label' )
260
- const subexit = context . enter ( node . type + 'Label' )
261
- const value = containerPhrasing ( label , context , { before : '[' , after : ']' } )
262
- subexit ( )
263
268
exit ( )
264
- return value ? '[' + value + ']' : ''
269
+ return value
270
+ }
271
+
272
+ /** @type {ToMarkdownHandle } */
273
+ function peekDirective ( ) {
274
+ return ':'
265
275
}
266
276
267
277
/**
@@ -348,21 +358,6 @@ function attributes(node, context) {
348
358
}
349
359
}
350
360
351
- /**
352
- * @param {ContainerDirective } node
353
- * @param {Context } context
354
- * @returns {string }
355
- */
356
- function content ( node , context ) {
357
- const head = ( node . children || [ ] ) [ 0 ]
358
-
359
- if ( inlineDirectiveLabel ( head ) ) {
360
- node = Object . assign ( { } , node , { children : node . children . slice ( 1 ) } )
361
- }
362
-
363
- return containerFlow ( node , context )
364
- }
365
-
366
361
/**
367
362
* @param {BlockContent } node
368
363
* @returns {node is Paragraph & {data: {directiveLabel: boolean}} }
0 commit comments