1
1
/**
2
- * @typedef {import('mdast').BlockContent } BlockContent
3
- * @typedef {import('mdast').DefinitionContent } DefinitionContent
2
+ * @typedef {import('mdast').Nodes } Nodes
4
3
* @typedef {import('mdast').Paragraph } Paragraph
5
4
*
6
5
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
13
12
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
14
13
* @typedef {import('mdast-util-to-markdown').State } State
15
14
*
15
+ * @typedef {import('../index.js').Directive } Directive
16
16
* @typedef {import('../index.js').LeafDirective } LeafDirective
17
17
* @typedef {import('../index.js').TextDirective } TextDirective
18
- * @typedef {import('../index.js').Directive } Directive
19
18
*/
20
19
20
+ import { ok as assert } from 'devlop'
21
21
import { parseEntities } from 'parse-entities'
22
22
import { stringifyEntitiesLight } from 'stringify-entities'
23
23
import { visitParents } from 'unist-util-visit-parents'
@@ -144,7 +144,12 @@ function enter(type, token) {
144
144
* @param {Token } token
145
145
*/
146
146
function exitName ( token ) {
147
- const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
147
+ const node = this . stack [ this . stack . length - 1 ]
148
+ assert (
149
+ node . type === 'containerDirective' ||
150
+ node . type === 'leafDirective' ||
151
+ node . type === 'textDirective'
152
+ )
148
153
node . name = this . sliceSerialize ( token )
149
154
}
150
155
@@ -181,9 +186,8 @@ function enterAttributes() {
181
186
* @type {FromMarkdownHandle }
182
187
*/
183
188
function exitAttributeIdValue ( token ) {
184
- const list = /** @type {Array<[string, string]> } */ (
185
- this . data . directiveAttributes
186
- )
189
+ const list = this . data . directiveAttributes
190
+ assert ( list , 'expected `directiveAttributes`' )
187
191
list . push ( [
188
192
'id' ,
189
193
parseEntities ( this . sliceSerialize ( token ) , {
@@ -197,9 +201,8 @@ function exitAttributeIdValue(token) {
197
201
* @type {FromMarkdownHandle }
198
202
*/
199
203
function exitAttributeClassValue ( token ) {
200
- const list = /** @type {Array<[string, string]> } */ (
201
- this . data . directiveAttributes
202
- )
204
+ const list = this . data . directiveAttributes
205
+ assert ( list , 'expected `directiveAttributes`' )
203
206
list . push ( [
204
207
'class' ,
205
208
parseEntities ( this . sliceSerialize ( token ) , {
@@ -213,9 +216,8 @@ function exitAttributeClassValue(token) {
213
216
* @type {FromMarkdownHandle }
214
217
*/
215
218
function exitAttributeValue ( token ) {
216
- const list = /** @type {Array<[string, string]> } */ (
217
- this . data . directiveAttributes
218
- )
219
+ const list = this . data . directiveAttributes
220
+ assert ( list , 'expected `directiveAttributes`' )
219
221
list [ list . length - 1 ] [ 1 ] = parseEntities ( this . sliceSerialize ( token ) , {
220
222
attribute : true
221
223
} )
@@ -226,9 +228,8 @@ function exitAttributeValue(token) {
226
228
* @type {FromMarkdownHandle }
227
229
*/
228
230
function exitAttributeName ( token ) {
229
- const list = /** @type {Array<[string, string]> } */ (
230
- this . data . directiveAttributes
231
- )
231
+ const list = this . data . directiveAttributes
232
+ assert ( list , 'expected `directiveAttributes`' )
232
233
233
234
// Attribute names in CommonMark are significantly limited, so character
234
235
// references can’t exist.
@@ -240,9 +241,8 @@ function exitAttributeName(token) {
240
241
* @type {FromMarkdownHandle }
241
242
*/
242
243
function exitAttributes ( ) {
243
- const list = /** @type {Array<[string, string]> } */ (
244
- this . data . directiveAttributes
245
- )
244
+ const list = this . data . directiveAttributes
245
+ assert ( list , 'expected `directiveAttributes`' )
246
246
/** @type {Record<string, string> } */
247
247
const cleaned = { }
248
248
let index = - 1
@@ -259,7 +259,12 @@ function exitAttributes() {
259
259
260
260
this . data . directiveAttributes = undefined
261
261
this . resume ( ) // Drop EOLs
262
- const node = /** @type {Directive } */ ( this . stack [ this . stack . length - 1 ] )
262
+ const node = this . stack [ this . stack . length - 1 ]
263
+ assert (
264
+ node . type === 'containerDirective' ||
265
+ node . type === 'leafDirective' ||
266
+ node . type === 'textDirective'
267
+ )
263
268
node . attributes = cleaned
264
269
}
265
270
@@ -275,12 +280,12 @@ function exit(token) {
275
280
* @type {ToMarkdownHandle }
276
281
* @param {Directive } node
277
282
*/
278
- function handleDirective ( node , _ , state , safeOptions ) {
279
- const tracker = state . createTracker ( safeOptions )
283
+ function handleDirective ( node , _ , state , info ) {
284
+ const tracker = state . createTracker ( info )
280
285
const sequence = fence ( node )
281
286
const exit = state . enter ( node . type )
282
287
let value = tracker . move ( sequence + ( node . name || '' ) )
283
- /** @type {Paragraph | LeafDirective | TextDirective | undefined } */
288
+ /** @type {LeafDirective | Paragraph | TextDirective | undefined } */
284
289
let label
285
290
286
291
if ( node . type === 'containerDirective' ) {
@@ -424,7 +429,7 @@ function attributes(node, state) {
424
429
}
425
430
426
431
/**
427
- * @param {BlockContent | DefinitionContent } node
432
+ * @param {Nodes } node
428
433
* @returns {node is Paragraph & {data: {directiveLabel: true}} }
429
434
*/
430
435
function inlineDirectiveLabel ( node ) {
0 commit comments