1
1
/**
2
2
* @typedef {import('mdast').BlockContent } BlockContent
3
- * @typedef {import('mdast').Root } Root
4
3
* @typedef {import('mdast').Paragraph } Paragraph
5
- * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
6
- * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
4
+ *
7
5
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
6
+ * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
7
+ * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
8
8
* @typedef {import('mdast-util-from-markdown').Token } Token
9
+ *
9
10
* @typedef {import('mdast-util-to-markdown').ConstructName } ConstructName
10
- * @typedef {import('mdast-util-to-markdown/lib/types.js').Handle } ToMarkdownHandle
11
+ * @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
12
+ * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
11
13
* @typedef {import('mdast-util-to-markdown').State } State
12
- * @typedef {import('mdast-util-to-markdown/lib/types.js').Options } ToMarkdownExtension
13
- * @typedef {import('../complex-types.js').ContainerDirective } ContainerDirective
14
- * @typedef {import('../complex-types.js').LeafDirective } LeafDirective
15
- * @typedef {import('../complex-types.js').TextDirective } TextDirective
16
- * @typedef {ContainerDirective|LeafDirective|TextDirective } Directive
14
+ *
15
+ * @typedef {import('../index.js').LeafDirective } LeafDirective
16
+ * @typedef {import('../index.js').TextDirective } TextDirective
17
+ * @typedef {import('../index.js').Directive } Directive
17
18
*/
18
19
19
20
import { parseEntities } from 'parse-entities'
@@ -24,6 +25,9 @@ import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phras
24
25
import { checkQuote } from 'mdast-util-to-markdown/lib/util/check-quote.js'
25
26
import { track } from 'mdast-util-to-markdown/lib/util/track.js'
26
27
28
+ // To do: next major: replace `containerFlow`, `containerPhrasing`, `track`
29
+ // with `state` methods.
30
+
27
31
const own = { } . hasOwnProperty
28
32
29
33
const shortcut = / ^ [ ^ \t \n \r " # ' . < = > ` } ] + $ /
@@ -173,7 +177,7 @@ function enterAttributes() {
173
177
* @type {FromMarkdownHandle }
174
178
*/
175
179
function exitAttributeIdValue ( token ) {
176
- const list = /** @type {Array. <[string, string]> } */ (
180
+ const list = /** @type {Array<[string, string]> } */ (
177
181
this . getData ( 'directiveAttributes' )
178
182
)
179
183
list . push ( [
@@ -189,7 +193,7 @@ function exitAttributeIdValue(token) {
189
193
* @type {FromMarkdownHandle }
190
194
*/
191
195
function exitAttributeClassValue ( token ) {
192
- const list = /** @type {Array. <[string, string]> } */ (
196
+ const list = /** @type {Array<[string, string]> } */ (
193
197
this . getData ( 'directiveAttributes' )
194
198
)
195
199
list . push ( [
@@ -205,7 +209,7 @@ function exitAttributeClassValue(token) {
205
209
* @type {FromMarkdownHandle }
206
210
*/
207
211
function exitAttributeValue ( token ) {
208
- const list = /** @type {Array. <[string, string]> } */ (
212
+ const list = /** @type {Array<[string, string]> } */ (
209
213
this . getData ( 'directiveAttributes' )
210
214
)
211
215
list [ list . length - 1 ] [ 1 ] = parseEntities ( this . sliceSerialize ( token ) , {
@@ -218,7 +222,7 @@ function exitAttributeValue(token) {
218
222
* @type {FromMarkdownHandle }
219
223
*/
220
224
function exitAttributeName ( token ) {
221
- const list = /** @type {Array. <[string, string]> } */ (
225
+ const list = /** @type {Array<[string, string]> } */ (
222
226
this . getData ( 'directiveAttributes' )
223
227
)
224
228
@@ -232,10 +236,10 @@ function exitAttributeName(token) {
232
236
* @type {FromMarkdownHandle }
233
237
*/
234
238
function exitAttributes ( ) {
235
- const list = /** @type {Array. <[string, string]> } */ (
239
+ const list = /** @type {Array<[string, string]> } */ (
236
240
this . getData ( 'directiveAttributes' )
237
241
)
238
- /** @type {Record. <string, string> } */
242
+ /** @type {Record<string, string> } */
239
243
const cleaned = { }
240
244
let index = - 1
241
245
@@ -336,13 +340,13 @@ function attributes(node, state) {
336
340
const quote = checkQuote ( state )
337
341
const subset = node . type === 'textDirective' ? [ quote ] : [ quote , '\n' , '\r' ]
338
342
const attrs = node . attributes || { }
339
- /** @type {Array. <string> } */
343
+ /** @type {Array<string> } */
340
344
const values = [ ]
341
- /** @type {string| undefined } */
345
+ /** @type {string | undefined } */
342
346
let classesFull
343
- /** @type {string| undefined } */
347
+ /** @type {string | undefined } */
344
348
let classes
345
- /** @type {string| undefined } */
349
+ /** @type {string | undefined } */
346
350
let id
347
351
/** @type {string } */
348
352
let key
@@ -359,9 +363,9 @@ function attributes(node, state) {
359
363
id = shortcut . test ( value ) ? '#' + value : quoted ( 'id' , value )
360
364
} else if ( key === 'class' ) {
361
365
const list = value . split ( / [ \t \n \r ] + / g)
362
- /** @type {Array. <string> } */
366
+ /** @type {Array<string> } */
363
367
const classesFullList = [ ]
364
- /** @type {Array. <string> } */
368
+ /** @type {Array<string> } */
365
369
const classesList = [ ]
366
370
let index = - 1
367
371
@@ -429,7 +433,20 @@ function fence(node) {
429
433
let size = 0
430
434
431
435
if ( node . type === 'containerDirective' ) {
432
- visitParents ( node , 'containerDirective' , onvisit )
436
+ visitParents ( node , function ( node , parents ) {
437
+ if ( node . type === 'containerDirective' ) {
438
+ let index = parents . length
439
+ let nesting = 0
440
+
441
+ while ( index -- ) {
442
+ if ( parents [ index ] . type === 'containerDirective' ) {
443
+ nesting ++
444
+ }
445
+ }
446
+
447
+ if ( nesting > size ) size = nesting
448
+ }
449
+ } )
433
450
size += 3
434
451
} else if ( node . type === 'leafDirective' ) {
435
452
size = 2
@@ -438,18 +455,4 @@ function fence(node) {
438
455
}
439
456
440
457
return ':' . repeat ( size )
441
-
442
- /** @type {import('unist-util-visit-parents').BuildVisitor<Root, Directive> } */
443
- function onvisit ( _ , parents ) {
444
- let index = parents . length
445
- let nesting = 0
446
-
447
- while ( index -- ) {
448
- if ( parents [ index ] . type === 'containerDirective' ) {
449
- nesting ++
450
- }
451
- }
452
-
453
- if ( nesting > size ) size = nesting
454
- }
455
458
}
0 commit comments