12
12
* @typedef {import('hast-util-is-element').Test } Test
13
13
* @typedef {import('unist-util-visit-parents').VisitorResult } VisitorResult
14
14
*
15
+ * @typedef RegExpMatchObject
16
+ * @property {number } index
17
+ * @property {string } input
18
+ * @property {[Root, ...Array<Element>, Text] } stack
19
+ *
15
20
* @typedef {string|RegExp } Find
16
21
* @typedef {string|ReplaceFunction } Replace
17
22
*
@@ -97,16 +102,17 @@ export function findAndReplace(tree, find, replace, options) {
97
102
}
98
103
99
104
if ( grandparent ) {
100
- return handler ( node , grandparent )
105
+ return handler ( node , parents )
101
106
}
102
107
}
103
108
104
109
/**
105
110
* @param {Text } node
106
- * @param {Parent } parent
111
+ * @param {Array<Root|Element> } parents
107
112
* @returns {VisitorResult }
108
113
*/
109
- function handler ( node , parent ) {
114
+ function handler ( node , parents ) {
115
+ const parent = parents [ parents . length - 1 ]
110
116
const find = pairs [ pairIndex ] [ 0 ]
111
117
const replace = pairs [ pairIndex ] [ 1 ]
112
118
let start = 0
@@ -122,7 +128,14 @@ export function findAndReplace(tree, find, replace, options) {
122
128
123
129
while ( match ) {
124
130
position = match . index
125
- let value = replace ( ...match , { index : match . index , input : match . input } )
131
+ /** @type {RegExpMatchObject } */
132
+ const matchObject = {
133
+ index : match . index ,
134
+ input : match . input ,
135
+ // @ts -expect-error: stack is fine.
136
+ stack : [ ...parents , node ]
137
+ }
138
+ let value = replace ( ...match , matchObject )
126
139
127
140
if ( typeof value === 'string' ) {
128
141
value = value . length > 0 ? { type : 'text' , value} : undefined
0 commit comments