9
9
* @typedef {import('mdast').PhrasingContent } PhrasingContent
10
10
* @typedef {import('mdast').Text } Text
11
11
* @typedef {Content|Root } Node
12
- * @typedef {Extract<Node, import('mdast').Parent> } Parent
12
+ * @typedef {Exclude< Extract<Node, import('mdast').Parent>, Root > } Parent
13
13
*
14
14
* @typedef {import('unist-util-visit-parents').Test } Test
15
15
* @typedef {import('unist-util-visit-parents').VisitorResult } VisitorResult
16
16
*
17
17
* @typedef RegExpMatchObject
18
18
* @property {number } index
19
19
* @property {string } input
20
+ * @property {[Root, ...Array<Parent>, Text] } stack
20
21
*
21
22
* @typedef {string|RegExp } Find
22
23
* @typedef {string|ReplaceFunction } Replace
@@ -119,16 +120,18 @@ export const findAndReplace =
119
120
}
120
121
121
122
if ( grandparent ) {
122
- return handler ( node , grandparent )
123
+ // @ts -expect-error: stack is fine.
124
+ return handler ( node , parents )
123
125
}
124
126
}
125
127
126
128
/**
127
129
* @param {Text } node
128
- * @param {Parent } parent
130
+ * @param {[Root, ...Array< Parent>] } parents
129
131
* @returns {VisitorResult }
130
132
*/
131
- function handler ( node , parent ) {
133
+ function handler ( node , parents ) {
134
+ const parent = parents [ parents . length - 1 ]
132
135
const find = pairs [ pairIndex ] [ 0 ]
133
136
const replace = pairs [ pairIndex ] [ 1 ]
134
137
let start = 0
@@ -145,10 +148,13 @@ export const findAndReplace =
145
148
146
149
while ( match ) {
147
150
position = match . index
148
- let value = replace ( ...match , {
151
+ /** @type {RegExpMatchObject } */
152
+ const matchObject = {
149
153
index : match . index ,
150
- input : match . input
151
- } )
154
+ input : match . input ,
155
+ stack : [ ...parents , node ]
156
+ }
157
+ let value = replace ( ...match , matchObject )
152
158
153
159
if ( typeof value === 'string' ) {
154
160
value = value . length > 0 ? { type : 'text' , value} : undefined
0 commit comments