1
1
/**
2
+ * @typedef {import('mdast').BlockContent } BlockContent
3
+ * @typedef {import('mdast').Paragraph } Paragraph
2
4
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
3
5
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
4
- * @typedef {import('mdast-util-to -markdown/lib/types.js ').Node } Node
5
- * @typedef {import('mdast-util-to -markdown/lib/types.js ').Parent } Parent
6
+ * @typedef {import('mdast-util-from -markdown').CompileContext } CompileContext
7
+ * @typedef {import('mdast-util-from -markdown').Token } Token
6
8
* @typedef {import('mdast-util-to-markdown/lib/types.js').Handle } ToMarkdownHandle
7
9
* @typedef {import('mdast-util-to-markdown/lib/types.js').Context } Context
8
10
* @typedef {import('mdast-util-to-markdown/lib/types.js').Options } ToMarkdownExtension
9
- *
10
- * @typedef {Record<string, string> } Attributes
11
- * @typedef {{name: string, attributes?: Attributes} } Directive
12
- *
13
- * @typedef {Parent & Directive & {type: 'textDirective', children: Array.<import('mdast').PhrasingContent>} } TextDirective
14
- * @typedef {Parent & Directive & {type: 'leafDirective', children: Array.<import('mdast').PhrasingContent>} } LeafDirective
15
- * @typedef {Parent & Directive & {type: 'containerDirective', children: Array.<import('mdast').BlockContent>} } ContainerDirective
11
+ * @typedef {import('./complex-types').ContainerDirective } ContainerDirective
12
+ * @typedef {import('./complex-types').LeafDirective } LeafDirective
13
+ * @typedef {import('./complex-types').TextDirective } TextDirective
14
+ * @typedef {ContainerDirective|LeafDirective|TextDirective } Directive
16
15
*/
17
16
18
17
import { decodeEntity } from 'parse-entities/decode-entity.js'
@@ -112,21 +111,21 @@ function enterText(token) {
112
111
}
113
112
114
113
/**
115
- * @this {ThisParameterType<FromMarkdownHandle> }
116
- * @param {string } type
117
- * @param {Parameters<FromMarkdownHandle>[0] } token
114
+ * @this {CompileContext }
115
+ * @param {Directive['type'] } type
116
+ * @param {Token } token
118
117
*/
119
118
function enter ( type , token ) {
120
- // @ts -expect-error: custom node.
121
119
this . enter ( { type, name : '' , attributes : { } , children : [ ] } , token )
122
120
}
123
121
124
122
/**
125
- * @this {ThisParameterType<FromMarkdownHandle> }
126
- * @param {Parameters<FromMarkdownHandle>[0] } token
123
+ * @this {CompileContext }
124
+ * @param {Token } token
127
125
*/
128
126
function exitName ( token ) {
129
- this . stack [ this . stack . length - 1 ] . name = this . sliceSerialize ( token )
127
+ const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
128
+ node . name = this . sliceSerialize ( token )
130
129
}
131
130
132
131
/** @type {FromMarkdownHandle } */
@@ -150,33 +149,33 @@ function enterAttributes() {
150
149
151
150
/** @type {FromMarkdownHandle } */
152
151
function exitAttributeIdValue ( token ) {
153
- /** @type {Array.<[string, string]> } */
154
- // @ts -expect-error: custom.
155
- const list = this . getData ( 'directiveAttributes' )
152
+ const list = /** @type {Array.<[string, string]> } */ (
153
+ this . getData ( 'directiveAttributes' )
154
+ )
156
155
list . push ( [ 'id' , decodeLight ( this . sliceSerialize ( token ) ) ] )
157
156
}
158
157
159
158
/** @type {FromMarkdownHandle } */
160
159
function exitAttributeClassValue ( token ) {
161
- /** @type {Array.<[string, string]> } */
162
- // @ts -expect-error: custom.
163
- const list = this . getData ( 'directiveAttributes' )
160
+ const list = /** @type {Array.<[string, string]> } */ (
161
+ this . getData ( 'directiveAttributes' )
162
+ )
164
163
list . push ( [ 'class' , decodeLight ( this . sliceSerialize ( token ) ) ] )
165
164
}
166
165
167
166
/** @type {FromMarkdownHandle } */
168
167
function exitAttributeValue ( token ) {
169
- /** @type {Array.<[string, string]> } */
170
- // @ts -expect-error: custom.
171
- const list = this . getData ( 'directiveAttributes' )
168
+ const list = /** @type {Array.<[string, string]> } */ (
169
+ this . getData ( 'directiveAttributes' )
170
+ )
172
171
list [ list . length - 1 ] [ 1 ] = decodeLight ( this . sliceSerialize ( token ) )
173
172
}
174
173
175
174
/** @type {FromMarkdownHandle } */
176
175
function exitAttributeName ( token ) {
177
- /** @type {Array.<[string, string]> } */
178
- // @ts -expect-error: custom.
179
- const list = this . getData ( 'directiveAttributes' )
176
+ const list = /** @type {Array.<[string, string]> } */ (
177
+ this . getData ( 'directiveAttributes' )
178
+ )
180
179
181
180
// Attribute names in CommonMark are significantly limited, so character
182
181
// references can’t exist.
@@ -185,9 +184,9 @@ function exitAttributeName(token) {
185
184
186
185
/** @type {FromMarkdownHandle } */
187
186
function exitAttributes ( ) {
188
- /** @type {Array.<[string, string]> } */
189
- // @ts -expect-error: custom.
190
- const list = this . getData ( 'directiveAttributes' )
187
+ const list = /** @type {Array.<[string, string]> } */ (
188
+ this . getData ( 'directiveAttributes' )
189
+ )
191
190
/** @type {Record.<string, string> } */
192
191
const cleaned = { }
193
192
let index = - 1
@@ -204,7 +203,8 @@ function exitAttributes() {
204
203
205
204
this . setData ( 'directiveAttributes' )
206
205
this . resume ( ) // Drop EOLs
207
- this . stack [ this . stack . length - 1 ] . attributes = cleaned
206
+ const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
207
+ node . attributes = cleaned
208
208
}
209
209
210
210
/** @type {FromMarkdownHandle } */
@@ -214,7 +214,7 @@ function exit(token) {
214
214
215
215
/**
216
216
* @type {ToMarkdownHandle }
217
- * @param {TextDirective|LeafDirective|ContainerDirective } node
217
+ * @param {Directive } node
218
218
*/
219
219
function handleDirective ( node , _ , context ) {
220
220
const prefix = fence ( node )
@@ -241,18 +241,18 @@ function peekDirective() {
241
241
}
242
242
243
243
/**
244
- * @param {TextDirective|LeafDirective|ContainerDirective } node
244
+ * @param {Directive } node
245
245
* @param {Context } context
246
246
* @returns {string }
247
247
*/
248
248
function label ( node , context ) {
249
- /** @type {Parent } */
249
+ /** @type {Directive|Paragraph } */
250
250
let label = node
251
251
252
252
if ( node . type === 'containerDirective' ) {
253
- if ( ! inlineDirectiveLabel ( node ) ) return ''
254
- // @ts -expect-error: we just asserted it’s a parent.
255
- label = node . children [ 0 ]
253
+ const head = ( node . children || [ ] ) [ 0 ]
254
+ if ( ! inlineDirectiveLabel ( head ) ) return ''
255
+ label = head
256
256
}
257
257
258
258
const exit = context . enter ( 'label' )
@@ -264,7 +264,7 @@ function label(node, context) {
264
264
}
265
265
266
266
/**
267
- * @param {TextDirective|LeafDirective|ContainerDirective } node
267
+ * @param {Directive } node
268
268
* @param {Context } context
269
269
* @returns {string }
270
270
*/
@@ -348,29 +348,27 @@ function attributes(node, context) {
348
348
}
349
349
350
350
/**
351
- * @param {TextDirective|LeafDirective| ContainerDirective } node
351
+ * @param {ContainerDirective } node
352
352
* @param {Context } context
353
353
* @returns {string }
354
354
*/
355
355
function content ( node , context ) {
356
- return containerFlow (
357
- inlineDirectiveLabel ( node )
358
- ? Object . assign ( { } , node , { children : node . children . slice ( 1 ) } )
359
- : node ,
360
- context
361
- )
356
+ const head = ( node . children || [ ] ) [ 0 ]
357
+
358
+ if ( inlineDirectiveLabel ( head ) ) {
359
+ node = Object . assign ( { } , node , { children : node . children . slice ( 1 ) } )
360
+ }
361
+
362
+ return containerFlow ( node , context )
362
363
}
363
364
364
365
/**
365
- * @param {TextDirective|LeafDirective|ContainerDirective } node
366
- * @returns {boolean }
366
+ * @param {BlockContent } node
367
+ * @returns {node is Paragraph & {data: {directiveLabel: boolean}} }
367
368
*/
368
369
function inlineDirectiveLabel ( node ) {
369
370
return Boolean (
370
- node . children &&
371
- node . children [ 0 ] &&
372
- node . children [ 0 ] . data &&
373
- node . children [ 0 ] . data . directiveLabel
371
+ node && node . type === 'paragraph' && node . data && node . data . directiveLabel
374
372
)
375
373
}
376
374
@@ -395,7 +393,7 @@ function decodeIfPossible($0, $1) {
395
393
}
396
394
397
395
/**
398
- * @param {TextDirective|LeafDirective|ContainerDirective } node
396
+ * @param {Directive } node
399
397
* @returns {string }
400
398
*/
401
399
function fence ( node ) {
@@ -412,7 +410,7 @@ function fence(node) {
412
410
413
411
return ':' . repeat ( size )
414
412
415
- /** @type {import('unist-util-visit-parents').Visitor<TextDirective|LeafDirective|ContainerDirective > } */
413
+ /** @type {import('unist-util-visit-parents').Visitor<Directive > } */
416
414
function onvisit ( _ , parents ) {
417
415
let index = parents . length
418
416
let nesting = 0
0 commit comments