1414 * List,
1515 * Nodes,
1616 * Paragraph,
17- * Parent,
1817 * PhrasingContent,
1918 * ReferenceType,
2019 * Root,
2524 * @import {
2625 * Encoding,
2726 * Event,
28- * ParseOptions,
29- * TokenizeContext,
3027 * Token,
3128 * Value
3229 * } from 'micromark-util-types'
3330 * @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'
13540 */
13641
13742import { ok as assert } from 'devlop'
@@ -562,24 +467,14 @@ function compiler(options) {
562467 }
563468
564469 /**
565- * @this {CompileContext}
566- * @returns {undefined }
470+ * @type {CompileContext['buffer'] }
567471 */
568472 function buffer ( ) {
569473 this . stack . push ( { type : 'fragment' , children : [ ] } )
570474 }
571475
572476 /**
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'] }
583478 */
584479 function enter ( node , token , errorHandler ) {
585480 const parent = this . stack [ this . stack . length - 1 ]
@@ -589,7 +484,7 @@ function compiler(options) {
589484 const siblings = parent . children
590485 siblings . push ( node )
591486 this . stack . push ( node )
592- this . tokenStack . push ( [ token , errorHandler ] )
487+ this . tokenStack . push ( [ token , errorHandler || undefined ] )
593488 node . position = {
594489 start : point ( token . start ) ,
595490 // @ts -expect-error: `end` will be patched later.
@@ -620,14 +515,7 @@ function compiler(options) {
620515 }
621516
622517 /**
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'] }
631519 */
632520 function exit ( token , onExitError ) {
633521 const node = this . stack . pop ( )
@@ -657,8 +545,7 @@ function compiler(options) {
657545 }
658546
659547 /**
660- * @this {CompileContext}
661- * @returns {string }
548+ * @type {CompileContext['resume'] }
662549 */
663550 function resume ( ) {
664551 return toString ( this . stack . pop ( ) )
0 commit comments