14
14
* List,
15
15
* Nodes,
16
16
* Paragraph,
17
- * Parent,
18
17
* PhrasingContent,
19
18
* ReferenceType,
20
19
* Root,
25
24
* @import {
26
25
* Encoding,
27
26
* Event,
28
- * ParseOptions,
29
- * TokenizeContext,
30
27
* Token,
31
28
* Value
32
29
* } from 'micromark-util-types'
33
30
* @import {Point} from 'unist'
34
- * @import {CompileData} from 'mdast-util-from-markdown'
35
- */
36
-
37
- /**
38
- * @typedef {Omit<Parent, 'children' | 'type'> & {type: 'fragment', children: Array<PhrasingContent>} } Fragment
39
- */
40
-
41
- /**
42
- * @callback Transform
43
- * Extra transform, to change the AST afterwards.
44
- * @param {Root } tree
45
- * Tree to transform.
46
- * @returns {Root | null | undefined | void }
47
- * New tree or nothing (in which case the current tree is used).
48
- *
49
- * @callback Handle
50
- * Handle a token.
51
- * @param {CompileContext } this
52
- * Context.
53
- * @param {Token } token
54
- * Current token.
55
- * @returns {undefined | void }
56
- * Nothing.
57
- *
58
- * @typedef {Record<string, Handle> } Handles
59
- * Token types mapping to handles
60
- *
61
- * @callback OnEnterError
62
- * Handle the case where the `right` token is open, but it is closed (by the
63
- * `left` token) or because we reached the end of the document.
64
- * @param {Omit<CompileContext, 'sliceSerialize'> } this
65
- * Context.
66
- * @param {Token | undefined } left
67
- * Left token.
68
- * @param {Token } right
69
- * Right token.
70
- * @returns {undefined }
71
- * Nothing.
72
- *
73
- * @callback OnExitError
74
- * Handle the case where the `right` token is open but it is closed by
75
- * exiting the `left` token.
76
- * @param {Omit<CompileContext, 'sliceSerialize'> } this
77
- * Context.
78
- * @param {Token } left
79
- * Left token.
80
- * @param {Token } right
81
- * Right token.
82
- * @returns {undefined }
83
- * Nothing.
84
- *
85
- * @typedef {[Token, OnEnterError | undefined] } TokenTuple
86
- * Open token on the stack, with an optional error handler for when
87
- * that token isn’t closed properly.
88
- */
89
-
90
- /**
91
- * @typedef Config
92
- * Configuration.
93
- *
94
- * We have our defaults, but extensions will add more.
95
- * @property {Array<string> } canContainEols
96
- * Token types where line endings are used.
97
- * @property {Handles } enter
98
- * Opening handles.
99
- * @property {Handles } exit
100
- * Closing handles.
101
- * @property {Array<Transform> } transforms
102
- * Tree transforms.
103
- *
104
- * @typedef {Partial<Config> } Extension
105
- * Change how markdown tokens from micromark are turned into mdast.
106
- *
107
- * @typedef CompileContext
108
- * mdast compiler context.
109
- * @property {Array<Fragment | Nodes> } stack
110
- * Stack of nodes.
111
- * @property {Array<TokenTuple> } tokenStack
112
- * Stack of tokens.
113
- * @property {(this: CompileContext) => undefined } buffer
114
- * Capture some of the output data.
115
- * @property {(this: CompileContext) => string } resume
116
- * Stop capturing and access the output data.
117
- * @property {(this: CompileContext, node: Nodes, token: Token, onError?: OnEnterError) => undefined } enter
118
- * Enter a node.
119
- * @property {(this: CompileContext, token: Token, onError?: OnExitError) => undefined } exit
120
- * Exit a node.
121
- * @property {TokenizeContext['sliceSerialize'] } sliceSerialize
122
- * Get the string value of a token.
123
- * @property {Config } config
124
- * Configuration.
125
- * @property {CompileData } data
126
- * Info passed around; key/value store.
127
- *
128
- * @typedef FromMarkdownOptions
129
- * Configuration for how to build mdast.
130
- * @property {Array<Extension | Array<Extension>> | null | undefined } [mdastExtensions]
131
- * Extensions for this utility to change how tokens are turned into a tree.
132
- *
133
- * @typedef {ParseOptions & FromMarkdownOptions } Options
134
- * Configuration.
31
+ * @import {
32
+ * CompileContext,
33
+ * CompileData,
34
+ * Config,
35
+ * Extension,
36
+ * Handle,
37
+ * OnEnterError,
38
+ * Options
39
+ * } from './types.js'
135
40
*/
136
41
137
42
import { ok as assert } from 'devlop'
@@ -562,24 +467,14 @@ function compiler(options) {
562
467
}
563
468
564
469
/**
565
- * @this {CompileContext}
566
- * @returns {undefined }
470
+ * @type {CompileContext['buffer'] }
567
471
*/
568
472
function buffer ( ) {
569
473
this . stack . push ( { type : 'fragment' , children : [ ] } )
570
474
}
571
475
572
476
/**
573
- * @this {CompileContext}
574
- * Context.
575
- * @param {Nodes } node
576
- * Node to enter.
577
- * @param {Token } token
578
- * Corresponding token.
579
- * @param {OnEnterError | undefined } [errorHandler]
580
- * Handle the case where this token is open, but it is closed by something else.
581
- * @returns {undefined }
582
- * Nothing.
477
+ * @type {CompileContext['enter'] }
583
478
*/
584
479
function enter ( node , token , errorHandler ) {
585
480
const parent = this . stack [ this . stack . length - 1 ]
@@ -589,7 +484,7 @@ function compiler(options) {
589
484
const siblings = parent . children
590
485
siblings . push ( node )
591
486
this . stack . push ( node )
592
- this . tokenStack . push ( [ token , errorHandler ] )
487
+ this . tokenStack . push ( [ token , errorHandler || undefined ] )
593
488
node . position = {
594
489
start : point ( token . start ) ,
595
490
// @ts -expect-error: `end` will be patched later.
@@ -620,14 +515,7 @@ function compiler(options) {
620
515
}
621
516
622
517
/**
623
- * @this {CompileContext}
624
- * Context.
625
- * @param {Token } token
626
- * Corresponding token.
627
- * @param {OnExitError | undefined } [onExitError]
628
- * Handle the case where another token is open.
629
- * @returns {undefined }
630
- * Nothing.
518
+ * @type {CompileContext['exit'] }
631
519
*/
632
520
function exit ( token , onExitError ) {
633
521
const node = this . stack . pop ( )
@@ -657,8 +545,7 @@ function compiler(options) {
657
545
}
658
546
659
547
/**
660
- * @this {CompileContext}
661
- * @returns {string }
548
+ * @type {CompileContext['resume'] }
662
549
*/
663
550
function resume ( ) {
664
551
return toString ( this . stack . pop ( ) )
0 commit comments